PDA

View Full Version : سوال: جستجوی دودویی



a-t-e-f-e-h
شنبه 22 مهر 1391, 20:40 عصر
سلام
ممکنه یکی راهنماییم کنه؟
باید برنامه ای رو بنویسم که جستجوی دودویی رو انجام بده ولی در سه بازه
این برنامه رو نوشتم ولی خطای break میده
نمیتونم چطوری باید مشکلش رو حل کنم:افسرده:
میدونم برنامه ش خیلی ساده هست ولی متاسفانه نتونستم مشکلش رو حل کنم
visual c++)


#include<iostream>
#include<conio.h>
using namespace std;
const int max=11;
int value[]={1,3,4,5,8,12,15,18,20,24,26};

int binarysearch(int low,int high,int n)
{
if(low>high)
return -1;
int mid1=(low+high)/3;
int mid2=(mid*2)+1;
int mid1=mid+1;
if(n==value[mid1])
return mid1;
else if(n<value[mid1])
return binarysearch(low,mid1-2,n);
else if(value[mid1]<n && n>value[mid2])
return binarysearch(mid1+1,mid2-1,n);
else if(value[mid2]==n)
return mid2;
else if(n>value[mid2])
binarysearch(mid2+1,high,n);
}
int main(){
system("cls");
cout<<"enter number.";
int n;
cin>>n;
int t=binarysearch(0,10,n);
if(t==-1)
cout<<" not found";
else
cout<<value[t];
getch();
return 0;
}