Table of contents | |
Introduction | |
Prerequisites | |
Binary Search Algorithm | |
Binary Search Implementation in C++ | |
Recursive Binary Search | |
Sample Problems |
Binary search is a fundamental searching algorithm used to efficiently find a target value in a sorted array or list. It follows the principle of divide and conquer to quickly narrow down the search space. In this article, we will explore binary search in detail, including its implementation in C++ along with examples and explanations.
Before diving into binary search, make sure you have a basic understanding of C++ programming and arrays. Familiarity with loops and conditional statements will also be helpful.
Binary search is based on the following steps:
Let's now look at the implementation of binary search in C++. We will start with a basic iterative approach and then explore a recursive implementation.
Iterative Binary Search
#include <iostream>
using namespace std;
int binarySearch(int arr[], int size, int target) {
int left = 0;
int right = size - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target)
return mid;
if (arr[mid] < target)
left = mid + 1;
else
right = mid - 1;
}
return -1; // Target value not found
}
int main() {
int arr[] = {1, 3, 5, 7, 9};
int size = sizeof(arr) / sizeof(arr[0]);
int target = 5;
int result = binarySearch(arr, size, target);
if (result == -1)
cout << "Element not found";
else
cout << "Element found at index: " << result;
return 0;
}
Output:
Element found at index: 2
Explanation:
#include <iostream>
using namespace std;
int binarySearchRecursive(int arr[], int left, int right, int target) {
if (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target)
return mid;
if (arr[mid] < target)
return binarySearchRecursive(arr, mid + 1, right, target);
else
return binarySearchRecursive(arr, left, mid - 1, target);
}
return -1; // Target value not found
}
int main() {
int arr[] = {1, 3, 5, 7, 9};
int size = sizeof(arr) / sizeof(arr[0]);
int target = 5;
int result = binarySearchRecursive(arr, 0, size - 1, target);
if (result == -1)
cout << "Element not found";
else
cout << "Element found at index: " << result;
return 0;
}
Output:
Element found at index: 2
Explanation:
Problem 1: Given a sorted array '[2, 4, 6, 8, 10, 12]', find the index of the target element '6'.
Applying binary search, the target element '6' is found at index '2'.
Problem 2: Given a sorted array '[-2, 0, 3, 5, 7, 9]', find the index of the target element '0'.
Applying binary search, the target element '0' is found at index '1'.
Problem 3: Given a sorted array '[1, 3, 5, 7, 9]', find the index of the target element '4'.
Applying binary search, the target element '4' is not found in the array. The search returns '-1'.
Binary search is a powerful algorithm for efficiently searching sorted arrays or lists. It follows a divide and conquer approach, reducing the search space in half at each step. In this article, we explored binary search and its implementation in C++. We discussed both iterative and recursive approaches, along with example codes and explanations. By understanding the binary search algorithm, you can optimize your search operations and improve the efficiency of your programs.
153 videos|115 docs|24 tests
|
|
Explore Courses for Software Development exam
|