`echo "Thanks" `
Run
Thanks for the response. i will try it out
While loop should use:
while lo <= hi:
Run
Or it would fail on lists of length 1.
And this change also needed:
else:
lo = m + 1
Run
Otherwise search gets stuck on last two elements of list, when x > last element.
Assuming that the code is just wrongly formatted and it looks like this:
proc find_x(list: openArray[int], x: int): bool =
var lo = list.low
var hi = list.high
while lo < hi:
var m = int((lo + (hi - lo)) / 2)
if x == list[m]:
return t
proc find_x(list: openArray[int], x: int): bool =
var lo = list.low var hi = list.high
while lo < hi:
var m = int((lo + (hi - lo)) / 2)
if x == list[m]:
return true
elif x < list[m]:
hi = m - 1
else:
lo = m
return false
var arr: array = [1, 2, 3, 4, 6, 8, 12, 45, 67, 80, 90,