به وبلاگم خوش آمدید

کپی برداری از مطالب وبلاگ فقط با ذکر منبع (azarprogrammer.blog.ir)

به وبلاگم خوش آمدید

کپی برداری از مطالب وبلاگ فقط با ذکر منبع (azarprogrammer.blog.ir)

جستجوی دودویی به روش بازگشتی

پنجشنبه, ۲۷ آذر ۱۳۹۳، ۰۱:۱۲ ق.ظ
//azarprogrammer.blog.ir
#include<iostream>
#include<conio.h>
using namespace std;
int binsearch_rec(int low,int high,int x,int s[])
{
int mid;
if(low>high)
return (-1);
else
{
mid=(low+high)/2;
if(x==s[mid])
return mid;
else if (x<s[mid])
return binsearch_rec(low,mid-1,x,s);
else
return binsearch_rec(mid+1,high,x,s);
}
}
int main()//azarprogrammer.blog.ir
{
int i,x,z,l=0,h=20,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;
z=binsearch_rec(l,h,x,s);
if(z==(-1))
cout<<"Not found";
else
cout<<"The place is :"<<z+1;
getch();
return 0;
}
//azarprogrammer.blog.ir

موافقین ۰ مخالفین ۰ ۹۳/۰۹/۲۷
سجاد بوزکند

نظرات  (۱)

۰۶ اسفند ۹۷ ، ۱۳:۵۶ سیاوش فیضی
سلام.
خیلی جالب بود

ارسال نظر

ارسال نظر آزاد است، اما اگر قبلا در بیان ثبت نام کرده اید می توانید ابتدا وارد شوید.
شما میتوانید از این تگهای html استفاده کنید:
<b> یا <strong>، <em> یا <i>، <u>، <strike> یا <s>، <sup>، <sub>، <blockquote>، <code>، <pre>، <hr>، <br>، <p>، <a href="" title="">، <span style="">، <div align="">
تجدید کد امنیتی