排序演算法測試程式入口

來源:互聯網
上載者:User
排序演算法測試程式入口

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Collections;using System.Diagnostics;namespace Sort{    class Program    {        static void Main(string[] args)        {            //Test(5);            EfficiencyTest(100000,10,"BubbleSorter");                  }        //<產生隨機數GenerateRandomNumber>        public static List<int> GenerateRandomNumber(int Length)        {            List<int> newRandom = new List<int>();            Random rd = new Random();            for (int i = 0; i < Length; i++)            {                newRandom.Add(rd.Next());            }            return newRandom;        }        //測試排序演算法的正確性        private static void Test(int n)        {            int[] de = GenerateRandomNumber(n).ToArray();            Console.WriteLine("排序前:");            foreach (int dee in de)            {                Console.WriteLine(dee);            }            //int[] d = MergeSorter.Sort(de);            //int[] d=HeapSorter.Sort(de);            //int[] d = ShellSorter.Sort(de);            //int[] d = InsertSorter.Sort(de);            //int[] d = SelectSorter.Sort(de);            //int[] d = CockTailSorter.Sort(de);            int[] d = BubbleSorter.Sort(de);            Console.WriteLine("排序後:");            foreach (int dee in d)            {                Console.WriteLine(dee);            }            //Console.ReadKey();        }        //測試各個排序演算法效率//調用Name函數排序i個數j次        private static void EfficiencyTest(int i,int j,string Name)        {            double AverageTime = 0; ;            string Cname = null;            for (int n = 0; n < j;n++)            {                              int[] de = GenerateRandomNumber(i).ToArray();                Stopwatch stopwatch = new Stopwatch();                stopwatch.Start();                DateTime dateStart = DateTime.Now;                switch(Name)                {                    case "MergeSorter":                        MergeSorter.Sort(de);                        Cname = "MergeSorter";                        break;                    case "HeapSorter":                        HeapSorter.Sort(de);                        Cname = "HeapSorter";                        break;                    case "ShellSorter":                        ShellSorter.Sort(de);                        Cname = "ShellSorter";                        break;                    case "InsertSorter":                        InsertSorter.Sort(de);                        Cname = "InsertSorter";                        break;                    case "SelectSorter":                        SelectSorter.Sort(de);                        Cname = "SelectSorter";                        break;                    case "CockTailSorter":                        CockTailSorter.Sort(de);                        Cname = "CockTailSorter";                        break;                    case "BubbleSorter":                        BubbleSorter.Sort(de);                        Cname = "BubbleSorter";                        break;                    case "QuickSorter":                        QuickSorter.Sort(de);                        Cname = "QuickSorter";                        break;                }                stopwatch.Stop();                AverageTime = (DateTime.Now - dateStart).TotalMilliseconds;                    }            Double span = AverageTime / j;            Console.WriteLine(Cname + "排序" + i + "個數" + j + "次所用平均時間為:" + span + " 毫秒");            Console.ReadKey();        }    }}

以上就是 排序演算法測試程式入口的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.