static void Main(string[] args) { // 耗时 1019ms(@Surface Pro 3 i7 512G) Console.WriteLine(SortCompare.TimeRandomInput(new InsertionSort(), 10000, 3) / 3.0); // 耗时 925ms(@Surface Pro 3 i7 512G) Console.WriteLine(SortCompare.TimeRandomInput(new Sort.InsertionSort(), 10000, 3) / 3.0); }
public void Test() { Debug.Log(Time.time); float t1 = SortCompare.TimeRandomInput("Selection", 10, 20); float t2 = SortCompare.TimeRandomInput("Insertion", 10, 20); Debug.Log(Time.time); }
/// <summary> /// 执行八次耗时测试,每次数据量翻倍。 /// </summary> /// <param name="n">初始数据量。</param> /// <returns>测试结果数据。</returns> public double[] Test(int n) { double[] result = new double[8]; for (int i = 0; i < result.Length; i++) { result[i] = SortCompare.TimeRandomInput(this.sort, n, 3); n *= 2; } return(result); }
// 选择排序的耗时与输入值的内容无关,不受影响。 // 对于插入排序,以上几种情况都是重复值较多的情况,插入排序的速度会加快。 // 希尔排序本质上也是插入排序,因此也会更快一些。 static void Main(string[] args) { var n = 10000; var insertionSort = new InsertionSort(); var selectionSort = new SelectionSort(); var shellSort = new ShellSort(); var arrayInsertion = new int[n]; var arraySelection = new int[n]; var arrayShell = new int[n]; // 对照,完全随机 arrayInsertion = HalfZeroHalfOne(n); arrayInsertion.CopyTo(arraySelection, 0); arrayInsertion.CopyTo(arrayShell, 0); Console.WriteLine("totally random"); Console.WriteLine("Insertion Sort:" + SortCompare.TimeRandomInput(insertionSort, n, 1)); Console.WriteLine("Selection Sort:" + SortCompare.TimeRandomInput(selectionSort, n, 1)); Console.WriteLine("Shell Sort:" + SortCompare.TimeRandomInput(shellSort, n, 1)); Console.WriteLine(); // 一半是 0 一半是 1 arrayInsertion = HalfZeroHalfOne(n); arrayInsertion.CopyTo(arraySelection, 0); arrayInsertion.CopyTo(arrayShell, 0); Console.WriteLine("half 0 and half 1"); Console.WriteLine("Insertion Sort:" + SortCompare.Time(insertionSort, arrayInsertion)); Console.WriteLine("Selection Sort:" + SortCompare.Time(selectionSort, arraySelection)); Console.WriteLine("Shell Sort:" + SortCompare.Time(shellSort, arrayShell)); Console.WriteLine(); // 一半是 0, 1/4 是 1, 1/8 是 2…… arrayInsertion = HalfAndHalf(n); arrayInsertion.CopyTo(arraySelection, 0); arrayShell.CopyTo(arrayShell, 0); Console.WriteLine("half and half and half ..."); Console.WriteLine("Insertion Sort:" + SortCompare.Time(insertionSort, arrayInsertion)); Console.WriteLine("Selection Sort:" + SortCompare.Time(selectionSort, arraySelection)); Console.WriteLine("Shell Sort:" + SortCompare.Time(shellSort, arrayShell)); Console.WriteLine(); // 一半是 0,一半是随机 int 值 arrayInsertion = HalfZeroHalfRandom(n); arrayInsertion.CopyTo(arraySelection, 0); arrayShell.CopyTo(arrayShell, 0); Console.WriteLine("half 0 half random"); Console.WriteLine("Insertion Sort:" + SortCompare.Time(insertionSort, arrayInsertion)); Console.WriteLine("Selection Sort:" + SortCompare.Time(selectionSort, arraySelection)); Console.WriteLine("Shell Sort:" + SortCompare.Time(shellSort, arrayShell)); }
// 选择排序的性能只与数组大小有关,以上三种情况耗时都是近似的。 // 插入排序的性能与逆序对数量有关,部分有序的情况下耗时会小于完全随机。 // 希尔排序与插入排序类似。 static void Main(string[] args) { InsertionSort insertionSort = new InsertionSort(); SelectionSort selectionSort = new SelectionSort(); ShellSort shellSort = new ShellSort(); int n = 10000; int[] selectionArray = new int[n]; int[] insertionArray = new int[n]; int[] shellArray = new int[n]; // 完全随机的对照 Console.WriteLine("totally random"); Console.WriteLine("Selection Sort:" + SortCompare.TimeRandomInput(selectionSort, n, 1)); Console.WriteLine("Insertion Sort:" + SortCompare.TimeRandomInput(insertionSort, n, 1)); Console.WriteLine("Shell Sort:" + SortCompare.TimeRandomInput(shellSort, n, 1)); // 95% 有序,其余部分为随机值。 selectionArray = Sorted95Random5(n); selectionArray.CopyTo(insertionArray, 0); selectionArray.CopyTo(shellArray, 0); Console.WriteLine("95% sorted + 5% random"); Console.WriteLine("Selection Sort:" + SortCompare.Time(selectionSort, selectionArray)); Console.WriteLine("Insertion Sort:" + SortCompare.Time(insertionSort, insertionArray)); Console.WriteLine("Shell Sort:" + SortCompare.Time(shellSort, shellArray)); // 所有元素和它们的正确位置的距离都不超过 10。 selectionArray = RandomIn10(n); selectionArray.CopyTo(insertionArray, 0); selectionArray.CopyTo(shellArray, 0); Console.WriteLine("a sorted array that left shift 6 times"); Console.WriteLine("Selection Sort:" + SortCompare.Time(selectionSort, selectionArray)); Console.WriteLine("Insertion Sort:" + SortCompare.Time(insertionSort, insertionArray)); Console.WriteLine("Shell Sort:" + SortCompare.Time(shellSort, shellArray)); // 5% 的元素随机分布在整个数组中,剩下的数据都是有序的。 selectionArray = RandomIn10(n); selectionArray.CopyTo(insertionArray, 0); selectionArray.CopyTo(shellArray, 0); Console.WriteLine("95% elements is sorted while 5% elements are placed randomly"); Console.WriteLine("Selection Sort:" + SortCompare.Time(selectionSort, selectionArray)); Console.WriteLine("Insertion Sort:" + SortCompare.Time(insertionSort, insertionArray)); Console.WriteLine("Shell Sort:" + SortCompare.Time(shellSort, shellArray)); }
static void Main(string[] args) { MergeSortThreeWay mergeSortThreeWay = new MergeSortThreeWay(); int n = 131072; int trialTime = 5; double previousTime = 1; Console.WriteLine("数组\t耗时\t比率"); for (int i = 0; i < 6; i++) { double time = SortCompare.TimeRandomInput(mergeSortThreeWay, n, trialTime); time /= trialTime; if (i == 0) { Console.WriteLine(n + "\t" + time + "\t----"); } else { Console.WriteLine(n + "\t" + time + "\t" + time / previousTime); } previousTime = time; n *= 2; } }