//二分查找
#include<iostream>
using namespace std;
struct LNode {
int Element[100];
int Length;
};
int BinarySearch(LNode *Tbl, int K)
{ int left, right, mid, NotFound = -1;
left = 1;
right = Tbl->Length;
while (left <= right)
{
mid = (left+right)/2;
if (K < Tbl->Element[mid]) right = mid - 1;
else if (K > Tbl->Element[mid]) left = mid + 1;
else return mid;
}
return NotFound;
}
int main()
{
LNode test = { {0,1,2,3,4,5,6,7,8,9}, 9};//待比较数据从第二个开始,第一个位置用来设置哨兵
int result, num = 8;
result = BinarySearch(&test, num);
if(result == -1)
cout<<"没找到!"<<endl;
else
cout<<"在m["<<result<<"]里找到"<<num <<endl;
system("pause");
return 0;
}