二分查找
程序员文章站
2022-05-08 19:12:44
...
Position BinarySearch( List L, ElementType X )
{
int p = 0;
int left = 1;
int right = L->Last;
int mid;
while(left<=right)
{
mid = (left + right) / 2;
if(L->Data[mid] == X)//中间的数为被查数!!!
{
p = mid;
break;
}
else if(L->Data[mid] > X)
{
right = mid-1;
}
else if(L->Data[mid] < X)
{
left = mid+1;
}
}
if(p == 0)
return NotFound;
return p;
}