32 lines
658 B
Text
32 lines
658 B
Text
use Structure;
|
|
|
|
bundle Default {
|
|
class BinarySearch {
|
|
function : Main(args : String[]) ~ Nil {
|
|
values := [-1, 3, 8, 13, 22];
|
|
DoBinarySearch(values, 13)->PrintLine();
|
|
DoBinarySearch(values, 7)->PrintLine();
|
|
}
|
|
|
|
function : native : DoBinarySearch(values : Int[], value : Int) ~ Int {
|
|
low := 0;
|
|
high := values->Size() - 1;
|
|
|
|
while(low <= high) {
|
|
mid := (low + high) / 2;
|
|
|
|
if(values[mid] > value) {
|
|
high := mid - 1;
|
|
}
|
|
else if(values[mid] < value) {
|
|
low := mid + 1;
|
|
}
|
|
else {
|
|
return mid;
|
|
};
|
|
};
|
|
|
|
return -1;
|
|
}
|
|
}
|
|
}
|