Write an algorithm to search an element using Binary Search Algorithm.
Algorithm for Binary Search algorithm steps:
Step 1: Low = 0
Step 2 : High = N – 1
Step 3 : LOC = -1
Step 4 : While ( Low < = High ) Do
Step 5 : M = ( Low + High ) / 2
Step 6 : If (ELEMENT = A[M ] ) Then
Step 7: LOC = M goto Step 12
[ end if ]
Step 8 : If (ELEMENT < A [M ] ) Then
Step 9: High ← M – 1
Step 10 : Else
Step 11 : Low ← M + 1
[ end of if ]
[ end of While loop ]
Step 12 : if ( LOC > = 0 ) Then
Step 13: Print “ ELEMENT , found , search successful ” , LOC
Step 14: else
Step 15 : Print “ Item not found , search Unsuccessful ”
[ end if ]
Step 16 : Stop
Thanks
Mukesh Rajput
Post A Comment:
0 comments: