对有序数组进行二分查找(折半查找)

C code:

 1  void Bin_Search(int a[], int key)
 2 {
 3      int left = 0;
 4      int right = MAX-1;
 5      int mid = 0;
 6      //折半查找 
 7      while(left <= right){
 8          mid = (left + right) / 2;
 9          if ( key==a[mid] )
10             printf("你所查找的数在数组中的位置是:%d\n", mid);
11         else if( key<a[mid] )
12             right = mid-1;
13         else left = mid+1;
14     }
15     if (left > right)
16         printf("您所要查找的数不在该数组。\n");
17 }

 

posted @ 2017-03-11 12:05  NemoWang  阅读(619)  评论(0编辑  收藏  举报