Get index of closest value with binary search
Here is the code that will return the index if the value is found, otherwise the index of the item that is closest to that value, hope it helps.
def binarySearch(data, val):
lo, hi = 0, len(data) - 1
best_ind = lo
while lo <= hi:
mid = lo + (hi - lo) // 2
if data[mid] < val:
lo = mid + 1
elif data[mid] > val:
hi = mid - 1
else:
best_ind = mid
break
# check if data[mid] is closer to val than data[best_ind]
if abs(data[mid] - val) < abs(data[best_ind] - val):
best_ind = mid
return best_ind
def main():
data = [1, 2, 3, 4, 5, 6, 7]
val = 6.1
ind = binarySearch(data, val)
print 'data[%d]=%d' % (ind, data[ind])
if __name__ == '__main__':
main()
Something like this should work. It returns an array with two indexes. If val is found, both values in the return array are the same. Otherwise, it returns the indexes of the two items closest to val.
def binarySearch(data, val):
highIndex = len(data)-1
lowIndex = 0
while highIndex > lowIndex:
index = (highIndex + lowIndex) / 2
sub = data[index]
if data[lowIndex] == val:
return [lowIndex, lowIndex]
elif sub == val:
return [index, index]
elif data[highIndex] == val:
return [highIndex, highIndex]
elif sub > val:
if highIndex == index:
return sorted([highIndex, lowIndex])
highIndex = index
else:
if lowIndex == index:
return sorted([highIndex, lowIndex])
lowIndex = index
return sorted([highIndex, lowIndex])
I know this is an old question, but it's high on Google's results and I had the same issue. There's a built-in to do this which uses binary search and allows you to feed in a reference array and a comparison array.
numpy.searchsorted(a, v, side='left', sorter=None)
a
is the reference array (data
in original question), v
is the array to compare (val
from the question). This returns an array
of size v
with int values for the index the nth element of v
would need to be inserted into a
to preserve the sort order in a
' The side
keyword determines whether you want the elements of v
to be placed to the 'left' (before) or the 'right' (after) the appropriate value in a
.
[documentation link as of July 2017] https://docs.scipy.org/doc/numpy/reference/generated/numpy.searchsorted.html#numpy.searchsorted