2分查找,递归实现
谁帮我看下代码哪里错了#include <stdio.h>
#include <stdlib.h>
#define N 11
#define COMPARE(x,y) (x)<(y)?-1:((x)==(y)?0:1)
int binsearch(int searchnum,int list[],int left,int right);
int main()
{
int list[N]={1,3,5,8,10,14,17,19,23,26,29};
int ele,conse;
printf("input a number searching for.");
scanf("%d",&ele);
conse=binsearch(ele,list,0,11);
if(conse==-1)
fprintf(stderr,"error,have no equal value.");
else
printf("%d is found in list.",ele) ;
}
int binsearch(int searchnum,int list[],int left,int right)
{
int middle;
if(left<=right){
middle=(left+right)/2;
switch(COMPARE(list[middle],searchnum)){
case-1: return
binsearch(searchnum,list,middle+1,right);
case 0:
return middle;
case 1: return
binsearch(searchnum,list,left,middle-1);
}
}
return -1;
}
}