A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© sunrise2 高级黑马   /  2014-7-23 23:26  /  907 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 sunrise2 于 2014-7-23 23:31 编辑

1、归并排序
归并排序先分解要排序的序列,从1分成2,2分成4,依次分解,
当分解到只有1个一组的时候,就可以排序这些分组,
然后依次合并回原来的序列中,这样就可以排序所有数据。
合并排序比堆排序稍微快一点,但是需要比堆排序多一倍的内存空间,
因为它需要一个额外的数组。
  1. /// <summary>
  2.     /// 归并排序
  3.     /// </summary>
  4.     public class MergeSort : ISort
  5.     {
  6.         public int[] Sort(int[] array)
  7.         {
  8.             if (array != null)
  9.             {
  10.                 int[] temp = new int[array.Length];
  11.                 SortLeftAndRight(array, 0, array.Length - 1, temp);
  12.             }
  13.             return array;
  14.         }
  15.         /// <summary>
  16.         /// 对array[first]-array[middle]和array[middle+1]-array[last]进行并归
  17.         /// </summary>
  18.         /// <param name="array"></param>
  19.         /// <param name="first"></param>
  20.         /// <param name="middle"></param>
  21.         /// <param name="last"></param>
  22.         /// <param name="temp"></param>
  23.         private void Merge(int[] array, int first, int middle, int last, int[] temp)
  24.         {
  25.             int i = first;
  26.             int n = middle;
  27.             int j = middle + 1;
  28.             int m = last;
  29.             int k = 0;
  30.             while (i <= n && j <= m)
  31.             {
  32.                 if (array[i] <= array[j])
  33.                 {
  34.                     temp[k++] = array[i++];
  35.                 }
  36.                 else
  37.                 {
  38.                     temp[k++] = array[j++];
  39.                 }
  40.             }
  41.             while (i <= n)
  42.             {
  43.                 temp[k++] = array[i++];
  44.             }
  45.             while (j <= m)
  46.             {
  47.                 temp[k++] = array[j++];
  48.             }
  49.             for (i = 0; i < k; i++)
  50.             {
  51.                 array[first + i] = temp[i];
  52.             }
  53.         }

  54.         /// <summary>
  55.         /// 递归分治
  56.         /// </summary>
  57.         /// <param name="array"></param>
  58.         /// <param name="first"></param>
  59.         /// <param name="last"></param>
  60.         /// <param name="temp"></param>
  61.         private void SortLeftAndRight(int[] array, int first, int last, int[] temp)
  62.         {
  63.             if (first < last)
  64.             {
  65.                 int middle = (first + last) / 2;
  66.                 SortLeftAndRight(array, first, middle, temp);
  67.                 SortLeftAndRight(array, middle + 1, last, temp);
  68.                 Merge(array, first, middle, last, temp);
  69.             }
  70.         }

  71.     }
复制代码
2、快速排序      快速排序是一个就地排序,分而治之,大规模递归的算法。从本质上来说,它是归并排序的就地版本。快速排序可以由下面四步组成。
     (1) 如果不多于1个数据,直接返回。
    (2) 一般选择序列最左边的值作为支点数据。
    (3) 将序列分成2部分,一部分都大于支点数据,另外一部分都小于支点数据
    (4) 对两边利用递归排序数列。快速排序比大部分排序算法都要快。尽管我们可以在某些特殊的情况下写出比快速排序快的算法,但是就通常情况而言,没有比它更快的了。快速排序是递归的,对于内存非常有限的机器来说,它不是一个好的选择。
  1. /// <summary>
  2.     /// 快速排序
  3.     /// </summary>
  4.     public class QuickSort : ISort
  5.     {
  6.         public int[] Sort(int[] array)
  7.         {
  8.             if (array != null)
  9.             {
  10.                 int[] temp = new int[array.Length];
  11.                 QuickSorting(array, 0, array.Length - 1);
  12.             }
  13.             return array;
  14.         }
  15.         private static void QuickSorting(int[] array, int l, int r)
  16.         {
  17.             if (l < r)
  18.             {
  19.                 int i = l;
  20.                 int j = r;
  21.                 int temp = array[i];
  22.                 while (i < j)
  23.                 {
  24.                     while (i < j && array[j] >= temp)
  25.                     {
  26.                         j--;
  27.                     }
  28.                     if (i < j)
  29.                     {
  30.                         array[i++] = array[j];
  31.                     }

  32.                     while (i < j && array[i] < temp)
  33.                     {
  34.                         i++;
  35.                     }
  36.                     if (i < j)
  37.                     {
  38.                         array[j--] = array[i];
  39.                     }
  40.                 }
  41.                 array[i] = temp;

  42.                 QuickSorting(array, l, i - 1);
  43.                 QuickSorting(array, i + 1, r);
  44.             }
  45.         }
  46.     }
复制代码
测试代码:
  1. class Program
  2.     {
  3.         public static Random re = new Random();
  4.         static void Main(string[] args)
  5.         {
  6.             Stopwatch stw4 = new Stopwatch();
  7.             Stopwatch stw5 = new Stopwatch();
  8.             Stopwatch stw6 = new Stopwatch();

  9.             int[] intArray4 = GetArray(int.MaxValue / 100000);
  10.             int[] intArray5 = GetArray(int.MaxValue / 100000);
  11.             int[] intArray6 = GetArray(int.MaxValue / 100000);

  12.             ISort sort4 = new HeapSort();//堆排序
  13.             stw4.Start();
  14.             int[] result4 = sort4.Sort(intArray4);
  15.             stw4.Stop();
  16.             Console.WriteLine("输出排序的结果(堆排序)");
  17.             Console.WriteLine("程序共运行时间:" + stw4.Elapsed.ToString());

  18.             ISort sort5 = new MergeSort();//归并排序
  19.             stw5.Start();
  20.             int[] result5 = sort5.Sort(intArray5);
  21.             stw5.Stop();
  22.             Console.WriteLine("输出排序的结果(归并排序)");
  23.             Console.WriteLine("程序共运行时间:" + stw5.Elapsed.ToString());

  24.             ISort sort6 = new QuickSort();//快速排序
  25.             stw6.Start();
  26.             int[] result6 = sort6.Sort(intArray6);
  27.             stw6.Stop();
  28.             Console.WriteLine("输出排序的结果(快速排序)");
  29.             Console.WriteLine("程序共运行时间:" + stw6.Elapsed.ToString());

  30.             Console.ReadKey();
  31.         }

  32.         private static int[] GetArray(long _arrayLength)
  33.         {
  34.             long arrayLength = _arrayLength;

  35.             //初始化数组
  36.             int[] intArray = new int[arrayLength];

  37.             for (int i = 0; i < arrayLength; i++)
  38.             {
  39.                 intArray[i] = re.Next();
  40.             }
  41.             return intArray;
  42.         }
  43.     }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马