程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> c# 排序算法測試程序入口 代碼

c# 排序算法測試程序入口 代碼

編輯:關於C#
 
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();
        }
    }
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved