VB 编程中的顺序查找和折半查找怎么编的

如题所述

1.顺序查找没什么说的,就是
for(int
i=0;i<len;i++)
if(arr[i]==data)
return
i;
return
-1;
2.折半就是设计low,high
int
low=0;
int
high=len-1;
int
mid;
while(low<=high)
{
mid=(low+high)/2;
if(data>arr[mid])
low=mid+1;
else
if(data<arr[mid])
high=mid-1;
else
return
mid;
}
缉弧光旧叱搅癸些含氓return
-1;
温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-12-26
首先要求被查找的表是已经排序好的,我机器上没VB,凭记忆写,格式可能稍有错误。
顺序查找:
Private
Function
ShunXu(
Array()
as
integer,
data
as
integer
)
as
integer
dim
i
as
integer
for
i
=
0
to
(len(Array)
-
1)
if
Array(i)
=
data
then
ShunXu
=
i
end
if
next
i
End
Function
折半查找:
private
function
ZheBan(
Array()
as
integer,
data
as
integer
)
as
integer
dim
i
as
integer
dim
intHigh
as
integer
dim
intLow
as
integer
dim
intMid
as
integer
while
(intLow
<=
intHigh)
intMid
=
(intLow
+
intHigh)
/
2
if
Array(intMid)
>
data
then
intHigh
=
intMid
-
1
elseif
Array(intMid)
<
data
then
intLow
=
intMid
+
1
else
ZheBan
=
intMid
end
if
wend
end
function本回答被提问者采纳
相似回答