Binary Search Algorithm in C++

Binary search is one of the most fundamental and effective search methods in the field of computer science and algorithms. It is frequently utilized in many applications where sorted data is arranged. We will go into the world of Binary Search in C++ algorithms in this post, examining its ideas, applications, and effectiveness.

What does the C++ Binary Search Algorithm do?


An effective approach for locating a target value’s location within a sorted array is a binary search. With each iteration, the algorithm uses a divide-and-conquer strategy to cut the search space in half, making it extremely effective for big datasets.

The Binary Search Process

Data input: The input data must be arranged in either ascending or descending order in order to do a binary search. Assume we have a target value target and a sorted array called arr that we are trying to find.

Setting two pointers, left and right, to the first and last members of the array, respectively, initiates the binary search process. (Left + Right) / 2 is another formula for calculating the midpoint.

Comparison: We contrast the element at the midpoint arr[mid] with the goal value target. Three scenarios are possible:

step one: If the target equals arr[mid], the search has successfully located the requested element.

Two: The target value must be on the left side of the array if the target is less than arr[mid].In order to search in the left sub-array, we update right = mid-1.

step Three: The Target must be in the right half of the array if it is greater than arr[mid]. So, to search in the right sub-array, we update left = mid + 1.

Iterative Method: Until the target element is located or until left exceeds right, indicating that the element is absent from the array, steps 2 and 3 are repeated.

Output: The binary search provides the index of the target element if it is located; else, it returns a special value (for example, -1) to denote that the element is absent from the array.

Implementing binary search Algorithm in C++

#include <iostream>
using namespace std;

int binarySearch(int arr[], int left, int right, int target) {
    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;
}

int main() {
    int arr[] = {2, 4, 6, 8, 10, 12, 14, 16};
    int n = sizeof(arr) / sizeof(arr[0]);
    int target = 10;

    int result = binarySearch(arr, 0, n - 1, target);//Binary Search in C++

    if (result != -1)
        cout << "Element found at index: " << result << endl;
    else
        cout << "Element not found in the array." << endl;

    return 0;
}

A very effective technique, binary search has a temporal complexity of O(log n), where n is the number of entries in the array. Due to this, binary search is much quicker than linear search, which has an O(n) time complexity. This is especially true for large datasets. As binary search divides the search space in half continually, it eliminates half of the remaining elements with each iteration, making the search process noticeably faster.

Conclusion

Searching for particular elements within sorted arrays is made simple and efficient using binary search methods in C++. Binary search ensures an effective search method with a time complexity of O(log n) by splitting the search space in half with each iteration. Understanding that data must be sorted before the binary search method can be applied is essential. Binary search may substantially accelerate searching operations and improve the overall performance of your C++ programs when used properly. Greetings from the code!

Introduction to the While Statement in C++

3 thoughts on “Binary Search Algorithm in C++”

  1. Very nice post. I simply stumbled upon your blog and wished to mention that I’ve truly loved browsing your
    weblog posts. After all I’ll be subscribing for your feed and I’m
    hoping you write again soon!

    Reply
  2. I’m not that much of a internet reader to be honest but your blogs really nice, keep it up!
    I’ll go ahead and bookmark your website to come back later
    on. All the best

    Reply

Leave a Comment