Binary Search in C++ Standard Template Library (STL) - GeeksforGeeks

17 Feb, 2021
Binary search is a widely used searching algorithm that requires the array to be sorted before search is applied. The main idea behind this algorithm is to keep dividing the array in half (divide and conquer) until the element is found, or all the elements are exhausted.It works by comparing the middle item of the array with our target, if it matches, it returns true otherwise if the middle term is greater than the target, the search is performed in the left sub-array. If the middle term is less than the target, the search is performed in the right sub-array.  The prototype for binary search is : 
binary_search(startaddress, 
              endaddress, valuetofind)
Parameters :
startaddress: the address of the first 
              element of the array.
endaddress: the address of the next contiguous 
            location of the last element of the array.
valuetofind: the target value which we have 
             to search for.
Returns :
true if an element equal to valuetofind is found, else false.
CPP
// CPP program to implement// Binary Search in// Standard Template Library (STL)#include <algorithm>#include <iostream>  using namespace std;  void show(int a[], int arraysize){    for (int i = 0; i < arraysize; ++i)        cout << a[i] << ",";}  int main(){    int a[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };    int asize = sizeof(a) / sizeof(a[0]);    cout << "\nThe array is : \n";    show(a, asize);      cout << "\n\nLet's say we want to search for ";    cout << "\n2 in the array So, we first sort the array";    sort(a, a + asize);    cout << "\n\nThe array after sorting is : \n";    show(a, asize);    cout << "\n\nNow, we do the binary search";    if (binary_search(a, a + 10, 2))        cout << "\nElement found in the array";    else        cout << "\nElement not found in the array";      cout << "\n\nNow, say we want to search for 10";    if (binary_search(a, a + 10, 10))        cout << "\nElement found in the array";    else        cout << "\nElement not found in the array";      return 0;}
The array is : 
1,5,8,9,6,7,3,4,2,0,

Let's say we want to search for 
2 in the array So, we first sort the array

The array after sorting is : 
0,1,2,3,4,5,6,7,8,9,

Now, we do the binary search
Element found in the array

Now, say we want to search for 10
Element not found in the array
YouTubeGeeksforGeeks500K subscribersC++ Programming Language Tutorial | Searching & Sorting in an Array using STL | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:001:05 / 2:04•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=QTjc0HYfjYg" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
Related Article: std::bsearch in C++ Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
ChandanaAkriti
jazymatricx
ss801101
Binary Search
cpp-algorithm-library
STL
C++
STL
Binary Search
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Constructors in C++
C++ Classes and Objects
Socket Programming in C/C++
Operator Overloading in C++
Copy Constructor in C++
rand() and srand() in C/C++
vector erase() and clear() in C++
Virtual Function in C++
Templates in C++ with Examples
unordered_map in C++ STL