SHARE
TWEET

Binary search

a guest Oct 12th, 2017 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<stdio.h>
  2. int main()
  3. {
  4.     int a[1000],n,x,i,m,l,h;
  5.     printf("How many no =");
  6.     scanf("%d",&n);
  7.     for(i=0;i<n;i++)
  8.         scanf("%d",&a[i]);
  9.     printf("which no do u search=");
  10.     scanf("%d",&x);
  11.     l=a[0];
  12.     h=a[n-1];
  13.     while(l<=h){
  14.         m=(l+h)/2;
  15.         if(a[m]==x){
  16.         printf("'Element found'index no= %d",m+1);
  17.         break;}
  18.         else if(a[m]>x)
  19.             h=m-1;
  20.         else if(a[m]<x)
  21.             l=m+1;
  22.     }
  23. }
RAW Paste Data
Top