归并排序

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

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

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

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

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

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

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

归并操作实现代码如下:

private static int[] mergearray(int[] arrayA,int[] arrayB)  //归并操作
{
    int[] tempArray = new int[arrayA.Length + arrayB.Length];
    int itemp = 0;
    
    int iA =0, iB=0;
    
    while(iA < arrayA.Length && iB < arrayB.Length)
    {
        if(arrayA[iA] < arrayB[iB])
        {
            tempArray[itemp++] = arrayA[iA++];
        }
        else
        {
            tempArray[itemp++] = arrayB[iB++];
        }
    }
    
    while(iA < arrayA.Length)
    {
        tempArray[itemp++] = arrayA[iA++];
    }
    while(iB < arrayB.Length)
    {
        tempArray[itemp++] = arrayB[iB++];
    }
    
    return tempArray;
}

从归并操作到归并排序:

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

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

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

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

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

实现代码如下:

private static int[] mergesort(int[] array) //归并排序
{
    if(array.Length > 1)
    {
        int iFront =0, iBack=array.Length/2;
        int lengthFront = array.Length/2, lengthBack = array.Length - array.Length/2;
        int[] arrayFront = new int[lengthFront];
        int[] arrayBack = new int[lengthBack];
        Array.Copy(array,iFront, arrayFront, 0, lengthFront);
        Array.Copy(array, iBack, arrayBack, 0, lengthBack);
 
        arrayFront = mergesort(arrayFront);
        arrayBack = mergesort(arrayBack);
        array = mergearray(arrayFront, arrayBack);
    }
    return array;
}

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

/*
 * 由SharpDevelop创建。
 * 用户: Jensen
 * 日期: 2014/6/1
 * 时间: 8:00
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.Diagnostics;
 
 
namespace 算法测试
{
    class Program
    {
        private const int testDataCount = 200000;
            
        public static void Main(string[] args)
        {
            Console.WriteLine("敲入任意字符进行测试:");
            Console.ReadKey(true);
            //测试准备:
            int[] arrayTest = CreateTestData();
            //测试:
            Stopwatch sw = new Stopwatch();
            sw.Start();
            arrayTest = mergesort(arrayTest);
            sw.Stop();
        
            Console.WriteLine(sw.Elapsed.TotalMilliseconds);
        }
        
        private static int[] CreateTestData()
        {
            int[] testData = new int[testDataCount];
            for(int i=0;i<testData.Length;i++)
            {
                Random random = new Random(i);
                testData[i] = (int)(random.NextDouble() * 10000);
            }
            return testData;
        }
        
        private static int[] mergearray(int[] arrayA,int[] arrayB)  //归并操作
        {
            int[] tempArray = new int[arrayA.Length + arrayB.Length];
            int itemp = 0;
            
            int iA =0, iB=0;
            
            while(iA < arrayA.Length && iB < arrayB.Length)
            {
                if(arrayA[iA] < arrayB[iB])
                {
                    tempArray[itemp++] = arrayA[iA++];
                }
                else
                {
                    tempArray[itemp++] = arrayB[iB++];
                }
            }
            
            while(iA < arrayA.Length)
            {
                tempArray[itemp++] = arrayA[iA++];
            }
            while(iB < arrayB.Length)
            {
                tempArray[itemp++] = arrayB[iB++];
            }
            
            return tempArray;
        }
        
        private static int[] mergesort(int[] array) //归并排序
        {
            if(array.Length > 1)
            {
                int iFront =0, iBack=array.Length/2;
                int lengthFront = array.Length/2, lengthBack = array.Length - array.Length/2;
                int[] arrayFront = new int[lengthFront];
                int[] arrayBack = new int[lengthBack];
                Array.Copy(array,iFront, arrayFront, 0, lengthFront);
                Array.Copy(array, iBack, arrayBack, 0, lengthBack);
 
                arrayFront = mergesort(arrayFront);
                arrayBack = mergesort(arrayBack);
                array = mergearray(arrayFront, arrayBack);
            }
            return array;
        }
        
    }
}

在Release环境下:

排序10000条数据,约2.3ms.

image

排序20000条数据,约5ms:

image

排序200000条数据,约45ms。


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

image

原文地址:https://www.cnblogs.com/hanzhaoxin/p/3763508.html