小白笔记--------------二分搜索算法

 1 #include
  2 #include
  3 
  4 int main (int argc ,char** argv){
  5     int a[10] = {0,1,3,4,7,8,9,22,33,44};
  6     int result;
  7     result = erfen(a,0,9);
  8     printf("%d\n",result);
  9     return 0;
 10 }
 11 int erfen(int a[],int left,int right){
 12     while(left < right){
 13         int mid = (left + right)/2;
 14         if(a[mid] == 0){
 15             return mid;
 16         }else{
 17             if(a[mid] < 0){
 18                 left = mid;
 19             }else{
 20                 right = mid -1;
 21             }
 22 
 23         }
 24         if(a[left] == 0) return left;
 25         if(a[right] == 0) return right;
 26     }
 27 }

你可能感兴趣的:(算法设计)