二分查找

#include <stdio.h>
#define N 6
int f(int a[],int low,int high,int x)
{
    int mid =(low+high)/2 ;
    if (low>high)      return -1 ;
    else     if (a[mid]==x)     return mid ;
    else     if (a[mid]>x )     return  f(a,low,mid-1,x);
    else            return  f(a,mid+1, high,x);


}
int main ( )
{

    int x, a[N]={ 1,2,3,5,6,7} ;
    scanf("%d",&x);
    printf("%d\n",f(a,0,N-1,x));

}

********************************************************


#include <stdio.h>
#define N 6
int f(int a[],int low,int high,int x)
{
	int mid =(low+high)/2 ;
	if(low>high)      return -1 ;
	 if(a[mid]==x)     return mid ;
	 if(a[mid]>x)    return  f(a,low,mid-1,x);
	else            return  f(a,mid+1, high,x);
	 

}
int main ( )
{
	 
	 int x, a[N]={ 1,2,3,5,6,7} ;
	 scanf("%d",&x);
	printf("%d\n",f(a,0,N-1,x));

}

  


  

posted @ 2013-12-23 22:47  博园少主  阅读(192)  评论(0编辑  收藏  举报