16 lines
628 B
Text
16 lines
628 B
Text
const func integer: binarySearch (in array elemType: arr, in elemType: aKey, in integer: low, in integer: high) is func
|
|
result
|
|
var integer: result is 0;
|
|
begin
|
|
if low <= high then
|
|
result := (low + high) div 2;
|
|
if aKey < arr[result] then
|
|
result := binarySearch(arr, aKey, low, pred(result)); # search left
|
|
elsif aKey > arr[result] then
|
|
result := binarySearch(arr, aKey, succ(result), high); # search right
|
|
end if;
|
|
end if;
|
|
end func;
|
|
|
|
const func integer: binarySearchRecursive (in array elemType: arr, in elemType: aKey) is
|
|
return binarySearch(arr, aKey, 1, length(arr));
|