|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | + |
| 4 | +namespace _3._1._41 |
| 5 | +{ |
| 6 | + /* |
| 7 | + * 3.1.41 |
| 8 | + * |
| 9 | + * 插值查找的临界点。 |
| 10 | + * 找出使用插值查找比二分查找要快 1 倍、2 倍和 10 倍的 N 值, |
| 11 | + * 其中假设所有键为随机的 32 二进位整数(请见练习 3.1.24)。 |
| 12 | + * 分析并预测 N 的大小并通过实验验证它。 |
| 13 | + * |
| 14 | + */ |
| 15 | + class Program |
| 16 | + { |
| 17 | + static long binarySearchCompare = 0; |
| 18 | + static long interpolationSearchCompare = 0; |
| 19 | + static Random random = new Random(); |
| 20 | + |
| 21 | + static void Main(string[] args) |
| 22 | + { |
| 23 | + // 原文:1, 2 and 10 times faster |
| 24 | + // 也就是一样快,快一倍和快十倍 |
| 25 | + |
| 26 | + // 对于均匀分布的数组,插值查找的平均需要 lg(lgN)) 次比较 |
| 27 | + // 解方程 |
| 28 | + // lg(lgN)) = lgN, N = 0.8.. |
| 29 | + // 2lg(lgN)) = lgN, N = 4 |
| 30 | + // 10lg(lgN)) = lgN, N = 58 |
| 31 | + |
| 32 | + Console.WriteLine("n\tist(compare/time)\tbst(compare/time)\tratio(compare/time)"); |
| 33 | + Test(1, 10000000); |
| 34 | + Test(4, 10000000); |
| 35 | + Test(58, 10000000); |
| 36 | + } |
| 37 | + static void Test(int n, int trial) |
| 38 | + { |
| 39 | + binarySearchCompare = 0; |
| 40 | + interpolationSearchCompare = 0; |
| 41 | + Console.Write(n + "\t"); |
| 42 | + |
| 43 | + Stopwatch sw = new Stopwatch(); |
| 44 | + |
| 45 | + int[] data = new int[n]; |
| 46 | + int[] dataSorted = new int[n]; |
| 47 | + int[] dataQuery = new int[trial]; |
| 48 | + for (int i = 0; i < n; i++) |
| 49 | + { |
| 50 | + dataSorted[i] = i; |
| 51 | + data[i] = i; |
| 52 | + } |
| 53 | + Shuffle(data); |
| 54 | + for (int i = 0; i < trial; i++) |
| 55 | + { |
| 56 | + dataQuery[i] = random.Next(0, n); |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + long bstTime = 0, istTime = 0; |
| 61 | + // 测试 ist |
| 62 | + sw.Start(); |
| 63 | + for (int i = 0; i < trial; i++) |
| 64 | + { |
| 65 | + InterpolationSearch(data, dataQuery[i]); |
| 66 | + } |
| 67 | + sw.Stop(); |
| 68 | + istTime = sw.ElapsedMilliseconds; |
| 69 | + Console.Write(interpolationSearchCompare + "/" + istTime + "\t\t"); |
| 70 | + |
| 71 | + // 测试 bst |
| 72 | + sw.Restart(); |
| 73 | + for (int i = 0; i < trial; i++) |
| 74 | + { |
| 75 | + BinarySearch(dataSorted, dataQuery[i]); |
| 76 | + } |
| 77 | + sw.Stop(); |
| 78 | + |
| 79 | + bstTime = sw.ElapsedMilliseconds; |
| 80 | + Console.Write(binarySearchCompare + "/" + bstTime + "\t\t"); |
| 81 | + |
| 82 | + // 比例 |
| 83 | + Console.WriteLine((float)interpolationSearchCompare / binarySearchCompare + "/" + (float)istTime / bstTime); |
| 84 | + } |
| 85 | + |
| 86 | + static void Shuffle<T>(T[] array) |
| 87 | + { |
| 88 | + for (int i = 0; i < array.Length; i++) |
| 89 | + { |
| 90 | + int p = i + random.Next(array.Length - i); |
| 91 | + T temp = array[p]; |
| 92 | + array[p] = array[i]; |
| 93 | + array[i] = temp; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + static int BinarySearch(int[] a, int key) |
| 98 | + { |
| 99 | + int lo = 0, hi = a.Length - 1; |
| 100 | + while (lo <= hi) |
| 101 | + { |
| 102 | + int mid = (lo + hi) / 2; |
| 103 | + int compare = a[mid].CompareTo(key); |
| 104 | + binarySearchCompare++; |
| 105 | + if (compare > 0) |
| 106 | + hi = mid - 1; |
| 107 | + else if (compare < 0) |
| 108 | + lo = mid + 1; |
| 109 | + else |
| 110 | + return mid; |
| 111 | + } |
| 112 | + return -1; |
| 113 | + } |
| 114 | + |
| 115 | + |
| 116 | + static int InterpolationSearch(int[] a, int key) |
| 117 | + { |
| 118 | + int lo = 0, hi = a.Length - 1; |
| 119 | + while (lo <= hi) |
| 120 | + { |
| 121 | + double percent = a[hi] == a[lo] ? 0 : (key - a[lo]) / (a[hi] - a[lo]); |
| 122 | + int index = lo + (int)Math.Floor((hi - lo) * percent); |
| 123 | + if (percent < 0) |
| 124 | + index = lo; |
| 125 | + if (percent > 1) |
| 126 | + index = hi; |
| 127 | + |
| 128 | + int compare = a[index].CompareTo(key); |
| 129 | + interpolationSearchCompare++; |
| 130 | + if (compare > 0) |
| 131 | + hi = index - 1; |
| 132 | + else if (compare < 0) |
| 133 | + lo = index + 1; |
| 134 | + else |
| 135 | + return index; |
| 136 | + } |
| 137 | + return -1; |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments