RosettaCodeData/Task/Binary-search/Sidef/binary-search-2.sidef

17 lines
406 B
Text
Raw Permalink Normal View History

2016-12-05 23:44:36 +01:00
func binary_search(arr, value, low=0, high=arr.end) {
2017-09-23 10:01:46 +02:00
high < low && return -1
var middle = ((high+low) // 2)
2016-12-05 23:44:36 +01:00
2017-09-23 10:01:46 +02:00
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
}
2016-12-05 23:44:36 +01:00
}
}