#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int l_v=1;
int h_v;
int a[51];
int middle;
int num,i=0;
cout<<"Input your sorted num :
";
while(scanf("%d",&a[i])!=EOF)
i++;
h_v=i;
cout<<"
The input numbers are :
";
i=0;
while(i<h_v)
{
cout<<a[i]<<endl;
i++;
}
getch();
cout<<"
Input your searching number :
";
cin>>num;
for(int n=0;a[middle]!=num;n++)
{
middle = (l_v + h_v)/2;
if(a[middle]>num)
h_v = middle - 1;
else if(a[middle]<num)
l_v = middle + 1;
else if(a[middle]==num)
cout<<"
Found your number in "<<middle+1<<" position.";
}
cout<<"
This program's loop is executed "<<n<<" times to find out
the
number.";
getch();
}
No comments:
Post a Comment