- It is another search technique to search for an element in the array.
- In binary search, every time we find the mid element and compare with the element to be searched.
- If element matches, we return mid location index as element index.
- If the element is less than the mid location element, search continues only in left part of mid location.
- If the element is higher than the mid location element, search continues in the right side part of array from mid location.
- This process continues until element search.
- Note: We can apply binary search only on sorted array.
int a, n, i, key, found=0, low, mid, high;
printf(“Enter length of elements : “);
printf(“Enter %d elements : \n”, n);
for(i=0 ; i<n ; i++)
printf(“Enter element to be searched :”);
printf(“Found @ loc : %d \n”, mid);
printf(“Element not found \n”);
Two way serach algorithm:
|int TwoWayLinearSearch(arr, key, n);|
if(a[p]!=k or a[q]!=k)
then p<-p+1 ; q<-q-1 ;
then found<-1; //end while