#include<stdio.h>
#include<conio.h>
// Binary search
void main()
{
int x[10],low,high,mid,y,i,j,k;
clrscr();
for(i=0;i<=9;i++)
{
printf("\n Enter value :");
scanf("%d",&x[i]);
}
for(i<=0;i<=9;i++)
{
for(j=i+1;j<=9;j++)
{
if(x[i] > x[j])
{
k=x[i];
x[i]=x[j];
x[j] = k;
}
}
}
printf("\n Enter value to search :");
scanf("%d",&y);
high = 10;
low =0;
while(low <= high)
{
mid = (low + high) /2;
if(y < x[mid])
high = mid -1;
else if(y>x[mid])
low = mid +1;
else
{
printf("\n Element found on %dth location ",mid+1);
break;
}
}
}
#include<conio.h>
// Binary search
void main()
{
int x[10],low,high,mid,y,i,j,k;
clrscr();
for(i=0;i<=9;i++)
{
printf("\n Enter value :");
scanf("%d",&x[i]);
}
for(i<=0;i<=9;i++)
{
for(j=i+1;j<=9;j++)
{
if(x[i] > x[j])
{
k=x[i];
x[i]=x[j];
x[j] = k;
}
}
}
printf("\n Enter value to search :");
scanf("%d",&y);
high = 10;
low =0;
while(low <= high)
{
mid = (low + high) /2;
if(y < x[mid])
high = mid -1;
else if(y>x[mid])
low = mid +1;
else
{
printf("\n Element found on %dth location ",mid+1);
break;
}
}
}
No comments:
Post a Comment