1. 程式人生 > >三個基本排序演算法執行效率比較(氣泡排序,選擇排序和插入排序)

三個基本排序演算法執行效率比較(氣泡排序,選擇排序和插入排序)

1、冒泡演算法。

冒泡演算法是最基礎的一個排序演算法,每次使用第一個值和身後相鄰的值進行比較,如果是升序將大數向左邊交換,降序則向右邊交換。最終將大數移動到一邊,最終排成一個序列:

public class Sorting
{
    public void BubbleSorting()
    {
        int[] arr = new int[10];
        Random rd = new Random(100);
        for (int i = 0; i < arr.Length; i++)
        {
            arr[i] = rd.Next(1, 100);
        }
        Console.WriteLine("Init array:");
        Print(arr);
        Console.WriteLine("Sorting:");
        for (int i = arr.Length - 1; i >= 1; i--)
        {
            for (int j = 0; j <= i - 1; j++)
            {
                if (arr[j] > arr[j + 1])
                {
                    Swap(ref arr[j], ref arr[j + 1]);
                }
            }

            Print(arr);
        }
        Console.WriteLine("Sort Result:");
        Print(arr);
    }

    void Swap(ref int a, ref int b)
    {
        var temp = a;
        a = b;
        b = temp;
    }
    void Print(int[] list)
    {
        for (int i = 0; i < list.Length; i++)
        {
            Console.Write("" + list[i]);
        } 
        Console.WriteLine();
    }
}

結果:

image

2、選擇排序

選擇排序需要兩層迴圈來實現,外層迴圈控制次數,內層迴圈控制找到最小的值。然後將內層迴圈找到的最小值與外層迴圈本次索引對應元素進行交換,直至遍歷完整個陣列。

public void SelectionSort()
{
    int[] arr = InitArray(10);
    int length = 10;
    Console.WriteLine("Sorting:");
    for (int i = 0; i < length; i++)
    {
        int min = i;
        for (int j = i + 1; j < length; j++)
        {
            if (arr[min] > arr[j]) min = j;
        }
        Swap(ref arr[i], ref arr[min]);
        Print(arr);
    }
    Console.WriteLine("Sort Result:");
    Print(arr);
}

結果:

image

3、插入排序

插入排序有兩層迴圈,外層迴圈逐個遍歷陣列元素,內層迴圈把外層迴圈的元素與該元素在內層迴圈的下一個元素進行比較,如果外層迴圈選擇的元素小於內層迴圈選擇的元素,那麼陣列元素都行右移動為內層迴圈元素留出位置。

public void InsertionSort()
{
    int[] arr = InitArray(10);
    int length = 10;
    Console.WriteLine("Sorting:");
    for (int i = 1; i < length; i++)
    {
        int temp = arr[i];
        int inner=i;
        while (inner > 0 && arr[inner - 1] >= temp)
        {
            arr[inner] = arr[inner - 1];
            inner--;
        }
        arr[inner] = temp;
        Print(arr);
    }
    Console.WriteLine("Sort Result:");
    Print(arr);
}

結果:

image

4、三種演算法的效率

做一個簡單的比較,這裡的初始化資料在每種演算法之中, 因為每個演算法都包含初始化,因此不會影響到比較.

測試程式碼:

static void TestSorting(int size)
{    
    Sorting sort = new Sorting();
    Stopwatch watch = new Stopwatch();
    watch.Start();
    sort.BubbleAscSorting(size);
    watch.Stop();
    Console.WriteLine("BubbleAscSorting Time Milliseconds:" + watch.ElapsedMilliseconds);
    watch.Restart();
    sort.SelectionSort(size);
    watch.Stop();
    Console.WriteLine("SelectionSort Time Milliseconds:" + watch.ElapsedMilliseconds);
    watch.Restart();
    sort.InsertionSort(size);
    Console.WriteLine("InsertionSort Time Milliseconds:" + watch.ElapsedMilliseconds);
    Console.ReadKey();
}

1000個整數結果:

 image

10000個整數結果:

image

100000個整數結果:

image

從測試結果得到下面的表格:

Bubble Selection Insertion Bubble/Selection Bubble/Insertion Selection/Insertion
1000 15 4 3 3.75 5 1.333333333
10000 1342 412 283 3.257281553 4.74204947 1.455830389
100000 125212 40794 27570 3.069372947 4.541603192 1.479651795
Avg 3.358884833 4.761217554 1.422938506

忽略測試環境,因為三個演算法都是在同一個環境中跑的, 可以得出如下結論:

1.冒泡演算法效率最低。

2.插入演算法效率最高。

3.選擇演算法是冒泡演算法的3.3倍。

4.插入演算法是冒泡演算法的4.7倍。

5.插入演算法是選擇演算法的1.4陪。