Featured Post

Thursday, October 13, 2016

Linear Search and Binary Search

Linear Search:
                    In linear search we search for an element from first to last element in a array. There is no consideration of arrangement of elements in array. For example if  'a' is an element of length 10 then we search from a[0] to a[9] to find the required element. When the element is found we break the searching process.

/* C Program to implement linear search */

main()
{
   int a[100],ele,i,c=0,n;
   printf("\n enter your range");
   scanf("%d",&n);
   printf("\n enter your elements");
   for(i=0;i<n;i++)
   {
        scanf("%d",&a[i]);
   }
   printf("\n enter the element to be searched");
   scanf("%d",&ele);
   for(i=0;i<n;i++)
   {
      if(a[i]==ele)/* Comparing each and every value in array with search element */
       {
              c=1;
              printf("\n element is fount at location %d",i+1);
              break;
        }
    }
   if(c==0)
   printf("\n element is not found");
 }

OUTPUT:
  enter your range  
  10
  enter your elements
  10 20 30 40 50 67 78 89 90 99
  enter the element to be searched 78
  element is found at location 7

Binary search:
     Using Binary search we search for an element in an array of elements. To implement binary search, the list of elements in an array must be arranged in ascending order or descending order. In binary search we calculate mid position and compare the value at mid position with requires search position. Accordingly the first and last positions of the array are adjusted until the search element is found at mid position or first position crosses last position(search fail).

/* C program to implement Binary Search */

main()
{
   int a[100],n,ele,i,first, last,mid,c=0;
   printf("\n enter your range");
   scand("%d",&n);
   printf("\n enter your elements");
   for(i=0;i<n;i++)
   {
      scanf("%d",&a[i]);
    }
    printf("\n enter the element to be searched");
    scanf("%d",&ele);
   first=0;
   last=n-1;
   while(first<=last)
   {
       mid=(first+last)/2;
       if(a[mid]==ele)
        {
            c=1;
            printf("\n element is found at location %d",mid+1);
            break;
         }
        else if(a[mid]<ele)  /* element is in right hand side of mid position */
         {
            first=mid+1;
          }
        else  /* element is in left hand size of mid position */
           last=mid-1;
    }
       if(c==0)
       printf("\n element is not found");
}

OUTPUT:
   enter your range
    10
  enter your elements
  10 20 30 40 50 60 70 80 90 100
  enter the element to be searched 
  50
  element is found at location 5


                                      Functions

linear search, binary search, hashing, searching , sorting, biggest and smallest element in an array, bubble sort, insertion sort, selection sort, bucket sort. initialisation of one dimensional array, two dimensional array.matrix addition , matrix multiplication, symmetric matrix.

               
 











No comments:

Post a Comment