【競賽】排序算法的最快實現

 【競賽】排序算法的最快實現

 

C# code
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static int MaxRange = 20080623;
        static void Main(string[] args)
        {
            Test(10*10000);
            Console.Read();
            Test(100*10000);
            Console.Read();
            Test(1000*10000);
            Console.Read();
        }
        public static void Test(int MAX)
        {         
            int[] nums = new int[MAX];
            Random r = new Random(MaxRange);
            for (int i = 0; i < MAX; i++)
            {
                nums[i] = r.Next(MAX);
            }
            long begin = System.DateTime.Now.Ticks;
            Sort(nums);
            long end = System.DateTime.Now.Ticks;
            Console.WriteLine("總共" + MAX/10000 + "萬數據,用時" + System.TimeSpan.FromTicks(end - begin).Milliseconds + "豪秒");         
        }

        public static void Sort(int[] a)
        {

            int[] p = new int[MaxRange + 1]; 

            for (int i = 0; i <a.Length; i++)
            {
              p[a[i]]++;
            }
            for (int i = 0, j = 0; i <= MaxRange; i++)
            {
                while (p[i] > 0)
                {
                    a[j++] = i;
                    p[i]--;
                }
            }
           
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章