static void Main(string[] args) { Console.WriteLine("n\t2-way\t3-way\tRatio\t4-way\tRatio"); int n = 1000; // 当数据量到达 10^9 时会需要 2G 左右的内存 int multiTen = 7; for (int i = 0; i < multiTen; i++) { Console.Write("10^" + (i + 3) + "\t"); short[] data = GetRandomArray(n); BackupArray(data, i); // 暂存数组 long originCount = HeapAnalysis.Sort(data); Console.Write(originCount + "\t"); RestoreArray(data, i); // 恢复数组 long threeWayCount = HeapMultiwayAnalysis.Sort(data, 3); Console.Write(threeWayCount + "\t" + (float)threeWayCount / originCount + "\t"); RestoreArray(data, i); // 恢复数组 long fourWayCount = HeapMultiwayAnalysis.Sort(data, 4); Console.WriteLine(fourWayCount + "\t" + (float)fourWayCount / originCount); n *= 10; } }
static void Main(string[] args) { Console.WriteLine("n\tOrigin\tFloyd\tRatio"); int n = 1000; // 当数据量到达 10^9 时会需要 2G 左右的内存 int multiTen = 7; for (int i = 0; i < multiTen; i++) { short[] data = GetRandomArray(n); BackupArray(data, i); // 暂存数组 long originCount = HeapAnalysis.Sort(data); RestoreArray(data, i); // 恢复数组 long floydCount = HeapFloydAnalysis.Sort(data); Console.WriteLine(n + "\t" + originCount + "\t" + floydCount + "\t" + (double)floydCount / originCount); n *= 10; } }
static void Main(string[] args) { Console.WriteLine("n\torigin\tpre-order\tRatio"); var n = 1000; // 当数据量到达 10^9 时会需要 2G 左右的内存 var multiTen = 7; for (var i = 0; i < multiTen; i++) { Console.Write("10^" + (i + 3) + "\t"); var data = GetRandomArray(n); BackupArray(data, i); // 暂存数组 var originCount = HeapAnalysis.Sort(data); Console.Write(originCount + "\t"); RestoreArray(data, i); // 恢复数组 var preorderCount = HeapPreorderAnalysis.Sort(data); Console.WriteLine(preorderCount + "\t" + (float)preorderCount / originCount + "\t"); n *= 10; } }
static void Main(string[] args) { // 最好情况 // 比较简单,键值完全相同的数组即可。 // 最坏情况 // 参考论文:https://arxiv.org/abs/1504.01459 // 下面的代码能够生成最好和最坏情况并输出序列和比较次数。 int size = 32; int[] bestCase = new int[size]; for (int i = 0; i < size; i++) { bestCase[i] = 1; } Console.WriteLine("Best Input"); for (int i = 0; i < size; i++) { Console.Write(bestCase[i] + " "); } Console.WriteLine(); Console.Write("Best Compare Times: "); Console.WriteLine(HeapAnalysis.Sort(bestCase)); Console.WriteLine(); MaxPQWorstCase pq = new MaxPQWorstCase(size); int[] worstCase = pq.MakeWorst(size); Console.WriteLine("Worst Input"); for (int i = 0; i < worstCase.Length; i++) { Console.Write(worstCase[i] + " "); } Console.WriteLine(); Console.Write("Worst Compare Times: "); Console.WriteLine(HeapAnalysis.Sort(worstCase)); }