21 lines
590 B
Text
21 lines
590 B
Text
binarySearch: function [arr,val,low,high][
|
|
if high < low -> return ø
|
|
mid: shr low+high 1
|
|
when.has: val [
|
|
[< arr\[mid]] -> return binarySearch arr val low mid-1
|
|
[> arr\[mid]] -> return binarySearch arr val mid+1 high
|
|
true -> return mid
|
|
]
|
|
]
|
|
|
|
ary: [
|
|
0 1 4 5 6 7 8 9 12 26 45 67
|
|
78 90 98 123 211 234 456 769
|
|
865 2345 3215 14345 24324
|
|
]
|
|
|
|
loop [0 42 45 24324 99999] 'v [
|
|
i: binarySearch ary v 0 (size ary)-1
|
|
switch not? null? i -> print ["found" v "at index:" i]
|
|
-> print [v "not found"]
|
|
]
|