哨兵
//设置哨兵可在遍历查找时省去一个判断条件 i>0
int SqListSearch(Sqlist L,int key)
{
L->data[0] = sentry;//头结点设置哨兵
for (int i = L->length; L->data != key; i--);
return i;
}
二分查找
时间复杂度=O(logN)
int binFind(SqList L,int elem)
{
int left, right, mid;
left = 1;//0是头结点
right = L->length;
while (left < right)
{
mid = (left + right) / 2;
if (elem > L->data[mid])//L内数据从小到大排序
left = mid + 1;
else if (elem < L->data[mid])
right = mid + 1;
else
return mid;
}
return -1;
}