二分查找(折半查找) ——————最常用
#include <stdio.h>
#define LISTSIZE 100
typedef int Datatype;
typedef struct
{
Datatype data [LISTSIZE];
int nLength;
}SepList;
int main(int argc, const char * argv[])
{
return 0;
}
int BSearch(SepList *List, Datatype key)
{
//查找关键字,key.
//nLow n nHihg,分别确定两个查找的两个边界, nMiddle指向范围的中间,与关键字对比。
int nLow=0, nHigh=List->nLength-1;
int nMiddle=0;
while (nLow<=nHigh)
{
nMiddle=(nLow+nHigh)/2;
if (List->data[nMiddle]==key)
{
return nMiddle;
}
else if (List->data[nMiddle]<key)
{
nLow=nMiddle+1;
}
else
nHigh=nMiddle-1;
}
return -1;
}