Pages

Friday 6 September 2013

BINARY SEARCH



#include<stdio.h>
void main()
 {
  int a[100];
  int i,j,n,temp,s,top,bottom,mid;
  printf("\n Enter the Limit  :  ");
  scanf("%d",&n);
  printf("\n Enter the %d Elemets......\n",n);
  for(i=0;i<n;i++)
   scanf("%d",&a[i]);
  printf("\n Enter the Searching Element  :  ");
  scanf("%d",&s);
 for(i=0;i<n-1;i++)
  {
   for(j=i+1;j<n;j++)
    {
     if(a[i]>a[j])
      {
       temp=a[i];
       a[i]=a[j];
       a[j]=temp;
      }
    }
  }
 if(n%2==0)
 {
 top=n+1;
 bottom=0;
 }
 else
  {
    top=n;
    bottom=0;
 }

 do
  {
   mid=(bottom+top);
   if(s<a[mid])
    {
     top=mid-1;
    }
   else
    {
     bottom=mid+1;
    }
  }while((s!=a[mid])&&(top>=bottom));
 if(s==a[mid])
   printf("\n Se Searching element is found  \n");
 else
   printf("\n Searching Element is Not found  \n");
 }





No comments:

Post a Comment