C Program For Searching element in the list_Binary Search

#include<stdio.h>
#include<conio.h>
void main()
{
int i,a[10],n,ele,low,high,mid;
clrscr();
printf("enter the value of n: ");
scanf("%d",&n);
printf("\nenter the values in sorted order only");
for(i=0;i<n;i++)
scanf("%d",&a[i]);

printf("\nelements inserted are:\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
low=0;
high=n-1;
printf("\nenter element to be searched:\n");
scanf("%d",&ele);

while(low<=high)
{
 mid=(low+high)/2;
 if(a[mid]==ele)
 {
  printf("element is found");

  break;
 }
 else if(ele<a[mid])
  high=mid-1;
 else if (ele>a[mid])
  low=mid+1;


}

getch();
}


Bhanu Namikaze

Bhanu Namikaze is an Ethical Hacker, Security Analyst, Blogger, Web Developer and a Mechanical Engineer. He Enjoys writing articles, Blogging, Debugging Errors and Capture the Flags. Enjoy Learning; There is Nothing Like Absolute Defeat - Try and try until you Succeed.

No comments:

Post a Comment