Sunday, 12 March 2017

C Program for Implementation of Binary Search

Binary Search is also known as logarithmic search and half-interval search. Unlike Linear search, it works on sorted arrays or list. Binary search begins by comparing the middle element of the array with the target value. If the target value (element to be found) matches the middle element, its position in the array is returned. If the target value is less than or greater than the middle element, the search continues in the lower or upper half of the array, respectively, eliminating the other half from consideration.

Time complexity of Binary Search is O(log n) where n is the number of elements in the array.

For example:

      arr[]=  { 24, 45, 67, 89, 90, 92, 94, 96 }
      Let the element to be found = x = 45
      Now,
                  end = 8
                  beg = 1
                  mid = (beg+end)/2 =  4 (floor value)
     Now, we will check if arr[3]=x. Since, arr[4]=89 which is greater than
     Therefore we will update end and now end will become 3 (end=mid - 1). Now we will check for the
     target element in the first half and second half of the array will be eliminated.

     Now in first half, mid=(beg+end)/2 = (1+3)/3 = 2 . Since arr[mid] = arr[2] = 45= x, it will return true.

             

ALGORITHM:
   
set: beg=1, set: end=n   /*n is the number of elements in the array */
set: mid=(beg+end)/2
If element<arr[beg] or element>arr[end]:
      return false
Else:
      do While beg<=end:
           If arr[mid]=element:
                   return true
           Else:
                   If arr[mid]>element:
                        set: end=mid-1
                   Else:
                        set: beg=mid+1
                   End IfElse
           End IfElse
      End While loop
End IfElse


Program:
 
#include <stdio.h>
#define MAX 100
int bin_search(int arr[],int n,int element);
int main()
{
    int arr[MAX],n,i,element,location;
    printf("\nEnter the number of elements: ");
    scanf("%d",&n);
    printf("\nEnter the elements of the array: \n");
    for(i=0;i<n;i++)
    {
          scanf("%d",&arr[i]);
    }
    printf("\nEnter the element you want to search in the array: ");
    scanf("%d",&element);
       location=bin_search(arr,n,element);
       if(location==-1)
       {
             printf("\nElement is not present in the array !");
       }
       else
       {
             printf("\nElement %d is present at index %d",element,location);
       }
       return 0;
}
int bin_search(int arr[],int n,int element)
{
    int beg=0,mid,end=n-1;
    mid=(beg+end)/2;
    if(element>arr[end] || element<arr[beg])
    {
          return -1;
    }
    while(beg<=end)
    {
            if(arr[mid]==element)
            {
                   return mid;
            }
            else
            {
                   if(arr[mid]>element)
                   {
                        end=mid-1;
                   }
                   else
                   {
                        beg=mid+1;
                   }
                   mid=(beg+end)/2;
            }
    }
    return -1;
}

OUTPUT:


See also: Implementation of Binary Search in Python 


EmoticonEmoticon