// Bseasrch.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include<stdio.h> //非递归做法 int Besearch(int a[],int low,int high,int key) { int mid; while(low<=high) { mid=(low+high)/2; if(a[mid]==key) return mid; else if(key<a[mid]) high=mid-1; else low=mid+1; } return -1; } //递归做法 int Besearch1(int a[],int low,int high,int key) { int mid; if(low<=high) { mid=(low+high)/2; if(a[mid]==key) return mid; else if(a[mid]<key) return Besearch1(a,mid+1,high,key); else return Besearch1(a,low,mid-1,key); } return -1; } int _tmain(int argc, _TCHAR* argv[]) { int b[]={1,3,9,10,15,88,92,163}; //非递归操作 int n=Besearch(b,0,7,88); printf("这个数是第%d个数!/n",n); //递归操作 int m=Besearch1(b,0,7,0); printf("这个数是第%d个数!/n",m); getchar(); return 0; }