RosettaCodeData/Task/Binary-search/Sidef/binary-search-2.sidef
2017-09-25 22:28:19 +02:00

16 lines
406 B
Text

func binary_search(arr, value, low=0, high=arr.end) {
high < low && return -1
var middle = ((high+low) // 2)
given (arr[middle]) { |item|
case (value < item) {
binary_search(arr, value, low, middle-1)
}
case (value > item) {
binary_search(arr, value, middle+1, high)
}
case (value == item) {
middle
}
}
}