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; } }
private void _Init(SortCompare <T> comparer, int startCapacity) { Comparer = comparer; Capacity = startCapacity; innerArray = new T[startCapacity]; Offset = 0; }
/// <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[] 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> /// 第一个测试,测试结果按照 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) { Console.WriteLine("N\t准确值\t估计值\t比值"); QuickSortAnalyze sort = new QuickSortAnalyze(); int N = 100; int trialTime = 500; for (int i = 0; i < 3; i++) { int sumOfCompare = 0; int[] a = new int[N]; for (int j = 0; j < trialTime; j++) { for (int k = 0; k < N; k++) { a[k] = k; } SortCompare.Shuffle(a); sort.Sort(a); sumOfCompare += sort.CompareCount; } int averageCompare = sumOfCompare / trialTime; double estimatedCompare = 2 * N * Math.Log(N); Console.WriteLine(N + "\t" + averageCompare + "\t" + (int)estimatedCompare + "\t" + averageCompare / estimatedCompare); N *= 10; } }
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; } }
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); }
/// <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) { 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 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) { 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"); } }
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; } }
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); }
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)); }
/// <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 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="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); }
/// <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()); }
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"); }
/// <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 merge = new MergeSort(); var data = SortCompare.GetRandomArrayInt(200); merge.Sort(data); for (var i = 0; i < data.Length; i++) { Console.Write(data[i] + " "); } }
// 选择排序的耗时与输入值的内容无关,不受影响。 // 对于插入排序,以上几种情况都是重复值较多的情况,插入排序的速度会加快。 // 希尔排序本质上也是插入排序,因此也会更快一些。 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="sender"></param> /// <param name="e"></param> private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; QuickSortInsertion quickSortInsertion = new QuickSortInsertion { M = this.M }; int[] data = SortCompare.GetRandomArrayInt(this.N); worker.ReportProgress(50); quickSortInsertion.Sort(data); e.Result = quickSortInsertion.Counts; }
/// <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)); }
/// <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 { M = _m }; var data = SortCompare.GetRandomArrayInt(_n); worker?.ReportProgress(50); quickSortInsertion.Sort(data); e.Result = quickSortInsertion.Counts; }
// t = 2, 3, 4 // t 大于 10 之后,由于每次排序 h 缩减的太快, // 时间会越来越近似于直接插入排序。 static void Main(string[] args) { var array = SortCompare.GetRandomArrayInt(1000000); var array2 = new int[array.Length]; array.CopyTo(array2, 0); var timer = new Stopwatch(); var bestTimes = new long[3]; var bestTs = new long[3]; for (var i = 0; i < bestTimes.Length; i++) { bestTimes[i] = long.MaxValue; bestTs[i] = int.MaxValue; } long nowTime = 0; var shellSort = new ShellSort(); for (var t = 2; t <= 1000000; t++) { Console.WriteLine(t); timer.Restart(); shellSort.Sort(array, t); nowTime = timer.ElapsedMilliseconds; timer.Stop(); Console.WriteLine("Elapsed Time:" + nowTime); for (var i = 0; i < bestTimes.Length; i++) { Console.Write("t:" + bestTs[i]); Console.WriteLine("\tTime:" + bestTimes[i]); } if (bestTimes[2] > nowTime) { bestTimes[2] = nowTime; bestTs[2] = t; Array.Sort(bestTimes, bestTs); } array2.CopyTo(array, 0); } for (var i = 0; i < bestTimes.Length; i++) { Console.Write("t:" + bestTs[i]); Console.Write("\tTime:" + bestTimes[i]); } }