Binary Search in C++


Prev Tutorial Next Tutorial

Binary Search Program in C++

This searching technique is applicable only for sorted array, but this searching technique is faster than linear search.

Steps for binary search

You need to first sort elements of array if it is not in sorted order, because binary search is only application on sorted element.

binary search in c++
  • First find the middle element of the array.
  • Compare the middle element with the item. After this step there are three cases;
  1. If middle element is a desired item then search is successful
  2. If middle element is less than desired item then search only the first half of the array.
  3. If middle element is grater than the desired item search in the second half of the array.
  4. Repeat same steps until element is found.
binary search step

Binary Search Program in C++


void main()
int i, first, last, middle, n, search, array[100];
cout<<"Enter size of array to enter elements\n";

cout<<"Enter" <<n <<"elements\n";

for (i = 0; i < n; i++)

<<n"Enter element to find\n";

first = 0;
last = n - 1;
middle = (first+last)/2;

while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
cout<<search <<" found at location \n" <<middle+1;
last = middle - 1;

middle = (first + last)/2;
if (first > last)
cout<<search <<" is not present in the list.\n";
binary search
Prev Tutorial Next Tutorial


Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: or 9999595223

Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements

Buy Websites 9999595223
ptz cctv camera

PTZ CCTV Wifi IP Bullet Camera RS: 1999. Contact no. 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: or 9999595223 Try this Keyword C++ Programs