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, Web Developer, Student and Mechanical Engineer. He Enjoys writing articles, Blogging, Solving Errors and Social Networking. Feel Free to let me know any of your concerns about hacking or let me know if you need any more methods on hacking anything. Enjoy Learning

No comments:

Post a Comment