
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Binary Search in C++
The binary search algorithm works as per the divide-and-conquer principle, as it keeps dividing the array in half before searching. To search for an element in an array using binary search, it should be sorted.
In the sorted array, we find the middle element and compare it with the element that has to be searched, and based on the comparison, we either search in the left or right sub-array or return the middle element.
In this article, we are given a sorted array of integers, and our task is to search for the given target element using the binary search algorithm. Here are some example scenarios:
Scenario 1
Input: arr[] = {1, 3, 5, 7, 9}, target = 7 Output: Element 7 found at index: 3
Scenario 2
Input: arr[] = {2, 4, 6, 8, 10}, target = 5 Output: Element 5 not found
The approaches to implement binary search are given below:
- Binary Search Using Iteration
- Binary Search Using Recursion
- Binary Search Using binary_search() Function
Binary Search Using Iteration
The following are the steps to search for an element using binary search with an iterative approach:
- Define a binSearch() function that accepts the array, the size of the array, and the target that you want to search.
- Then calculate the middle index of the array.
- Compare the target with the middle element using an if-else statement. Return the middle element if the target is found at the middle index.
- If the target is not available at the middle index, then check if it is greater or less than the middle element.
- If the target is greater than the middle element, then set the left index to mid + 1 and search in the right half of the array.
- If the target is less than the middle element, then set the right index to mid - 1 and search in the left half of the array.
- Repeat the above four steps until the left index becomes greater than the right index. If the element is found, return the index; otherwise, return element not found.
Example
In the following example, we are searching for an element in an array using binary search:
#include <iostream> using namespace std; int binSearch(int arr[], int n, int target) { int left = 0, right = n - 1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == target) return mid; else if (arr[mid] < target) left = mid + 1; else right = mid - 1; } return -1; } int main() { int arr[] = {1, 3, 5, 7, 9}; int n = sizeof(arr) / sizeof(arr[0]); int target = 7; cout << "Given array: "; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; cout << "Target Element to search: " << target << endl; int result = binSearch(arr, n, target); cout << "Element " << target << " found at index: " << result << endl; return 0; }
The output of the above code is as follows:
Given array: 1 3 5 7 9 Target Element to search: 7 Element 7 found at index: 3
Binary Search Using Recursion
The following are the steps to implement binary search using recursion:
- Define a binSearch() function that accepts the array, the left index, the right index, and the target that you want to search.
- Check if the left index becomes greater than the right index. If yes, return element not found.
- Then, calculate the middle index of the array and compare the target with the middle element using an if-else statement. Return the middle index if the target is found at the middle index.
- If the target is not available at the middle index, then check if it is greater or less than the middle element.
- If the target is greater than the middle element, then recursively call the binSearch() function by setting the left index as mid + 1 and searching in the right half of the array.
- If the target is less than the middle element, then recursively call the binSearch() function by setting the right index as mid - 1 and searching in the left half of the array.
- Repeat the above four steps until the left index becomes greater than the right index. If the element is found, return the index; otherwise, return element not found.
Example
The following example has a recursive function that searches for an element in a binary tree:
#include <iostream> using namespace std; int binSearch(int arr[], int left, int right, int target) { if (left > right) return -1; int mid = left + (right - left) / 2; if (arr[mid] == target) return mid; else if (arr[mid] < target) return binSearch(arr, mid + 1, right, target); else return binSearch(arr, left, mid - 1, target); } int main() { int arr[] = {2, 4, 6, 8, 10}; int n = sizeof(arr) / sizeof(arr[0]); int target = 6; cout << "Given array: "; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; cout << "Target Element to search: " << target << endl; int result = binSearch(arr, 0, n - 1, target); cout << "Element " << target << " found at index: " << result << endl; return 0; }
The output of the above code is as follows:
Given array: 2 4 6 8 10 Target Element to search: 6 Element 6 found at index: 2
Binary Search Using binary_search() Function
The STL function binary_search() of the <algorithm> header accepts the starting index arr, the index up to which we want to search an element, i.e, arr+n, and the target value that we want to search. It implements binary search and finds the target value.
Example
In this example, we have used the binary_search() function to search for 9 in the given array with binary search:
#include <iostream> #include <algorithm> using namespace std; int main() { int arr[] = {1, 4, 6, 9, 13}; int n = sizeof(arr) / sizeof(arr[0]); int target = 9; cout << "Given array: "; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; cout << "Target Element to search: " << target << endl; bool result = binary_search(arr, arr + n, target); if (result) cout << "Element " << target << " found" << endl; else cout << "Not Found" << endl; return 0; }
The output of the above code is as follows:
Given array: 1 4 6 9 13 Target Element to search: 9 Element 9 found
Complexity Comparison
Here is a comparison of the time and space complexity of all the above approaches.
Approach | Time Complexity | Space Complexity |
---|---|---|
Iterative Technique | O(log n) | O(1) |
Recursive Approach | O(log n) | O(log n) |
binary_search() Function | O(log n) | O(1) |