# include <iostream.h>
# include<conio.h>
void main( )
{
int list[20], number, result = 0;
int i, n, low ,mid, high;
cout<<"\n Enter the number of elements : ";
cin>>n;
cout<<"\n Enter elements in ascending order : \n";
for(i=0; i<n; i++)
cin>>list[i];
cout<<"\n Enter the number to be searched : ";
cin>>number;
low = 0;
high = n-1;
while(low <= high)
{
mid = (low+ high) / 2;
if (number < list [mid])
high = mid - 1;
else if(number > list[mid])
low = mid + 1;
else
{
result = list[mid];
break;
}
}
if (result == 0)
cout<<"\n No match found";
else if(result != 0)
{
cout<<"\n Match found \n";
cout<< "Position of number "<< number<<" is : "<<mid +1;
}
}
0 comments:
Post a Comment