Binary Search i-WPS Office

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

BINARY SEARCH

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.

Binary Search in C++

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).

Binary Search in C++

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.

Algorithm of the binary search in C++

Following is the algorithm to perform the binary search in C++

beg = 0;

end = size - 1;

while ( beg <= end)

// calculate mid value

mid = (beg + end) / 2;

/* if the specified element is found at mid index, terminate the process and return the index. */

// Check middle element is equal to the defined element.

If (aarr[mid] == num)

{
return mid + 1;

else if (arr[mid] > num)

End = mid - 1;

Else if (arr [mid] < num)

Beg = mid + 1;

// If the element does not exist in the array, return -1.

Return -1;

Steps to perform the binary search in C++

Step 1: Declare the variables and input all elements of an array in sorted order (ascending or
descending).

Step 2: Divide the lists of array elements into halves.

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>

using namespace std;

int main ()

// declaration of the variables and array

int arr[100], st, mid, end, i, num, tgt;

cout << " Define the size of the array: " << endl;

cin >> num; // get size

// enter only sorted array

cout << " Enter the values in sorted array either ascending or descending order: " << endl;

// use for loop to iterate values

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

cout << " arr [" << i << "] = ";

cin >> arr[i];

// initialize the starting and ending variable's values

st = 0;

end = num - 1; // size of array (num) - 1

// define the item or value to be search

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'.

while ( st <= end)

// get middle value by splitting into half

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);

exit (0); // use for exit program the program

// check the value of target element is greater than the mid element' value

else if ( tgt > arr[mid])

st = mid + 1; // set the new value for st variable

// check the value of target element is less than the mid element' value

else if ( tgt < arr[mid])

end = mid - 1; // set the new value for end variable

}
cout << " Number is not found. " << endl;

return 0;

Output

Define the size of the array:

10

Enter the values in sorted array either ascending or descending order:

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

Define a value to be searched from sorted array:

50

Element is found at index 5


Republic of the Philippines

Mindanao State University-Sulu

COLLEGE OF COMPUTER STUDIES


Site, Jolo, Sulu

SCIENCE, TECHNOLOGY, AND SOCIETYS


ubject

ABSAR A. ARIP

AMINKADRA S. FAIZAL

ALWYN A. JUMMAHARI

SAMWILL IRAN

AZIZ K. JUMAH
Group name

2ND YEAR BSIT-A


Year & section

You might also like