Binary Searching in An Array
Binary search is much faster than Linear search:
• It uses the technique 'divide and conquer'
• array is divided into two half.
• now the desired element is presented in first half or second half of the element
• We search until we found the element.
****For using Binary Search: Array should be sorted in advance.
• Suppose we want to search value 14 and it position.
• We have to find the middle position of array.
(Lb+Ub)/2=(9+1)/2=5
• The value at 5 location is 11• As we are searching 14 so we will compare middle value to our searching value in order to decide in which half of Array we have to move.
11 is less than 14.
Mean search element is in second half that mean after 5th element.• This indicates that searching element with between series[6] series [9] • Now again we will find middle value
(6+9)/2=7
• Compare series[7] element to 1414=14 where we find out our value
So the position is series[7]= 7th place.Algorithm for Binary Searching in An Array
steps
• 1: First=1; Last=N; POS=0; flag=False
• 2. While(first<= last and(flag=false) perform step 3 to 5.
• 3. Middle =(first+ last) div 2
• 4. If(series [Middle]=('x') then [Pos=Middle; flag=true]
• 5. If (series[Midle]<('X') then first=Middle+1;
else Last=Middle-1;
• 6.if(Flag=true) then print pos
else print ' unsuccessful search'
• 7. End
12- Array- Binary Search-Algorithm- DataStructures
CLICK HERE TO Download This PDF NOTES