RosettaCodeData/Task/Binary-search/Jq/binary-search-1.jq
2023-07-01 13:44:08 -04:00

11 lines
435 B
Text

def binarySearch(value):
# To avoid copying the array, simply pass in the current low and high offsets
def binarySearch(low; high):
if (high < low) then (-1 - low)
else ( (low + high) / 2 | floor) as $mid
| if (.[$mid] > value) then binarySearch(low; $mid-1)
elif (.[$mid] < value) then binarySearch($mid+1; high)
else $mid
end
end;
binarySearch(0; length-1);