- using System;
- class Program
- {
- public static void BinSort(int[] src)
- {
- int[] count = new int[256];
- int[] temp = new int[src.Length];
- for (int i = 0; i < 4; i++)
- {
- Array.Clear(count, 0, count.Length);
- Array.Copy(src, temp, src.Length);
- for (int k = 0; k < src.Length; k++)
- ++count[(temp[k] >> (i << 3)) & 0xFF];
- int pos = 0;
- for (int j = 0; j < 256; j++)
- {
- int t = count[j];
- count[j] = pos;
- pos += t;
- }
- for (int l = 0; l < src.Length; l++)
- src[count[(temp[l] >> (i << 3)) & 0xFF]++] = temp[l];
- }
- }
- static void Main(string[] args)
- {
- Random rand = new Random();
- int[] a = new int[1024];
- int[] b = new int[1024];
- for (int i = 0; i < a.Length; i++)
- a[i] = rand.Next(1024);
- System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
- watch.Start();
- for (int i = 0; i < 10000; i++)
- {
- Array.Copy(a, b, a.Length);
- Array.Sort(b);
- }
- watch.Stop();
- Console.WriteLine("quick sort:" + watch.ElapsedMilliseconds);
- watch.Reset();
- watch.Start();
- for (int i = 0; i < 10000; i++)
- {
- Array.Copy(a, b, a.Length);
- BinSort(b);
- }
- Console.WriteLine("bin sort:" + watch.ElapsedMilliseconds);
- }
- }
一种快速排序算法
猜你喜欢
转载自blog.csdn.net/KAMILLE/article/details/3145441
今日推荐
周排行