جستجوی دودویی (binary search)
چهارشنبه, ۲۶ آذر ۱۳۹۳، ۱۰:۴۲ ب.ظ
//azarprogrammer.blog.ir
#include<iostream>
#include<conio.h>
using namespace std;
void binsearch(int x,int s[])
{
int low=1,high=20,location=0,mid;
while(low<=high&&location==0)
{
mid=(low+high)/2;
if(x==s[mid])
location=mid;
else if(x<s[mid])
high=mid-1;
else
low=mid+1;
}
cout<<"The place is :"<<location+1;
}
int main()//azarprogrammer.blog.ir
{
int i,x,s[20];
cout<<"Enter the 20-digit order"<<endl;
for(i=0;i<20;i++)
{
cout<<"number "<<i+1<<" :";
cin>>s[i];
}
cout<<"Enter the number Search"<<endl;
cin>>x;
binsearch(x,s);
getch();
return 0;
}
//azarprogrammer.blog.ir
۹۳/۰۹/۲۶