一杯清酒邀明月
天下本无事,庸人扰之而烦耳。

归并排序是建立在归并操作上的一种有效的排序算法。该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。

归并操作(merge),也叫归并算法,指的是将两个已经排序的序列合并成一个序列的操作。归并排序算法依赖归并操作。

归并操作步骤如下:(两个有序序列分别用A[aMax]、B[bMax]表示)

1)设定两个指针(iA、iB),最初位置分别为两个已经排序序列的起始位置;

2)比较A[iA]、B[iB],选择较小的放入合并空间,并移动对应指针到下一位置;

3)重复步骤二直至读完序列A或B其中之一的数据;

4)将另一序列剩下的所有元素直接复制到合并序列尾。

归并操作实现代码如下:

 1 private static int[] mergearray(int[] arrayA,int[] arrayB)  //归并操作
 2 {
 3     int[] tempArray = new int[arrayA.Length + arrayB.Length];
 4     int itemp = 0;
 5     
 6     int iA =0, iB=0;
 7     
 8     while(iA < arrayA.Length && iB < arrayB.Length)
 9     {
10         if(arrayA[iA] < arrayB[iB])
11         {
12             tempArray[itemp++] = arrayA[iA++];
13         }
14         else
15         {
16             tempArray[itemp++] = arrayB[iB++];
17         }
18     }
19     
20     while(iA < arrayA.Length)
21     {
22         tempArray[itemp++] = arrayA[iA++];
23     }
24     while(iB < arrayB.Length)
25     {
26         tempArray[itemp++] = arrayB[iB++];
27     }
28     
29     return tempArray;
30 }

从归并操作到归并排序:

通过归并操作我们可以将两个有序的序列合并成一个有序的序列,那我们怎么实现一个无序数组的排序呢?我们会想“如果将这个无序的数组分解成两个有序的数组就好了。”但如何让这二组组内数据有序呢?“可以将这两个数组各自再分成两组”。对,依次类推,就这样分下去,最终我们得到的是两个一个元素的数组(我们可以认为它是有序的了),反过来我们通过一次次归并操作,便可得到一个有序的数组。对,这是递归的应用。

前面,不知不觉还是说的有点绕,整理一下,其基本思路如下:

1)我们将一个要排序的无序数组,分解成两个数组;

2)重复步骤一,直到子数组只有一个元素;

3)使用归并操作获取所求。

实现代码如下:

 1 private static int[] mergesort(int[] array) //归并排序
 2 {
 3     if(array.Length > 1)
 4     {
 5         int iFront =0, iBack=array.Length/2;
 6         int lengthFront = array.Length/2, lengthBack = array.Length - array.Length/2;
 7         int[] arrayFront = new int[lengthFront];
 8         int[] arrayBack = new int[lengthBack];
 9         Array.Copy(array,iFront, arrayFront, 0, lengthFront);
10         Array.Copy(array, iBack, arrayBack, 0, lengthBack);
11  
12         arrayFront = mergesort(arrayFront);
13         arrayBack = mergesort(arrayBack);
14         array = mergearray(arrayFront, arrayBack);
15     }
16     return array;
17 }

华丽的分割线后,附上测试代码:

 1 /*
 2  * 由SharpDevelop创建。
 3  * 用户: Jensen
 4  * 日期: 2014/6/1
 5  * 时间: 8:00
 6  * 
 7  * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 8  */
 9 using System;
10 using System.Diagnostics;
11  
12  
13 namespace 算法测试
14 {
15     class Program
16     {
17         private const int testDataCount = 200000;
18             
19         public static void Main(string[] args)
20         {
21             Console.WriteLine("敲入任意字符进行测试:");
22             Console.ReadKey(true);
23             //测试准备:
24             int[] arrayTest = CreateTestData();
25             //测试:
26             Stopwatch sw = new Stopwatch();
27             sw.Start();
28             arrayTest = mergesort(arrayTest);
29             sw.Stop();
30         
31             Console.WriteLine(sw.Elapsed.TotalMilliseconds);
32         }
33         
34         private static int[] CreateTestData()
35         {
36             int[] testData = new int[testDataCount];
37             for(int i=0;i<testData.Length;i++)
38             {
39                 Random random = new Random(i);
40                 testData[i] = (int)(random.NextDouble() * 10000);
41             }
42             return testData;
43         }
44         
45         private static int[] mergearray(int[] arrayA,int[] arrayB)  //归并操作
46         {
47             int[] tempArray = new int[arrayA.Length + arrayB.Length];
48             int itemp = 0;
49             
50             int iA =0, iB=0;
51             
52             while(iA < arrayA.Length && iB < arrayB.Length)
53             {
54                 if(arrayA[iA] < arrayB[iB])
55                 {
56                     tempArray[itemp++] = arrayA[iA++];
57                 }
58                 else
59                 {
60                     tempArray[itemp++] = arrayB[iB++];
61                 }
62             }
63             
64             while(iA < arrayA.Length)
65             {
66                 tempArray[itemp++] = arrayA[iA++];
67             }
68             while(iB < arrayB.Length)
69             {
70                 tempArray[itemp++] = arrayB[iB++];
71             }
72             
73             return tempArray;
74         }
75         
76         private static int[] mergesort(int[] array) //归并排序
77         {
78             if(array.Length > 1)
79             {
80                 int iFront =0, iBack=array.Length/2;
81                 int lengthFront = array.Length/2, lengthBack = array.Length - array.Length/2;
82                 int[] arrayFront = new int[lengthFront];
83                 int[] arrayBack = new int[lengthBack];
84                 Array.Copy(array,iFront, arrayFront, 0, lengthFront);
85                 Array.Copy(array, iBack, arrayBack, 0, lengthBack);
86  
87                 arrayFront = mergesort(arrayFront);
88                 arrayBack = mergesort(arrayBack);
89                 array = mergearray(arrayFront, arrayBack);
90             }
91             return array;
92         }
93         
94     }
95 }

在Release环境下:

排序10000条数据,约2.3ms.

image

排序20000条数据,约5ms:

image

排序200000条数据,约45ms。


另,附冒泡算法排序200000条数据的测试结果以做对比:

image

posted on 2021-02-20 16:15  一杯清酒邀明月  阅读(129)  评论(0编辑  收藏  举报

/* 看板娘 */