一种快速排序算法

  1. using System;
  2. class Program
  3. {
  4.     public static void BinSort(int[] src)
  5.     {
  6.         int[] count = new int[256];
  7.         int[] temp = new int[src.Length];
  8.         for (int i = 0; i < 4; i++)
  9.         {
  10.             Array.Clear(count, 0, count.Length);
  11.             Array.Copy(src, temp, src.Length);
  12.             for (int k = 0; k < src.Length; k++)
  13.                 ++count[(temp[k] >> (i << 3)) & 0xFF];
  14.             int pos = 0;
  15.             for (int j = 0; j < 256; j++)
  16.             {
  17.                 int t = count[j];
  18.                 count[j] = pos;
  19.                 pos += t;
  20.             }
  21.             for (int l = 0; l < src.Length; l++)
  22.                 src[count[(temp[l] >> (i << 3)) & 0xFF]++] = temp[l];
  23.         }
  24.     }
  25.     static void Main(string[] args)
  26.     {
  27.         Random rand = new Random();
  28.         int[] a = new int[1024];
  29.         int[] b = new int[1024];
  30.         for (int i = 0; i < a.Length; i++)
  31.             a[i] = rand.Next(1024);
  32.         System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
  33.         watch.Start();
  34.         for (int i = 0; i < 10000; i++)
  35.         {
  36.             Array.Copy(a, b, a.Length);
  37.             Array.Sort(b);
  38.         }
  39.         watch.Stop();
  40.         Console.WriteLine("quick sort:" + watch.ElapsedMilliseconds);
  41.         watch.Reset();
  42.         watch.Start();
  43.         for (int i = 0; i < 10000; i++)
  44.         {
  45.             Array.Copy(a, b, a.Length);
  46.             BinSort(b);
  47.         }
  48.         Console.WriteLine("bin sort:" + watch.ElapsedMilliseconds);
  49.     }
  50. }

猜你喜欢

转载自blog.csdn.net/KAMILLE/article/details/3145441