Saad Lab 8
Saad Lab 8
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 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 A[midPoint] < x
if A[midPoint] > x
if A[midPoint] = x
end while
end procedure
PROGRAM:
OUTPUT: