// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Diagnostics; namespace E { public class Program { public struct Interval { public int Ix1; public int Ix2; } private static int Adjust(double[] a, double x, int ix) { if(ix >= a.Length) { return a.Length - 1; } else if(ix == 0) { return 0; } else { return Math.Abs(x - a[ix]) < Math.Abs(x - a[ix - 1]) ? ix : ix - 1; } } private static int SequentialSearchHelper(double[] a, double x, int minix, int maxix) { int ix = minix; while(ix < maxix && a[ix] < x) { ix++; } return ix; } private unsafe static int UnsafeSequentialSearchHelper(double[] a, double x, int minix, int maxix) { fixed(double *p = &a[0]) { double *p2 = p + maxix; double *p1 = p + minix; while(p1 < p2 && *p1 < x) { p1++; } return (int)(p1 - p); } } private static int BinarySearchHelper(double[] a, double x, int minix, int maxix, int threshold) { if(maxix - minix < threshold) { return SequentialSearchHelper(a, x, minix, maxix); } int pivot = (maxix + minix) / 2; if(x < a[pivot]) { return BinarySearchHelper(a, x, minix, pivot, threshold); } else if(x > a[pivot]) { return BinarySearchHelper(a, x, pivot + 1, maxix, threshold); } else { return pivot; } } private unsafe static int UnsafeBinarySearchHelper(double[] a, double x, int minix, int maxix, int threshold) { if(maxix - minix < threshold) { return UnsafeSequentialSearchHelper(a, x, minix, maxix); } int pivot = (maxix + minix) / 2; if(x < a[pivot]) { return UnsafeBinarySearchHelper(a, x, minix, pivot, threshold); } else if(x > a[pivot]) { return UnsafeBinarySearchHelper(a, x, pivot + 1, maxix, threshold); } else { return pivot; } } private static int InterpolateSearchHelper(double[] a, double x, int minix, int maxix, int threshold) { if(maxix - minix < threshold) { return SequentialSearchHelper(a, x, minix, maxix); } double d = (x - a[minix]) / (a[maxix - 1] - a[minix]); int pivot = minix + (int)(d * (maxix - minix)); if(pivot < minix + 1) pivot = minix + 1; if(pivot > maxix - 1) pivot = maxix - 1; if(x < a[pivot]) { return InterpolateSearchHelper(a, x, minix, pivot, threshold); } else if(x > a[pivot]) { return InterpolateSearchHelper(a, x, pivot + 1, maxix, threshold); } else { return pivot; } } private unsafe static int UnsafeInterpolateSearchHelper(double[] a, double x, int minix, int maxix, int threshold) { if(maxix - minix < threshold) { return UnsafeSequentialSearchHelper(a, x, minix, maxix); } double d = (x - a[minix]) / (a[maxix - 1] - a[minix]); int pivot = minix + (int)(d * (maxix - minix)); if(pivot < minix + 1) pivot = minix + 1; if(pivot > maxix - 1) pivot = maxix - 1; if(x < a[pivot]) { return UnsafeInterpolateSearchHelper(a, x, minix, pivot, threshold); } else if(x > a[pivot]) { return UnsafeInterpolateSearchHelper(a, x, pivot + 1, maxix, threshold); } else { return pivot; } } private static Interval[] PreHash(double[] a, double mina, double maxa) { Interval[] res = new Interval[100]; res[0].Ix1 = 0; for(int i = 0; i < res.Length - 1; i++) { double x = mina + (i + 1) * (maxa - mina) / res.Length; res[i].Ix2 = FindBinarySearch(a, x); res[i + 1].Ix1 = res[i].Ix2 + 1; } res[res.Length - 1].Ix2 = a.Length; return res; } public static int FindBuiltin(double[] a, double x) { int ix = Array.BinarySearch(a, x); if(ix >= 0) { return ix; } else { ix = ~ix; return Adjust(a, x, ix); } } public static int FindSequentialSearch(double[] a, double x) { int ix = SequentialSearchHelper(a, x, 0, a.Length); return Adjust(a, x, ix); } public static int FindBinarySearch(double[] a, double x) { int ix = BinarySearchHelper(a, x, 0, a.Length, 2); return Adjust(a, x, ix); } public static int FindBinarySequentialSearch(double[] a, double x) { int ix = BinarySearchHelper(a, x, 0, a.Length, 20); return Adjust(a, x, ix); } public static int FindInterpolateSearch(double[] a, double x) { int ix = InterpolateSearchHelper(a, x, 0, a.Length, 2); return Adjust(a, x, ix); } public static int FindInterpolateSequentialSearch(double[] a, double x) { int ix = InterpolateSearchHelper(a, x, 0, a.Length, 20); return Adjust(a, x, ix); } // FindHashBinarySearch only makes sense if: // * well defined min and max values // * called multiple time with same array and different values // * PreHash is only called once public static int FindHashBinarySearch(double[] a, double x, double mina, double maxa, Interval[] hash) { double d = (x - mina) / (maxa - mina); Interval iv = hash[(int)(d * hash.Length)]; int ix = BinarySearchHelper(a, x, iv.Ix1, iv.Ix2, 20); return Adjust(a, x, ix); } public static int UnsafeFindSequentialSearch(double[] a, double x) { int ix = UnsafeSequentialSearchHelper(a, x, 0, a.Length); return Adjust(a, x, ix); } public static int UnsafeFindBinarySequentialSearch(double[] a, double x) { int ix = UnsafeBinarySearchHelper(a, x, 0, a.Length, 20); return Adjust(a, x, ix); } public static int UnsafeFindInterpolateSequentialSearch(double[] a, double x) { int ix = UnsafeInterpolateSearchHelper(a, x, 0, a.Length, 20); return Adjust(a, x, ix); } // FindHashBinarySearch only makes sense if: // * well defined min and max values // * called multiple time with same array and different values // * PreHash is only called once public static int UnsafeFindHashBinarySearch(double[] a, double x, double mina, double maxa, Interval[] hash) { double d = (x - mina) / (maxa - mina); Interval iv = hash[(int)(d * hash.Length)]; int ix = UnsafeBinarySearchHelper(a, x, iv.Ix1, iv.Ix2, 20); return Adjust(a, x, ix); } private static readonly Random rng = new Random(); private static void Print(double[] a, string algo, int ix) { Console.WriteLine("{0,-36} : {1,10:F8} {2,10:F8} {3,10:F8} ", algo, ix > 0 ? a[ix - 1] : 0.0, a[ix], ix < a.Length - 1 ? a[ix + 1] : 1.0); } public static void Test(int n) { double[] a = new Double[n]; for(int i = 0; i < a.Length; i++) { a[i] = rng.NextDouble(); } Array.Sort(a); double x = rng.NextDouble(); int ix_b = FindBuiltin(a, x); Print(a, "Builtin", ix_b); int ix_ss = FindSequentialSearch(a, x); Print(a, "Sequential search", ix_ss); int ix_bs = FindBinarySearch(a, x); Print(a, "Binary search", ix_bs); int ix_bss = FindBinarySequentialSearch(a, x); Print(a, "Binary sequential search", ix_bss); int ix_is = FindInterpolateSearch(a, x); Print(a, "Interpolate search", ix_is); int ix_iss = FindInterpolateSequentialSearch(a, x); Print(a, "Interpolate sequential search", ix_iss); Interval[] hash = PreHash(a, 0.0, 1.0); int ix_hss = FindHashBinarySearch(a, x, 0.0, 1.0, hash); Print(a, "Hash binary search", ix_hss); int ix_uss = UnsafeFindSequentialSearch(a, x); Print(a, "Unsafe sequential search", ix_uss); int ix_ubss = UnsafeFindBinarySequentialSearch(a, x); Print(a, "Unsafe binary sequential search", ix_ubss); int ix_uiss = UnsafeFindInterpolateSequentialSearch(a, x); Print(a, "Unsafe interpolate sequential search", ix_uiss); Interval[] uhash = PreHash(a, 0.0, 1.0); int ix_uhbs = UnsafeFindHashBinarySearch(a, x, 0.0, 1.0, uhash); Print(a, "Unsafe hash binary search", ix_uhbs); } private static void PrintPerf(string algo, Stopwatch sw) { Console.WriteLine("{0,-36} : {1,6} ms", algo, sw.ElapsedMilliseconds); } public static void PerfTest(int n, int rep) { double[] a = new Double[n]; for(int i = 0; i < a.Length; i++) { a[i] = rng.NextDouble(); } Array.Sort(a); double[] x = new double[100]; for(int j = 0; j < x.Length; j++) x[j] = rng.NextDouble(); Stopwatch sw_b = new Stopwatch(); sw_b.Start(); for(int i = 0; i < rep; i++) { for(int j = 0; j < x.Length; j++) FindBuiltin(a, x[j]); } sw_b.Stop(); PrintPerf("Builtin", sw_b); Stopwatch sw_ss = new Stopwatch(); sw_ss.Start(); for(int i = 0; i < rep; i++) { for(int j = 0; j < x.Length; j++) FindSequentialSearch(a, x[j]); } sw_ss.Stop(); PrintPerf("Sequential search", sw_ss); Stopwatch sw_bs = new Stopwatch(); sw_bs.Start(); for(int i = 0; i < rep; i++) { for(int j = 0; j < x.Length; j++) FindBinarySearch(a, x[j]); } sw_bs.Stop(); PrintPerf("Binary search", sw_bs); Stopwatch sw_bss = new Stopwatch(); sw_bss.Start(); for(int i = 0; i < rep; i++) { for(int j = 0; j < x.Length; j++) FindBinarySequentialSearch(a, x[j]); } sw_bss.Stop(); PrintPerf("Binary sequential search", sw_bss); Stopwatch sw_is = new Stopwatch(); sw_is.Start(); for(int i = 0; i < rep; i++) { for(int j = 0; j < x.Length; j++) FindBinarySearch(a, x[j]); } sw_is.Stop(); PrintPerf("Interpolate search", sw_is); Stopwatch sw_iss = new Stopwatch(); sw_iss.Start(); for(int i = 0; i < rep; i++) { for(int j = 0; j < x.Length; j++) FindBinarySequentialSearch(a, x[j]); } sw_iss.Stop(); PrintPerf("Interpolate sequential search", sw_iss); Stopwatch sw_hbs = new Stopwatch(); sw_hbs.Start(); Interval[] hash = PreHash(a, 0.0, 1.0); for(int i = 0; i < rep; i++) { for(int j = 0; j < x.Length; j++) FindHashBinarySearch(a, x[j], 0.0, 1.0, hash); } sw_hbs.Stop(); PrintPerf("Hash binary search", sw_hbs); Stopwatch sw_uss = new Stopwatch(); sw_uss.Start(); for(int i = 0; i < rep; i++) { for(int j = 0; j < x.Length; j++) UnsafeFindSequentialSearch(a, x[j]); } sw_uss.Stop(); PrintPerf("Unsafe sequential search", sw_uss); Stopwatch sw_ubss = new Stopwatch(); sw_ubss.Start(); for(int i = 0; i < rep; i++) { for(int j = 0; j < x.Length; j++) UnsafeFindBinarySequentialSearch(a, x[j]); } sw_ubss.Stop(); PrintPerf("Unsafe binary sequential search", sw_ubss); Stopwatch sw_uiss = new Stopwatch(); sw_uiss.Start(); for(int i = 0; i < rep; i++) { for(int j = 0; j < x.Length; j++) UnsafeFindInterpolateSequentialSearch(a, x[j]); } sw_uiss.Stop(); PrintPerf("Unsafe interpolate sequential search", sw_uiss); Stopwatch sw_uhbs = new Stopwatch(); sw_uhbs.Start(); Interval[] uhash = PreHash(a, 0.0, 1.0); for(int i = 0; i < rep; i++) { for(int j = 0; j < x.Length; j++) UnsafeFindHashBinarySearch(a, x[j], 0.0, 1.0, uhash); } sw_uhbs.Stop(); PrintPerf("Unsafe hash binary search", sw_uhbs); } public static void Main(string[] args) { Test(3); Test(100); PerfTest(100, 200000); PerfTest(1000, 100000); PerfTest(10000, 50000); } } }