Binary Search Algorithm:

Binary search algorithm is used to search for an element in a already sorted list. The value of the element in the middle of the list is compared with the value of the element to be searched for . If the middle element is larger, the desired element has to be in the upper of the list. If the middle element is smaller, the desired element has to be lower half of the list. The number of elements to be searched is reduced by half in every iteration.

Algorithm for Binary Search:
Step 1: low = 0
Step 2 : high = n – 1
Step 3 : loc = -1
Step 4 : while ( low < = high ) 
do
Step 5 : middle = ( low + high ) / 2
Step 6 : If (element = array[middle ] ) 
Step 7: loc = middle      
then goto Step 12
[ end if ]
Step 8 : If (element < array[middle] ) 
Step 9: high ← middle – 1
Step 10 : else
Step 11 : low ← middle + 1
[ end of if ]
[ end of while loop ]
Step 12 : if ( loc > = 0 ) 
Step 13: print “ element found in the list, search Successful ” , loc.

Step 14: else
Step 15 : print “ Item not found in the list, search Unsuccessful ”.
[ end if ]
Step 16 : Stop.


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: