使用递归实现二分法查找
#include "stdio.h" void Search(int p[],int low,int height,int key) { int middle=(low+height)/2; if(low>height) { printf("没有该数!"); return; } if(p[middle]==key) { printf("%d\n",middle); return; } else if(p[middle]>key) { Search(p,low,middle-1,key); } else if(p[middle]<key) { Search(p,middle+1,height,key); } } int main() { int p[5]={1,2,3,4,5}; Search(p,0,4,4); return 0; }
----转载请注明出处http://www.cnblogs.com/JerryWang1991/ 谢谢!