static void Main(string[] args) { var quickNormal = new QuickSort(); var quickMedian = new QuickSortMedian3(); var arraySize = 200000; // 初始数组大小。 const int trialTimes = 4; // 每次实验的重复次数。 const int trialLevel = 5; // 双倍递增的次数。 Console.WriteLine("n\tmedian\tnormal\tratio"); for (var i = 0; i < trialLevel; i++) { double timeMedian = 0; double timeNormal = 0; for (var j = 0; j < trialTimes; j++) { var a = SortCompare.GetRandomArrayInt(arraySize); var b = new int[a.Length]; a.CopyTo(b, 0); timeNormal += SortCompare.Time(quickNormal, b); timeMedian += SortCompare.Time(quickMedian, a); } timeMedian /= trialTimes; timeNormal /= trialTimes; Console.WriteLine(arraySize + "\t" + timeMedian + "\t" + timeNormal + "\t" + timeMedian / timeNormal); arraySize *= 2; } }
static void Main(string[] args) { QuickSort quickNormal = new QuickSort(); QuickSortNonRecursive quickNonRecursive = new QuickSortNonRecursive(); int arraySize = 200000; // 初始数组大小。 const int trialTimes = 4; // 每次实验的重复次数。 const int trialLevel = 5; // 双倍递增的次数。 Console.WriteLine("n\tnon-recursive\tnormal\tratio"); for (int i = 0; i < trialLevel; i++) { double timeRecursive = 0; double timeNormal = 0; for (int j = 0; j < trialTimes; j++) { int[] a = SortCompare.GetRandomArrayInt(arraySize); int[] b = new int[a.Length]; a.CopyTo(b, 0); timeNormal += SortCompare.Time(quickNormal, b); timeRecursive += SortCompare.Time(quickNonRecursive, a); } timeRecursive /= trialTimes; timeNormal /= trialTimes; Console.WriteLine(arraySize + "\t" + timeRecursive + "\t\t" + timeNormal + "\t" + timeRecursive / timeNormal); arraySize *= 2; } }
static void Main(string[] args) { var n = 1024; var random = new Random(); double insertionPrev = 1; double selectionPrev = 1; while (n < 65538) { var testInsertion = new int[n]; var testSelection = new int[n]; for (var i = 0; i < n; i++) { testInsertion[i] = random.Next(2); testSelection[i] = testInsertion[i]; } Console.WriteLine("数组大小:" + n); Console.Write("Insertion Sort:"); var insertionNow = SortCompare.Time(new InsertionSort(), testInsertion); Console.WriteLine(insertionNow + "\tNow/Prev=" + insertionNow / insertionPrev); Console.Write("Selection Sort:"); var selectionNow = SortCompare.Time(new SelectionSort(), testSelection); Console.WriteLine(selectionNow + "\tNow/Prev=" + selectionNow / selectionPrev); Console.WriteLine(); insertionPrev = insertionNow; selectionPrev = selectionNow; n *= 2; } }
/// <summary> /// 第二个测试,测试结果按照 Insertion, Selection, Shell 排序。 /// </summary> /// <param name="n">测试的数组长度。</param> /// <returns>测试结果。</returns> static double[] TestB(int n) { var insertionSort = new InsertionSort(); var selectionSort = new SelectionSort(); var shellSort = new ShellSort(); var random = new Random(); // 每个元素的主键均为 double 类型,并含有 10 个 String 值(每个都至少长 10 个字符)。 var array = new Pair <double, string[]> [n]; var arrayBak = new Pair <double, string[]> [n]; for (var i = 0; i < n; i++) { var temp = new string[10]; for (var j = 0; j < 10; j++) { temp[j] = RandomString(12, random); } array[i] = new Pair <double, string[]>(random.NextDouble(), temp); } array.CopyTo(arrayBak, 0); var results = new double[3]; results[0] = SortCompare.Time(insertionSort, array); arrayBak.CopyTo(array, 0); results[1] = SortCompare.Time(selectionSort, array); arrayBak.CopyTo(array, 0); results[2] = SortCompare.Time(shellSort, array); return(results); }
/// <summary> /// 第一个测试,测试结果按照 Insertion, Selection, Shell 排序。 /// </summary> /// <param name="n">测试的数组长度。</param> /// <returns>测试结果。</returns> static double[] TestA(int n) { var insertionSort = new InsertionSort(); var selectionSort = new SelectionSort(); var shellSort = new ShellSort(); var random = new Random(); // 每个元素的主键均为 String 类型(至少长 10 个字符),并含有一个 double 值。 var array = new Pair <string, double> [n]; var arrayBak = new Pair <string, double> [n]; for (var i = 0; i < n; i++) { array[i] = new Pair <string, double>(RandomString(20, random), random.NextDouble()); } array.CopyTo(arrayBak, 0); var results = new double[3]; results[0] = SortCompare.Time(insertionSort, array); arrayBak.CopyTo(array, 0); results[1] = SortCompare.Time(selectionSort, array); arrayBak.CopyTo(array, 0); results[2] = SortCompare.Time(shellSort, array); return(results); }
static void Main(string[] args) { QuickSort quickNormal = new QuickSort(); QuickSortMedian3 quickMedian3 = new QuickSortMedian3(); QuickSortMedian5 quickMedian5 = new QuickSortMedian5(); int arraySize = 200000; // 初始数组大小。 const int trialTimes = 4; // 每次实验的重复次数。 const int trialLevel = 6; // 双倍递增的次数。 Console.WriteLine("n\tmedian5\tmedian3\tnormal\tmedian5/normal\t\tmedian5/median3"); for (int i = 0; i < trialLevel; i++) { double timeMedian3 = 0; double timeMedian5 = 0; double timeNormal = 0; for (int j = 0; j < trialTimes; j++) { int[] a = SortCompare.GetRandomArrayInt(arraySize); int[] b = new int[a.Length]; int[] c = new int[a.Length]; a.CopyTo(b, 0); a.CopyTo(c, 0); timeNormal += SortCompare.Time(quickNormal, a); timeMedian3 += SortCompare.Time(quickMedian3, b); timeMedian5 += SortCompare.Time(quickMedian5, c); } timeMedian5 /= trialTimes; timeMedian3 /= trialTimes; timeNormal /= trialTimes; Console.WriteLine(arraySize + "\t" + timeMedian5 + "\t" + timeMedian3 + "\t" + timeNormal + "\t" + timeMedian5 / timeNormal + "\t" + timeMedian5 / timeMedian3); arraySize *= 2; } }
static void Main(string[] args) { QuickSort quickNormal = new QuickSort(); QuickBentleyMcIlroy quickBentleyMcIlroy = new QuickBentleyMcIlroy(); int arraySize = 800000; // 初始数组大小。 const int trialTimes = 1; // 每次实验的重复次数。 const int trialLevel = 8; // 双倍递增的次数。 Console.WriteLine("n\t\t3way\tnormal\tratio"); for (int i = 0; i < trialLevel; i++) { double timeBentleyMcIlroy = 0; double timeNormal = 0; for (int j = 0; j < trialTimes; j++) { int[] a = SortCompare.GetRandomArrayInt(arraySize); int[] b = new int[a.Length]; a.CopyTo(b, 0); timeNormal += SortCompare.Time(quickNormal, b); timeBentleyMcIlroy += SortCompare.Time(quickBentleyMcIlroy, a); } timeBentleyMcIlroy /= trialTimes; timeNormal /= trialTimes; if (arraySize < 10000000) { Console.WriteLine(arraySize + "\t\t" + timeBentleyMcIlroy + "\t" + timeNormal + "\t" + timeBentleyMcIlroy / timeNormal); } else { Console.WriteLine(arraySize + "\t" + timeBentleyMcIlroy + "\t" + timeNormal + "\t" + timeBentleyMcIlroy / timeNormal); } arraySize *= 2; } }
/// <summary> /// 第三个测试,测试结果按照 Insertion, Selection, Shell 排序。 /// </summary> /// <param name="n">测试的数组长度。</param> /// <returns>测试结果。</returns> static double[] TestC(int n) { var insertionSort = new InsertionSort(); var selectionSort = new SelectionSort(); var shellSort = new ShellSort(); var random = new Random(); // 每个元素的主键均为 int 类型,并含有一个 int[20] 值。 var array = new Pair <int, int[]> [n]; var arrayBak = new Pair <int, int[]> [n]; for (var i = 0; i < n; i++) { var temp = new int[20]; for (var j = 0; j < 20; j++) { temp[j] = random.Next(); } array[i] = new Pair <int, int[]>(random.Next(), temp); } array.CopyTo(arrayBak, 0); var results = new double[3]; results[0] = SortCompare.Time(insertionSort, array); arrayBak.CopyTo(array, 0); results[1] = SortCompare.Time(selectionSort, array); arrayBak.CopyTo(array, 0); results[2] = SortCompare.Time(shellSort, array); return(results); }
/// <summary> /// 进行一次测试。 /// </summary> /// <param name="m">要使用的阈值</param> static void Trial(int m) { QuickSortInsertion withShuffle = new QuickSortInsertion(); QuickSortRandomPivot randomPivot = new QuickSortRandomPivot(); int trialTime = 5; // M=10 withShuffle.M = m; randomPivot.M = m; double timeShuffle = 0; double timeRandomPivot = 0; for (int N = 1000; N < 10000000; N *= 10) { for (int i = 0; i < trialTime; i++) { int[] a = new int[N]; int[] b = new int[N]; for (int j = 0; j < N; j++) { a[j] = j; } Shuffle(a); a.CopyTo(b, 0); timeShuffle += SortCompare.Time(withShuffle, a); timeRandomPivot += SortCompare.Time(randomPivot, b); } timeShuffle /= trialTime; timeRandomPivot /= trialTime; Console.WriteLine(withShuffle.M + "\t" + N + "\t" + timeShuffle + "\t" + timeRandomPivot + "\t" + timeShuffle / timeRandomPivot); } }
static void Main(string[] args) { var sort = new MergeSortKWay(); var sort2Way = new MergeSort(); var arraySize = 1000000; var trialTime = 10; var data = new int[arraySize][]; for (var i = 0; i < trialTime; i++) { data[i] = SortCompare.GetRandomArrayInt(arraySize); } double totalTime = 0; for (var j = 0; j < trialTime; j++) { var rawData = new int[data.Length]; data[j].CopyTo(rawData, 0); totalTime += SortCompare.Time(sort, rawData); } for (var k = 2; k < 100000; k *= 2) { Console.Write("k=" + k + "\t"); totalTime = 0; for (var j = 0; j < trialTime; j++) { var rawData = new int[data.Length]; data[j].CopyTo(rawData, 0); totalTime += SortCompare.Time(sort, rawData); } Console.WriteLine("平均耗时:" + totalTime / trialTime + "ms"); } }
/// <summary> /// 构造只有一个元素的数组并用其对指定排序算法做测试。 /// </summary> /// <param name="sort">需要做测试的排序算法。</param> /// <returns>排序算法的耗时。</returns> static double OneArraySizeSort(BaseSort sort) { int[] array = new int[1]; Random random = new Random(); array[0] = random.Next(); return(SortCompare.Time(sort, array)); }
static void Main(string[] args) { int[] a = DataManager.GetUnsortedData();// 获得 32 K 数据 int[] b = DataManager.GetUnsortedData(); // 耗时 12354 毫秒(@Surface Pro 3 i7 512G) Console.WriteLine(SortCompare.Time(new InsertionSort(), a)); // 耗时 15034 毫秒(@Surface Pro 3 i7 512G) Console.WriteLine(SortCompare.Time(new Sort.InsertionSort(), b)); }
static void Main(string[] args) { int N = 1000; InsertionSort insertion = new InsertionSort(); SelectionSort selection = new SelectionSort(); ShellSort shell = new ShellSort(); double prevInsertion = 0; double prevSelection = 0; double prevShell = 0; for (int i = 0; i < 10; i++) { Console.WriteLine("N:" + N); int[] array = SortCompare.GetRandomArrayInt(N); int[] arrayBak = new int[N]; array.CopyTo(arrayBak, 0); Console.WriteLine("\tInsertion Sort"); double now = SortCompare.Time(insertion, array); Console.WriteLine("\t\tActual Time(ms):" + now); if (i != 0) { Console.WriteLine("\t\tEstimate Time(ms):" + prevInsertion * 4); Console.WriteLine("\t\tRatio:" + now / prevInsertion); } prevInsertion = now; arrayBak.CopyTo(array, 0); Console.WriteLine("\tSelection Sort"); now = SortCompare.Time(selection, array); Console.WriteLine("\t\tActual Time(ms):" + now); if (i != 0) { Console.WriteLine("\t\tEstimate Time(ms):" + prevSelection * 4); Console.WriteLine("\t\tRatio:" + now / prevSelection); } prevSelection = now; arrayBak.CopyTo(array, 0); Console.WriteLine("\tShell Sort"); now = SortCompare.Time(shell, array); Console.WriteLine("\t\tActual Time(ms):" + now); if (i != 0) { Console.WriteLine("\t\tEstimate Time(ms):" + prevShell * 2); Console.WriteLine("\t\tRatio:" + now / prevShell); } prevShell = now; N *= 2; } }
/// <summary> /// 构造逆序数组并用其对指定输入算法进行测试。 /// </summary> /// <param name="sort">需要做测试的算法。</param> /// <returns>算法耗时。</returns> static double ReverseSortTest(BaseSort sort) { int[] array = new int[10000]; for (int i = 0; i < array.Length; i++) { array[i] = array.Length - i; } return(SortCompare.Time(sort, array)); }
/// <summary> /// 执行一次测试并绘制图像。 /// </summary> public void Test() { var random = new Random(); var array = SortCompare.GetRandomArrayDouble(_n); var time = SortCompare.Time(_sort, array); _resultList.Add(time); _resultYList.Add((float)(random.NextDouble() * _drawRect.Height)); DrawPanel(_resultList.ToArray(), _resultYList.ToArray()); }
/// <summary> /// 执行一次测试并绘制图像。 /// </summary> public void Test() { Random random = new Random(); double[] array = SortCompare.GetRandomArrayDouble(this.n); double time = SortCompare.Time(this.sort, array); this.resultList.Add(time); this.resultYList.Add((float)(random.NextDouble() * this.drawRect.Height)); DrawPanel(this.resultList.ToArray(), this.resultYList.ToArray()); }
static void Main(string[] args) { var auxInSort = new AuxInSortMergeSort(); var auxInMerge = new AuxInMergeMergeSort(); var data1 = SortCompare.GetRandomArrayInt(100000); var data2 = new int[data1.Length]; data1.CopyTo(data2, 0); Console.WriteLine("在Sort中创建aux[]\t" + SortCompare.Time(auxInSort, data1) + "ms"); Console.WriteLine("在Merge中创建aux[]\t" + SortCompare.Time(auxInMerge, data2) + "ms"); }
// 选择排序的耗时与输入值的内容无关,不受影响。 // 对于插入排序,以上几种情况都是重复值较多的情况,插入排序的速度会加快。 // 希尔排序本质上也是插入排序,因此也会更快一些。 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)); }
/// <summary> /// 构造只有两种取值的数组并用其对指定排序算法做测试。 /// </summary> /// <param name="sort">需要做测试的排序算法。</param> /// <returns>排序算法的耗时。</returns> static double BinarySortTest(BaseSort sort) { int[] array = new int[10000]; Random random = new Random(); for (int i = 0; i < array.Length; i++) { array[i] = random.Next(2); } return(SortCompare.Time(sort, array)); }
static void Main(string[] args) { var mergeSort = new MergeSort(); var mergeSortX = new MergeSortX(); var mergeSortUnstable = new MergeSortUnstable(); var n = 1000000; var cutoff = 2; var trialTime = 4; Console.WriteLine("归并排序改进前与改进后的比较:"); Console.WriteLine("数组\t耗时1\t耗时2\t阈值\t比率"); for (var i = 0; i < 20; i++) { double mergeSortTime = 0; double mergeSortXTime = 0; mergeSortX.SetCutOff(cutoff); for (var j = 0; j < trialTime; j++) { var a = SortCompare.GetRandomArrayInt(n); var b = new int[a.Length]; a.CopyTo(b, 0); mergeSortTime += SortCompare.Time(mergeSort, a); mergeSortXTime += SortCompare.Time(mergeSortX, b); } mergeSortTime /= trialTime; mergeSortXTime /= trialTime; Console.WriteLine(n + "\t" + mergeSortTime + "\t" + mergeSortXTime + "\t" + cutoff + "\t" + mergeSortTime / mergeSortXTime); cutoff++; } n = 100000; Console.WriteLine("稳定归并排序与不稳定版本的比较:"); Console.WriteLine("数组\t耗时1\t耗时2\t比率"); for (var i = 0; i < 6; i++) { double mergeSortTime = 0; double mergeSortUnstableTime = 0; for (var j = 0; j < trialTime; j++) { var a = SortCompare.GetRandomArrayInt(n); var b = new int[a.Length]; a.CopyTo(b, 0); mergeSortTime += SortCompare.Time(mergeSort, a); mergeSortUnstableTime += SortCompare.Time(mergeSortUnstable, b); } mergeSortTime /= trialTime; mergeSortUnstableTime /= trialTime; Console.WriteLine(n + "\t" + mergeSortTime + "\t" + mergeSortUnstableTime + "\t" + mergeSortTime / mergeSortUnstableTime); n *= 2; } }
/// <summary> /// 构造只有一个值的数组并用其对指定排序算法做测试。 /// </summary> /// <param name="sort">需要做测试的排序算法。</param> /// <returns>算法的耗时。</returns> static double EqualSortTest(BaseSort sort) { var array = new int[10000]; var random = new Random(); var num = random.Next(); for (var i = 0; i < array.Length; i++) { array[i] = num; } return(SortCompare.Time(sort, array)); }
static void Main(string[] args) { InsertionSort insertionSort = new InsertionSort(); SelectionSort selectionSort = new SelectionSort(); ShellSort shellSort = new ShellSort(); int n = 10000; // 高斯分布(正态分布) double[] arrayInsertion = SortCompare.GetNormalDistributionArray(n); double[] arraySelection = new double[n]; double[] arrayShell = new double[n]; arrayInsertion.CopyTo(arraySelection, 0); arrayInsertion.CopyTo(arrayShell, 0); Console.WriteLine("Normal Distribution:"); Console.WriteLine("Insertion: " + SortCompare.Time(insertionSort, arrayInsertion)); Console.WriteLine("Selection: " + SortCompare.Time(selectionSort, arraySelection)); Console.WriteLine("Shell: " + SortCompare.Time(shellSort, arrayShell)); // 泊松分布 arrayInsertion = SortCompare.GetPossionDistributionArray(n); arrayInsertion.CopyTo(arraySelection, 0); arrayInsertion.CopyTo(arrayShell, 0); Console.WriteLine("Poission Distribution:"); Console.WriteLine("Insertion: " + SortCompare.Time(insertionSort, arrayInsertion)); Console.WriteLine("Selection: " + SortCompare.Time(selectionSort, arraySelection)); Console.WriteLine("Shell: " + SortCompare.Time(shellSort, arrayShell)); // 几何分布 arrayInsertion = SortCompare.GetGeometricDistributionArray(n, 0.3); arrayInsertion.CopyTo(arraySelection, 0); arrayInsertion.CopyTo(arrayShell, 0); Console.WriteLine("Geometric Distribution:"); Console.WriteLine("Insertion: " + SortCompare.Time(insertionSort, arrayInsertion)); Console.WriteLine("Selection: " + SortCompare.Time(selectionSort, arraySelection)); Console.WriteLine("Shell: " + SortCompare.Time(shellSort, arrayShell)); // 离散分布 arrayInsertion = SortCompare.GetDiscretDistributionArray(n, new double[] { 0.1, 0.2, 0.3, 0.1, 0.1, 0.1, 0.1 }); arrayInsertion.CopyTo(arraySelection, 0); arrayInsertion.CopyTo(arrayShell, 0); Console.WriteLine("Discret Distribution:"); Console.WriteLine("Insertion: " + SortCompare.Time(insertionSort, arrayInsertion)); Console.WriteLine("Selection: " + SortCompare.Time(selectionSort, arraySelection)); Console.WriteLine("Shell: " + SortCompare.Time(shellSort, arrayShell)); }
/// <summary> /// 后台测试方法。 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; QuickSortInsertion quickSortInsertion = new QuickSortInsertion(); double[] timeRecord = new double[31]; for (int i = 0; i <= 30; i++) { worker.ReportProgress(i * 3); quickSortInsertion.M = i; int[] data = SortCompare.GetRandomArrayInt(this.N); timeRecord[i] = SortCompare.Time(quickSortInsertion, data); } e.Result = timeRecord; }
/// <summary> /// 后台测试方法。 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { var worker = sender as BackgroundWorker; var quickSortInsertion = new QuickSortInsertion(); var timeRecord = new double[31]; for (var i = 0; i <= 30; i++) { Debug.Assert(worker != null, nameof(worker) + " != null"); worker.ReportProgress(i * 3); quickSortInsertion.M = i; var data = SortCompare.GetRandomArrayInt(N); timeRecord[i] = SortCompare.Time(quickSortInsertion, data); } e.Result = timeRecord; }
static void Main(string[] args) { var quick = new QuickSort(); var quickSortX = new QuickSortX(); var arrayLength = 1000000; var a = SortCompare.GetRandomArrayInt(arrayLength); var b = new int[arrayLength]; a.CopyTo(b, 0); var time1 = SortCompare.Time(quick, a); var time2 = SortCompare.Time(quickSortX, b); Console.WriteLine("QSort\tQSort with Sentinels\t"); Console.WriteLine(time1 + "\t" + time2 + "\t"); }
// 选择排序的性能只与数组大小有关,以上三种情况耗时都是近似的。 // 插入排序的性能与逆序对数量有关,部分有序的情况下耗时会小于完全随机。 // 希尔排序与插入排序类似。 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) { var mergeSort = new MergeSortNatural(); Console.WriteLine("总长度\t有序\t时间\t比率"); var maxSorted = 256; var repeatTime = 4; double previousTime = 1; for (var i = 0; i < 4; i++) { var n = 16384; for (var j = 0; j < 6; j++) { double time = 0; for (var k = 0; k < repeatTime; k++) { var test = new int[n]; var unsorted = SortCompare.GetRandomArrayInt(n - maxSorted); var sorted = SortCompare.GetRandomArrayInt(maxSorted); Array.Sort(sorted); for (var l = 0; l < n - maxSorted; l++) { test[l] = unsorted[l]; } for (var l = 0; l < maxSorted; l++) { test[l + n - maxSorted] = sorted[l]; } time += SortCompare.Time(mergeSort, test); } if (j == 0) { Console.WriteLine(n + "\t" + maxSorted + "\t" + time / repeatTime + "\t---"); } else { Console.WriteLine(n + "\t" + maxSorted + "\t" + time / repeatTime + "\t" + (time / repeatTime) / previousTime); } previousTime = time / repeatTime; n *= 2; } maxSorted *= 4; } }
/// <summary> /// 后台测试方法。 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { var worker = sender as BackgroundWorker; var quick = new QuickSort(); var percentPerTrial = 100.0 / T; var totalTime = new double[T]; for (var i = 0; i < T; i++) { var data = SortCompare.GetRandomArrayDouble(N); totalTime[i] = SortCompare.Time(quick, data); worker.ReportProgress((int)(percentPerTrial * i)); } e.Result = totalTime; }
/// <summary> /// 后台测试方法。 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; QuickSort quick = new QuickSort(); double percentPerTrial = 100.0 / this.T; double[] totalTime = new double[this.T]; for (int i = 0; i < this.T; i++) { double[] data = SortCompare.GetRandomArrayDouble(this.N); totalTime[i] = SortCompare.Time(quick, data); worker.ReportProgress((int)(percentPerTrial * i)); } e.Result = totalTime; }
static void Main(string[] args) { int n = 128; Random random = new Random(); double shellPrev = 1; double insertionPrev = 1; double selectionPrev = 1; while (n < 65538) { int[] testShell = new int[n]; int[] testInsertion = new int[n]; int[] testSelection = new int[n]; for (int i = 0; i < n; i++) { testShell[i] = random.Next(); testInsertion[i] = testShell[i]; testSelection[i] = testShell[i]; } Console.WriteLine("数组大小:" + n); Console.Write("Shell Sort:"); double shellNow = SortCompare.Time(new ShellSort(), testShell); Console.WriteLine(shellNow + "\t\tNow/Prev=" + shellNow / shellPrev); Console.Write("Insertion Sort:"); double insertionNow = SortCompare.Time(new InsertionSort(), testInsertion); Console.WriteLine(insertionNow + "\tNow/Prev=" + insertionNow / insertionPrev); Console.Write("Selection Sort:"); double selectionNow = SortCompare.Time(new SelectionSort(), testSelection); Console.WriteLine(selectionNow + "\tNow/Prev=" + selectionNow / selectionPrev); Console.WriteLine(); shellPrev = shellNow; insertionPrev = insertionNow; selectionPrev = selectionNow; n *= 2; } }