Binary Search in C++: Efficient Way to Search in a Sorted Array
Binary search is one of the most efficient searching algorithms in computer science. If you're working with sorted arrays or lists, binary search helps you find an element in O(log n) time, making it significantly faster than linear search.
📘 What You'll Learn
-
What binary search is and how it works
-
Implementation of binary search in C++
-
How to interpret the output
-
Clean and copyable code with explanation
🔍 What is Binary Search?
Binary search is an algorithm that works only on sorted data. It divides the array into halves to locate the target element efficiently.
Here’s how it works:
-
Find the middle element of the array.
-
If it matches the target, return its index.
-
If the target is smaller, repeat the search in the left half.
-
If the target is larger, search in the right half.
C++ Code Example
Below is a copyable C++ code snippet that performs binary search on a sorted array.
#include <iostream>
using namespace std;
int binarySearch(int arr[], int size, int key) {
int start = 0, end = size - 1;
while (start <= end) {
int mid = start + (end - start) / 2;
if (arr[mid] == key) return mid;
else if (arr[mid] < key) start = mid + 1;
else end = mid - 1;
}
return -1; // Element not found
}
int main() {
int arr[] = {2, 4, 10, 23, 38, 45, 59};
int size = sizeof(arr)/sizeof(arr[0]);
int key = 23;
int result = binarySearch(arr, size, key);
if (result != -1)
cout << "Element found at index " << result << endl;
else
cout << "Element not found." << endl;
return 0;
}
#include <iostream>
using namespace std;
int binarySearch(int arr[], int size, int key) {
int start = 0, end = size - 1;
while (start <= end) {
int mid = start + (end - start) / 2;
if (arr[mid] == key) return mid;
else if (arr[mid] < key) start = mid + 1;
else end = mid - 1;
}
return -1; // Element not found
}
int main() {
int arr[] = {2, 4, 10, 23, 38, 45, 59};
int size = sizeof(arr)/sizeof(arr[0]);
int key = 23;
int result = binarySearch(arr, size, key);
if (result != -1)
cout << "Element found at index " << result << endl;
else
cout << "Element not found." << endl;
return 0;
}
🧾 Sample Output
Element found at index 3
In the sample array {2, 4, 10, 23, 38, 45, 59}
, the element 23
exists at index 3
(0-based indexing).
✅ Why Use Binary Search?
-
Much faster than linear search for large datasets
-
Reduces time complexity to O(log n)
-
Ideal for implementing in sorted arrays and database queries
💡 Pro Tips
-
Always ensure the array is sorted before using binary search.
-
You can implement both iterative and recursive versions.
-
For searching in
std::vector
, consider usingstd::binary_search
from<algorithm>
.
📘 Conclusion
Binary search is a powerful and efficient technique that every programmer should master. Especially in competitive programming and real-time systems, it can make a big difference in performance. By understanding the logic and writing clean C++ code, you’ll be ready for interviews and real-world coding tasks.
Post a Comment