Find the nearest/closest value in a sorted List

drBet picture drBet · May 14, 2015 · Viewed 40.3k times · Source

I was wondering if it is possible to find the closest element in a List for a element that is not there.

For example if we had the values [1,3,6,7] and we are looking for the element closest to 4, it should return 3, because 3 is the biggest number in the array, that is smaller than 4.

I hope it makes sense, because English is not my native language.

Answer

David Soroko picture David Soroko · May 14, 2015

If the array is sorted you can do a modified binary search in O( log n ) :

    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];
    }