Loading

二分查找

 

 1 Position BinarySearch( List L, ElementType X )
 2 {
 3     int left = 1, right = L->Last;
 4     int mid;
 5     while(left <= right)
 6     {
 7         mid = (left + right) / 2;
 8         if(L->Data[mid] > X)    //X在左侧
 9             right = mid - 1;    
10         else if(L->Data[mid] < X)  //X在右侧
11             left = mid + 1;
12         else 
13             return mid;         //X==L->Data[mid] 
14     }
15     return NotFound;     //没找到
16 }

 

posted @ 2018-09-27 13:51  拾月凄辰  阅读(99)  评论(0编辑  收藏  举报