排序算法测试程序入口

黄舟
Lepaskan: 2017-02-09 16:25:37
asal
1449 orang telah melayarinya

 排序算法测试程序入口

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();
        }
    }
}
Salin selepas log masuk

以上就是 排序算法测试程序入口的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!


sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan