#include<stdio.h> int main(){ int a[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; int l=sizeof(a)/4; //长度 int x=10; //要查找到的值 int Bisection(int x,int* a,int l); Bisection(x,a,l); return 0; } int Bisection(int x,int* a,int l){ int address; //查找的数的位置 //先考虑两端的位置 if(x==a[l-1]){ address=l-1; printf("%d ",address); return address; }else if(x==a[0]){ address=0; printf("%d ",address); return address; } //如果不在两端 address=l/2; l=l/2; while(true){ if(x==a[address]){ printf("%d ",address); break; }else if(x>a[address]){ l=l/2; address=address+l; }else if(x<a[address]){ l=l/2; address=address-l; } if(l==0){ //如果l=0了表示找不到 printf("not found"); break; } } return address; }