0% found this document useful (0 votes)
26 views

Binary Search

The document describes binary search in C++. It includes a program that demonstrates how to implement binary search to find an element in a sorted array. The binarySearch() function takes an array, lower and upper bounds, and the target number. It recursively searches half the array at a time by comparing the midpoint to the target until it is found or the array is empty. The main() function defines a sample array, calls binarySearch() to find the index of a number, and prints whether it was found.

Uploaded by

Dweight Haward
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Binary Search

The document describes binary search in C++. It includes a program that demonstrates how to implement binary search to find an element in a sorted array. The binarySearch() function takes an array, lower and upper bounds, and the target number. It recursively searches half the array at a time by comparing the midpoint to the target until it is found or the array is empty. The main() function defines a sample array, calls binarySearch() to find the index of a number, and prints whether it was found.

Uploaded by

Dweight Haward
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Example

Live Demo

#include<iostream>

using namespace std;

int binarySearch(int arr[], int p, int r, int num) {

if (p <= r) {

int mid = (p + r)/2;

if (arr[mid] == num)

return mid ;

if (arr[mid] > num)

return binarySearch(arr, p, mid-1, num);

if (arr[mid] > num)

return binarySearch(arr, mid+1, r, num);

return -1;

int main(void) {

int arr[] = {1, 3, 7, 15, 18, 20, 25, 33, 36, 40};

int n = sizeof(arr)/ sizeof(arr[0]);

int num = 33;

int index = binarySearch (arr, 0, n-1, num);

if(index == -1)

cout<< num <<" is not present in the array";

else

cout<< num <<" is present at index "<< index <<" in the


array";
return 0;

Output
33 is present at index 7 in the array

Binary Search in C++


C++ProgrammingServer Side Programming

Binary Search is a method to find the required element in a sorted array by repeatedly halving the
array and searching in the half.
This method is done by starting with the whole array. Then it is halved. If the required data value is
greater than the element at the middle of the array, then the upper half of the array is considered.
Otherwise, the lower half is considered. This is done continuously until either the required data value
is obtained or the remaining array is empty.
A program that demonstrates binary search in C++ is given below.

Example
Live Demo

#include<iostream>

using namespace std;

int binarySearch(int arr[], int p, int r, int num) {

if (p <= r) {

int mid = (p + r)/2;

if (arr[mid] == num)

return mid ;

if (arr[mid] > num)

return binarySearch(arr, p, mid-1, num);


if (arr[mid] > num)

return binarySearch(arr, mid+1, r, num);

return -1;

int main(void) {

int arr[] = {1, 3, 7, 15, 18, 20, 25, 33, 36, 40};

int n = sizeof(arr)/ sizeof(arr[0]);

int num = 33;

int index = binarySearch (arr, 0, n-1, num);

if(index == -1)

cout<< num <<" is not present in the array";

else

cout<< num <<" is present at index "<< index <<" in the array";

return 0;

Output
33 is present at index 7 in the array

In the above program, binarySearch() is a recursive function that is used to find the required element
in the array using binary search. The function takes the array, its lower bound and upper bound as
well as the number to be found as parameters. This is shown below.

int binarySearch(int arr[], int p, int r, int num)

Then the midpoint of the array is calculated. If the value at the midpoint is equal to num, then it is
returned. If the value is greater than num, then array calls itself recursively with the lower bound p
and upper bound mid-1. If the value is less than num, then array calls itself recursively with the lower
bound mid+1 and upper bound r. This can be seen by the following code snippet.

int binarySearch(int arr[], int p, int r, int num) {

if (p <= r) {

int mid = (p + r)/2;

if (arr[mid] == num)
return mid ;

if (arr[mid] > num)

return binarySearch(arr, p, mid-1, num);

if (arr[mid] < num)

return binarySearch(arr, mid+1, r, num);

return -1;

In the main() function, the array arr[] is defined. The size of the array is calculated and the number to
be found is specified. Then binarySearch() is called to find the index of the number. If the value
returned by binarySearch() is -1, then the number is not in the array. Otherwise the index value is
returned. This is given below.

int main(void) {

int arr[] = {1, 3, 7, 15, 18, 20, 25, 33, 36, 40};

int n = sizeof(arr)/ sizeof(arr[0]);

int num = 33;

int index = binarySearch (arr, 0, n-1, num);

if(index == -1)

cout<< num <<" is not present in the array";

else

cout<< num <<" is present at index "<< index <<" in the array";

return 0;

You might also like