clanguage07@gmail.com

clanguage07@gmail.com

Saturday, June 16, 2012

binary search

#include<stdio.h>
#include<conio.h>
#define SIZE 10
int found=0;
int binarysearch(int [],int);
void main()
{
  int list[SIZE]={22,36,45,65,59,48,21,54,77,91};
  int target,res;
  clrscr();
  printf("\n\t enter target:");
  scanf("%d",&target);
  res=binarysearch(list,target);
  if(found)
  printf("\n\t the target is found at location:%d",res);
  else
  printf("\n\t the target is not found");
  getch();
}
int binarysearch(int list[],int target)
{
     int low,mid,high;
     low=0;
     high=SIZE;
     while(low<=high)
      {
      mid=(low+high)/2;
      if(target<list[mid])
      high=mid-1;
      else
      if(target>list[mid])
      low=mid+1;
      else
    {
        found=1;
        break;
    }
      }
       return(mid);
}

No comments:

Post a Comment

GET MORE INFORMATION

http://ads.qadservice.com/t?id=c2168e05-8974-4816-872a-91936ff7379d&size=1024x768&drct=true