i was trying a simple binary search algorithm, what am i doing wrong

2024-03-28 Thread victor_isikwei
`echo "Thanks" ` Run

i was trying a simple binary search algorithm, what am i doing wrong

2024-03-28 Thread victor_isikwei
Thanks for the response. i will try it out

i was trying a simple binary search algorithm, what am i doing wrong

2024-03-28 Thread janAkali
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.

i was trying a simple binary search algorithm, what am i doing wrong

2024-03-28 Thread janAkali
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

i was trying a simple binary search algorithm, what am i doing wrong

2024-03-28 Thread victor_isikwei
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,