Binary Search is an efficient searching algorithm that works on sorted arrays by repeatedly dividing the search interval in half.
int binarySearch(int arr[], int low, int high, int key) {
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == key)
return mid;
else if (arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
Binary search is ideal for large sorted datasets due to its logarithmic time complexity.
Take quizzes related to this topic and see where you stand!
Start Quiz Now