Skip to content
Home » Blog » C++ Program to implement binary search

C++ Program to implement binary search

C++ Program to implement binary search

In this program, you’ll learn how to implement a binary search tree.

The working of Binary Search:

In a binary search, it looks for a particular item by comparing the middlemost item of the collection. If a match is found, then the index of an item is returned. If the middle item is greater than the item, then the item is searched in the sub-array to the left of the middle item.

Otherwise, the item is searched for in the sub-array to the right of the middle item. This process continues on the sub-array as well until the size of the subarray reduces to zero.

Program to implement binary search

 using namespace std;

  // Prototypes of Functions
  void bubbleSort(int array[], int size);
  bool binarySearch(int array[], int size,int key);

  int main(){
      cout<<"Enter any 5 numbers : "<<endl;

      // Size can be change by replacing 5
      int array[5]; //Declaring array
     for(int i=0; i<5; i++)
       cout<<"\t";  cin>>array[i]; // Initializing array

      //Passing Arrary for Sorting

    // Array has Sorted At This Point
    cout<<"\n\t\t\tEnter no to be searched : ";
    int key;

//Passing Array, size and key To Search Key
int result=binarySearch(array,5,key);

cout<<"\n\t\t\tNumber found in array "<<endl;
cout<<"\n\t\t\tNumber not found in array "<<endl;

return 0;

void bubbleSort(int array[], int size){
      cout<<"  Input array is: "<<endl;
      for(int j=0; j<size; j++)
       //Displaying Array
       cout<<"\t\t\tValue at "<<j<<" Index: "<<array[j]<<endl;
    // Bubble Sort Starts Here
     int temp;
     for(int i2=0; i2<size; i2++)
     for(int j=0; j<size-1; j++)
        //Swapping element in if statement
   // Displaying Sorted array
      cout<<"  Sorted Array is: "<<endl;
     for(int i3=0; i3<size; i3++)
    cout<<"\t\t\tvalue at "<<i3<<" index is : "<<array[i3]<<endl;
}// Sort Function Ends Here

bool binarySearch(int array[],int size, int key){
         int start=1, end=size;
         int mid=(start+end)/2;

     }// While Loop End

    return true; //Returnig to main
   return false;//Returnig to main

}// binarySearch Function Ends Here


Enter any 5 numbers:
Input array is:
		value at 0 index is : 5
		value at 1 index is : -17
		value at 2 index is : 15
		value at 3 index is : 3
		value at 4 index is : 0
sorted array is:
		value at 0 index is : -17
		value at 1 index is : 0
		value at 2 index is : 3
		value at 3 index is : 5
		value at 4 index is : 15

Enter no to be searched : 15

Number found in array

Time Complexity for Selection Sort

Best-case performanceO(1)
Average performanceO(log n)
Worst-case space complexityO(1)

Related Programs

  1. C++ Program for Selection Sort
  2. C++ Program to Multiply two Numbers
  3. C++ Program to Check Whether Number is Even or Odd
  4. C++ Program to Find the Largest Number Of Three Numbers
  5. C++ Program to Find All Roots of a Quadratic Equation
  6. C++ Program to check whether a number can be express as Sum of Two Prime Numbers.
  7. C++ Program to check Amstrong Number.
  8. C++ Programs To Create a Pyramid and Pattern
  9. C++ Program to Check Whether a Number is Prime or Not
  10. C++ Program to display Amstrong Number between Two intervals.

Ask your questions and clarify your/other doubts on the program to implement a binary search by commenting. Documentation.