예제 #1
0
        public static void TestSortMethod(SortMethodTest sortmethod)
        {
            //分别对长度为10,100,1000,10000,100000的数字进行排序,得到运行时间,每个长度运行10次取均值
            Random rn = new Random();

            int[] lengths = new int[] { 10, 100, 1000, 10000, 100000, 1000000 };
            int   times   = 10;

            foreach (var len in lengths)
            {
                DateTime dt1 = DateTime.Now;
                for (int i = 0; i < times; i++)
                {
                    int[] array = new int[len];
                    for (int j = 0; j < len; j++)
                    {
                        array[j] = rn.Next(0, len * 10);
                    }
                    sortmethod(ref array);
                }
                Console.WriteLine("method:{0},len:{1},costtime/len:{2}ms", sortmethod.GetType().Name, len, (DateTime.Now - dt1).TotalMilliseconds / times / len);
            }
        }
예제 #2
0
        public static void TestSortMethodIsRight(SortMethodTest sortmethod)
        {
            //分别对长度为10,100,1000,10000,100000的数字进行排序,得到运行时间,每个长度运行10次取均值
            Random rn  = new Random();
            int    len = 20;

            int[] array = new int[len];
            for (int j = 0; j < len; j++)
            {
                array[j] = rn.Next(0, len * 10);
            }
            sortmethod(ref array);
            bool isright = true;

            for (int i = 1; i < len; i++)
            {
                if (array[i] < array[i - 1])
                {
                    isright = false;
                    break;
                }
            }
            Console.WriteLine(isright);
        }