Professional Documents
Culture Documents
Binary Search i-WPS Office
Binary Search i-WPS Office
Binary Search i-WPS Office
Binary search is an efficient algorithm for finding an item from a sorted list of items. It works by
repeatedly dividing in half the portion of the list that could contain the item, until you've narrowed
down the possible locations to just one.
In computer science, binary search, also known as half-interval search, logarithmic search, or binary
chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search
compares the target value to the middle element of the array.
We will discuss the binary search in the C++ programming language. Binary search is a mechanism used
to find the given elements from the sorted array by continuously halving the array and then searching
specified elements from a half array. And the process goes on till the match is found. It works only the
sorted data structures. The time complexity of the binary search algorithm is O (log n).
Note: To perform a binary search technique in C++, a programmer or user should ensure the given array
must be sorted either be in ascending or descending order.
beg = 0;
end = size - 1;
/* if the specified element is found at mid index, terminate the process and return the index. */
If (aarr[mid] == num)
{
return mid + 1;
End = mid - 1;
Beg = mid + 1;
Return -1;
Step 1: Declare the variables and input all elements of an array in sorted order (ascending or
descending).
Step 3: Now compare the target elements with the middle element of the array. And if the value of the
target element is matched with the middle element, return the middle element's position and end the
search process.
Step 4: If the target element is less than the middle element, we search the elements into the lower half
of an array.
Step 5: If the target element is larger than the middle element, we need to search the element into the
greater half of the array.
Step 6: We will continuously repeat steps 4, 5, and 6 till the specified element is not found in the sorted
array.
Example 1: Program to find the specified number from the sorted array using the binary search
Let's write a program to find the specified number from a sorted array using the binary search in the C++
programming language.
#include <iostream>
#include <conio.h>
int main ()
cout << " Define the size of the array: " << endl;
cout << " Enter the values in sorted array either ascending or descending order: " << endl;
st = 0;
cout << " Define a value to be searched from sorted array: " << endl;
cin >> tgt;
// use while loop to check 'st', should be less than equal to 'end'.
mid = ( st + end ) / 2;
/* if we get the target value at mid index, print the position and exit from the program. */
if (arr[mid] == tgt)
cout << " Element is found at index " << (mid + 1);
// check the value of target element is greater than the mid element' value
// check the value of target element is less than the mid element' value
}
cout << " Number is not found. " << endl;
return 0;
Output
10
Arr [0] = 12
Arr [1] = 24
Arr [2] = 36
Arr [3] = 48
Arr [4] = 50
Arr [5] = 54
Arr [6] = 58
Arr [7] = 60
Arr [8] = 72
Arr [9] = 84
50
ABSAR A. ARIP
AMINKADRA S. FAIZAL
ALWYN A. JUMMAHARI
SAMWILL IRAN
AZIZ K. JUMAH
Group name