java - Find the nearest/closest value in a sorted List -
i wondering if possible find closest element in list
element that not there.
for example if had values [1,3,6,7] , looking element closest 4, should return 3, because 3 biggest number in array, smaller 4.
i hope makes sense, because english not native language.
if array sorted can modified binary search in log n
operations:
public static int search(int value, int[] a) { if(value < a[0]) { return a[0]; } if(value > a[a.length-1]) { return a[a.length-1]; } int lo = 0; int hi = a.length - 1; while (lo <= hi) { int mid = (hi + lo) / 2; if (value < a[mid]) { hi = mid - 1; } else if (value > a[mid]) { lo = mid + 1; } else { return a[mid]; } } // lo == hi + 1 return (a[lo] - value) < (value - a[hi]) ? a[lo] : a[hi]; }
Comments
Post a Comment