Binary Search
Implementation of binary search in array using iterative as well as recursive method.
Iterative Method
int binarySearch(int a[], int low, int high, int key) {
while (low <= high) {
int mid = (low + high)/2;
if (a[mid] == key) {
return mid;
} else if (a[mid] > key) {
// Key lies in the left sublist
high = mid - 1;
} else {
// Key lies in the right sublist
low = mid + 1;
}
}
return -1;
}Recursive Method
Last updated