Binary Search
Binary Search
Introduction: Binary search is an efficient algorithm for finding a target element in a sorted array or list. Unlike linear
search, which checks each element one by one, binary search divides the search range in half each time, significantly
reducing the number of comparisons. It works by comparing the target value to the middle element of the array.
#include <iostream>
if (arr[mid] == target) {
return mid;
right = mid - 1;
else {
left = mid + 1;
return -1;
int main() {
int target;
if (result != -1) {
cout << "Element " << target << " found at index " << result << endl;
} else {
cout << "Element " << target << " not found." << endl;
return 0;