#include<conio.h>
void sort(int a[],int n)
{
int i,j,b;
printf("\nSorted Array :");
for(i=0;i<n;i++)
for(j=0;j<n-1;j++)
if(a[j]>a[j+1])
{
b=a[j];
a[j]=a[j+1];
a[j+1]=b;
}
for(i=0;i<n;i++)
printf("\n%d",a[i]);
}
void search(int a[],int n)
{
int u,mid,l,k=0,x;
printf("\nEnter Element To Be Searched For :");
scanf("%d",&x);
u=n-1;
l=0;
while(l<=u)
{
mid=((u+l)/2);
if(a[mid]>x)
u=mid-1;
if(a[mid]<x)
l=mid+1;
if(a[mid]==x)
{
printf("\n\nElement Is Found At Position %d",mid+1);
k++;
break;}
}
if(k==0)
printf("\nElemnt NOT FOUND !!!");
}
void main()
{
int n,a[100],i;
clrscr();
printf("Enter Limit :");
scanf("%d",&n);
printf("\n\nEnter Array :\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,n);
search(a,n);
getch();
}
Wednesday, March 12, 2014
Binary Search
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Post You Comment Here. Always Respect Others . . . . . .