一种快速排序算法

  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. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值