CSC300: Binary search [6/8] |
01 |
public static boolean contains (double val, double[] list) { int lo = 0; int hi = list.length - 1; while (lo <= hi) { int mid = lo + (hi-lo)/2; if (val > list[mid]) lo = mid + 1; else if (val < list[mid]) hi = mid - 1; else return true; } return false; } |
Output
Finished tests Elapsed count N= 512 [ 0.000 : NaN] Elapsed count N= 1,024 [ 0.000 : NaN] Elapsed count N= 2,048 [ 0.000 : NaN] Elapsed count N= 4,096 [ 0.000 : NaN] Elapsed count N= 8,192 [ 0.000 : NaN] Elapsed count N= 16,384 [ 0.000 : NaN] Elapsed count N= 32,768 [ 0.000 : NaN] Elapsed count N= 65,536 [ 0.000 : NaN] Elapsed count N= 131,072 [ 0.000 : NaN] Elapsed count N= 262,144 [ 0.000 : NaN] Elapsed count N= 524,288 [ 0.000 : NaN] Elapsed count N= 1,048,576 [ 0.000 : NaN] Elapsed count N= 2,097,152 [ 0.000 : NaN] Elapsed count N= 4,194,304 [ 0.000 : NaN] Elapsed count N= 8,388,608 [ 0.000 : NaN] Elapsed count N= 16,777,216 [ 0.000 : NaN] Elapsed count N= 33,554,432 [ 0.000 : NaN] Elapsed count N= 67,108,864 [ 0.000 : NaN] Elapsed count N= 134,217,728 [ 0.000 : NaN] Elapsed count N= 268,435,456 [ 0.000 : NaN]