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) { 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) { 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(); 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) { 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; } }
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 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 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) { 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; }
/// <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; }
// 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]); } }
/// <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) { 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; } }
static void Main(string[] args) { var arraySize = 1000000; var sort = new NotifiedMergeSort(arraySize); for (var i = 0; i < 100; i++) { var data = SortCompare.GetRandomArrayInt(arraySize); sort.Sort(data); } Console.WriteLine("n\trest\ttimes"); for (var i = 0; i < sort.NArraySize.Length; i++) { if (sort.NArraySize[i] != 0) { Console.WriteLine(i + "\t" + sort.NArraySize[i] / sort.NArraySizeTime[i] + "\t" + sort.NArraySizeTime[i]); } } // 大致上是一个对数函数 }
static void Main(string[] args) { var insertion = new QuickSortInsertion(); var ignore = new QuickSortIgnore(); var arraySize = 20000; // 初始数组大小。 const int mSteps = 1; // M 值的递增次数。 const int trialTimes = 4; // 每次实验的重复次数。 const int trialLevel = 10; // 双倍递增的次数。 Console.WriteLine("M\tn\t\tignore\tinsert\tratio"); for (var i = 0; i < mSteps; i++) { var array = arraySize; for (var j = 0; j < trialLevel; j++) { double timeIgnore = 0; double timeInsertion = 0; for (var k = 0; k < trialTimes; k++) { var a = SortCompare.GetRandomArrayInt(array); var b = new int[a.Length]; a.CopyTo(b, 0); timeInsertion += SortCompare.Time(insertion, b); timeIgnore += SortCompare.Time(ignore, a); } timeIgnore /= trialTimes; timeInsertion /= trialTimes; if (arraySize < 10000000) { Console.WriteLine(ignore.M + "\t" + array + "\t\t" + timeIgnore + "\t" + timeInsertion + "\t" + timeIgnore / timeInsertion); } else { Console.WriteLine(ignore.M + "\t" + array + "\t" + timeIgnore + "\t" + timeInsertion + "\t" + timeIgnore / timeInsertion); } array *= 2; } ignore.M++; } }
static void Main(string[] args) { var quickNormal = new QuickSort(); var sampleSort = new SampleSort(); var arraySize = 1600000; // 初始数组大小。 const int kSteps = 10; // 取样 k 值的递增次数。 const int trialTimes = 1; // 每次实验的重复次数。 const int trialLevel = 2; // 双倍递增的次数。 Console.WriteLine("k\tn\t\tsample\tnormal\tratio"); for (var i = 0; i < kSteps; i++) { var array = arraySize; for (var j = 0; j < trialLevel; j++) { double timeSample = 0; double timeNormal = 0; for (var k = 0; k < trialTimes; k++) { var a = SortCompare.GetRandomArrayInt(array); var b = new int[a.Length]; a.CopyTo(b, 0); timeNormal += SortCompare.Time(quickNormal, b); timeSample += SortCompare.Time(sampleSort, a); } timeSample /= trialTimes; timeNormal /= trialTimes; if (arraySize < 10000000) { Console.WriteLine(sampleSort.K + "\t" + array + "\t\t" + timeSample + "\t" + timeNormal + "\t" + timeSample / timeNormal); } else { Console.WriteLine(sampleSort.K + "\t" + array + "\t" + timeSample + "\t" + timeNormal + "\t" + timeSample / timeNormal); } array *= 2; } sampleSort.K++; } }
static void Main(string[] args) { MergeSortX mergeSortX = new MergeSortX(); int n = 10000; int trialTimes = 10; Console.WriteLine("数组\t平均命中次数"); for (int i = 0; i < 4; i++) { int avgHit = 0; for (int j = 0; j < trialTimes; j++) { mergeSortX.ResetHitTime(); int[] a = SortCompare.GetRandomArrayInt(n); mergeSortX.Sort(a); avgHit += mergeSortX.GetHitTime(); } Console.WriteLine(n + "\t" + avgHit / trialTimes); n *= 10; } }
static void Compute() { var mergeSort = new MergeSort(); var mergeSortBU = new MergeSortBU(); var mergeResult = new int[10]; var mergeResultBU = new int[10]; var upperBound = new int[10]; // 进行计算 var dataSize = 1; for (var i = 0; i < 10; i++) { var dataMerge = SortCompare.GetRandomArrayInt(dataSize); var dataMergeBU = new int[dataSize]; dataMerge.CopyTo(dataMergeBU, 0); mergeSort.ClearArrayVisitCount(); mergeSortBU.ClearArrayVisitCount(); mergeSort.Sort(dataMerge); mergeSortBU.Sort(dataMergeBU); mergeResult[i] = mergeSort.GetArrayVisitCount(); mergeResultBU[i] = mergeSortBU.GetArrayVisitCount(); upperBound[i] = (int)(6 * dataSize * Math.Log(dataSize, 2)); dataSize *= 2; } // 绘图 var plot = new Form2(); plot.Show(); var graphics = plot.CreateGraphics(); // 获得绘图区矩形。 RectangleF rect = plot.ClientRectangle; var unitX = rect.Width / 10; var unitY = rect.Width / 10; // 添加 10% 边距作为文字区域。 var center = new RectangleF (rect.X + unitX, rect.Y + unitY, rect.Width - 2 * unitX, rect.Height - 2 * unitY); // 绘制坐标系。 graphics.DrawLine(Pens.Black, center.Left, center.Top, center.Left, center.Bottom); graphics.DrawLine(Pens.Black, center.Left, center.Bottom, center.Right, center.Bottom); graphics.DrawString("28000", plot.Font, Brushes.Black, rect.Location); graphics.DrawString("1024", plot.Font, Brushes.Black, center.Right, center.Bottom); graphics.DrawString("0", plot.Font, Brushes.Black, rect.Left, center.Bottom); // 初始化点。 var grayPoints = new PointF[10]; // 上限 var redPoints = new PointF[10]; // 自顶向下 var bluePoints = new PointF[10]; // 自底向上 unitX = center.Width / 11.0f; unitY = center.Height / 28000.0f; for (var i = 0; i < 10; i++) { grayPoints[i] = new PointF(center.Left + unitX * (i + 1), center.Bottom - (upperBound[i] * unitY) - 10); redPoints[i] = new PointF(center.Left + unitX * (i + 1), center.Bottom - (mergeResult[i] * unitY) - 10); bluePoints[i] = new PointF(center.Left + unitX * (i + 1), center.Bottom - (mergeResultBU[i] * unitY) - 10); } // 绘制点。 for (var i = 0; i < 10; i++) { graphics.FillEllipse(Brushes.Gray, new RectangleF(grayPoints[i], new SizeF(10, 10))); graphics.FillEllipse(Brushes.Red, new RectangleF(redPoints[i], new SizeF(10, 10))); graphics.FillEllipse(Brushes.Blue, new RectangleF(bluePoints[i], new Size(10, 10))); } graphics.Dispose(); }