#include<stdio.h> #include<conio.h> void main() { int a[10],p,temp,i,n,key,flag=0,low,high,mid; clrscr(); printf("Enter the size of an array:"); scanf("%d",&n); printf("Enter the elements:"); for(i=0;i<n;i++) scanf("%d",&a[i]); for(p=0;p<n;p++) if(a[i]>a[i+1]) temp=a[i]; a[i]=a[i+1]; a[i+1]=temp; printf("Enter the number to be searched:"); scanf("%d",&key); low=0,high=n-1; while(low<=high) { mid=(low+high)/2; if(key==a[mid]) { printf("search element found at location: %d",mid); flag=1; return; } else if (key<a[mid]) high=mid-1; else low=mid+1; } if(flag==0) printf("The number is not found"); getch(); }
C Program For Binary Search With Sorting
January 02, 2015
By:
Bhanu Namikaze
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