二分法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 3 int binsearch(int x,int a[],int n) 4 { 5 int low,high,mid; 6 7 low=0,high=n-1; 8 9 while(low<=high) 10 { 11 mid=(low+high)/2; 12 if(x<a[mid]){ 13 high=mid-1; 14 } 15 else if(x>a[mid]){ 16 low=mid+1; 17 } 18 else{ 19 return mid; 20 } 21 } 22 return -1; 23 }