You are on page 1of 1

(BINARY SEARCH) Case 1: If array is sorted in Ascending order /*Initialise segment variables*/ 1. Set beg=L, last=U 2.

Repeat steps 3 through 6 UNTIL beg>last 3. mid=INT((beg+last)/2) 4. if AR[mid]==ITEM then { print "Search Successful" print ITEM, "found at", mid break } 5. if AR[mid]<ITEM then beg=mid+1 6. if AR[mid]>ITEM then last=mid-1 /* End of Repeat*/ 7. if beg!=last print "Unsuccessful Search" 8. END. Case 2: If array is sorted in Descending Order /*Initialise */ : if AR[mid]==ITEM then { : } if AR[mid]<ITEM then last=mid-1 if AR[mid]>ITEM then beg=mid+1 : Rest is similar to the algorithm in Case 1

You might also like