//binary search
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int n;
cout<<"enter the array length: ";
cin>>n;
int bc[n];
for(int i=0;i<=n;i++)
{
cout<<"the value of bc["<<i<<"] is: ";
cin>>bc[i];
}
int m;
int c,f,mid;
c=1;
f=n+1;
mid=(c+f)/2;
cout<<"enter a element you want to search :";
cin>>m;
while(c<=f && bc[mid]!=m)
{ if (m<bc[mid])
f=mid-1;
else {
c=mid+1; }
mid = (c+f)/2;
}
if(f>=c){
cout<<"the element is located in mid";
}
else
{
cout<<"the element is not located in mid";
}
getch();
return 0;
}
No comments:
Post a Comment