Binary Search In Array

//Binary Search.

#include<stdio.h>
main()
{
int a[5],i,high=4,low=0,mid,x,flag=1;
 printf("Enter array\n");
 for(i=0;i<5;i++)
 scanf("%d",&a[i]);
 printf("Enter element to search\n");
 scanf("%d",&x);
 while(low<=high&&flag==1)
 {
  mid=high+low/2;
  
  if(x<a[mid])
  high=mid-1;
  else if(x>a[mid])
  low=mid+1;
  else
  {
  flag=0;
  mid=mid+1;
  printf("Data found at = %d Positon",mid);
     }   
 }
 if(flag==1)
 {
  printf("Data Not found\n");
 }
}

No comments:

Post a Comment