//二分查找
//查找前提是数列有序,以下程序以数组升序为例
#include <stdio.h>
#define MaxSize 10000
typedef struct LNode* List;
struct LNode {
int Element[MaxSize];
int Length;
};
struct LNode L;
List Tbl;
int BinarySearch(List Tbl, int K)
{
int left, mid, right;
int NoFound = -1;
left = 1;
right = Tbl->Length;
while (left <= right) {
mid = (left + right) / 2;
if (Tbl->Element[mid] > K)
right = mid - 1;
else if (Tbl->Element[mid] < K)
left = mid + 1;
else
return mid;
}
return NoFound;
}