0% found this document useful (0 votes)
5 views5 pages

Saad Lab 8

The document outlines the binary search algorithm, which involves selecting the middle item of a sorted array and comparing it with the key value to be searched. If the key is not found, the search continues in the appropriate sub-array until the key is located or the sub-array size becomes one. The pseudocode provided details the procedure for implementing the binary search, including setting bounds and conditions for exiting the search.

Uploaded by

multiplecrown3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Saad Lab 8

The document outlines the binary search algorithm, which involves selecting the middle item of a sorted array and comparing it with the key value to be searched. If the key is not found, the search continues in the appropriate sub-array until the key is located or the sub-array size becomes one. The pseudocode provided details the procedure for implementing the binary search, including setting bounds and conditions for exiting the search.

Uploaded by

multiplecrown3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

BINARY SEARCH:

ALGORITHM:

Step 1 − Select the middle item in the array and compare it with the key
value to be searched. If it is matched, return the position of the median.

Step 2 − If it does not match the key value, check if the key value is either
greater than or less than the median value.

Step 3 − If the key is greater, perform the search in the right sub-array;
but if the key is lower than the median value, perform the search in the left
sub-array.

Step 4 − Repeat Steps 1, 2 and 3 iteratively, until the size of sub-array


becomes 1.

Step 5 − If the key value does not exist in the array, then the algorithm
returns an unsuccessful search.

PSEUDOCODE:

Procedure binary_search

A ← sorted array

n ← size of array

x ← value to be searched

Set lowerBound = 1

Set upperBound = n
while x not found

if upperBound < lowerBound

EXIT: x does not exist.

set midPoint = lowerBound + ( upperBound - lowerBound ) / 2

if A[midPoint] < x

set lowerBound = midPoint + 1

if A[midPoint] > x

set upperBound = midPoint - 1

if A[midPoint] = x

EXIT: x found at location midPoint

end while

end procedure
PROGRAM:
OUTPUT:

You might also like