
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 Functions in C++ STL: Lower Bound and Upper Bound
Binary search is a search algorithm that searches for an element by comparing it with the middle value of the array and dividing it based on the value. The algorithm does this repeatedly until the element is found.
The array should be sorted in order to apply a binary search to it.
The time complexity of the binary search is of logarithmic order. That's why it is very important for programmers to know shorthands also related to binary search along with its implementations to reduce the time to code the algorithm. Functions related to the binary search algorithm included in the standard template library(STL) are discussed here.
lower_bound − The lower bound search returns the position where the element is found.
Syntax
lower_bound(start_pointer , end_pointer, element )
Here,
start_pointer is the pointer that holds the memory location of the starting point of the search structure.
end_pointer is a pointer that holds the memory location of the endpoint of the search structure.
element is the element that is to be found using the function.
The function returns the index of the element which is to be found.
The return value may take multiple values −
If the element occurs once in the structure, the position is returned.
If the element occurs more than once in the structure, the position of the first element is returned.
If the element does not occur in the structure, the position of the next higher number than an element is returned.
The index of any element is found by subtracting the base position of the structure.
Example
#include<bits/stdc++.h> using namespace std; int main(){ vector<int> sortedarray = {2 , 5, 7, 8 , 15, 20 }; vector<int> sortedarray2 = {2, 3, 4, 6 , 9 , 23 }; vector<int> sortedarray3 = {2 , 5, 7, 7 , 15, 20 }; cout<<"The position of element 7 found using lower_bound function :"; cout<<"\nCase 1 : When element is present in array but only once "; cout<<lower_bound(sortedarray.begin() , sortedarray.end(), 7) - sortedarray.begin(); cout<<"\nCase 2 : When element is present more than one times in the array "; cout<<lower_bound(sortedarray3.begin() , sortedarray3.end(), 7) - sortedarray3.begin(); cout<<"\nCase 3 : When element is not present in the array "; cout<<lower_bound(sortedarray2.begin() , sortedarray2.end(), 7) - sortedarray2.begin(); }
Output
The position of element 7 found using lower_bound function : Case 1 : When element is present in array but only once 2 Case 2 : When element is present more than one times in the array 2 Case 3 : When element is not present in the array 4
upper_bound − The upper bound search returns the position of the element that is higher than the passed element.
Syntax
upper_bound(start_pointer , end_pointer, element)
Here,
start_pointer is the pointer that holds the memory location of the starting point of the search structure.
end_pointer is a pointer that holds the memory location of the endpoint of the search structure.
element is the element that is to be found using the function.
The function returns the index of the element whose value is greater than the value of the element.
The return value may take multiple values −
If the element occurs once in the structure, position next higher element is returned.
If the element occurs more than once in the structure, the position of the last element’s next element is returned.
If the element does not occur in the structure, the position of the next higher number than element is returned.
The index of any element is found by subtracting the base position of the structure.
Example
#include<bits/stdc++.h> using namespace std; int main(){ vector<int> sortedarray = {2 , 5, 7, 8 , 15, 20 }; vector<int> sortedarray2 = {2, 3, 4, 6 , 9 , 23 }; vector<int> sortedarray3 = {2 , 5, 7, 7 , 15, 20 }; cout<<"The position of element 7 found using upper_bound function :"; cout<<"\nCase 1 : When element is present in array but only once "; cout<<upper_bound(sortedarray.begin() , sortedarray.end(), 7) - sortedarray.begin(); cout<<"\nCase 2 : When element is present more than one times in the array "; cout<<upper_bound(sortedarray3.begin() , sortedarray3.end(), 7) - sortedarray3.begin(); cout<<"\nCase 3 : When element is not present in the array "; cout<<upper_bound(sortedarray2.begin() , sortedarray2.end(), 7) - sortedarray2.begin(); }
Output
The position of element 7 found using upper_bound function : Case 1 : When element is present in array but only once 3 Case 2 : When element is present more than one times in the array 4 Case 3 : When element is not present in the array 4
binary_search is a function that checks whether the element is present in the structure or not.
Syntax
binary_search(start_pointer , end_pointer, element)
Here,
start_pointer is the pointer that holds the memory location of the starting point of the search structure.
end_pointer is a pointer that holds the memory location of the endpoint of the search structure.
element is the element that is to be found using the function.
If the Element is present in the structure, function return true. Else, it will return false.
Example
#include<bits/stdc++.h> using namespace std; int main(){ vector<int> sortedarray = {6, 15, 21, 27, 39, 42}; cout<<"The element to be found in the array is 21\n" ; if(binary_search(sortedarray.begin(), sortedarray.end(), 21)) cout<<"The element is found"; else cout<<"The element is not found"; cout<<"\nThe element to be found in the array is 5\n" ; if(binary_search(sortedarray.begin(), sortedarray.end(), 5)) cout<<"The element is found"; else cout<<"The element is not found"; }
Output
The element to be found in the array is 21 The element is found The element to be found in the array is 5 The element is not found