procedure midsearch(num:longint);
var top,bot,mid:longint;
begin
top:=0;bot:=nk+1;
if bot-top<=1 then mid:=bot;
while bot-top>1 do
begin
mid:=(top+bot) div 2;
if list[num]<=list[res[mid]] then
top:=mid
else bot:=mid;
end;
res[bot]:=num;
rec[num]:=res[bot-1];
if bot=nk+1 then inc(nk)
end;
var top,bot,mid:longint;
begin
top:=0;bot:=nk+1;
if bot-top<=1 then mid:=bot;
while bot-top>1 do
begin
mid:=(top+bot) div 2;
if list[num]<=list[res[mid]] then
top:=mid
else bot:=mid;
end;
res[bot]:=num;
rec[num]:=res[bot-1];
if bot=nk+1 then inc(nk)
end;
本文介绍了一种使用Pascal语言实现的二分查找算法。该算法通过定义过程midsearch,接收一个长整型参数,并利用变量top、bot和mid来追踪搜索范围。当bot-top大于1时循环进行中间值计算,根据比较结果调整搜索区间直至找到位置或确定插入点。
1366

被折叠的 条评论
为什么被折叠?



