中国石凳联盟 登录注册

Rockage进来看看

rj45··8 回复·403 浏览

发表回复
题目:
编写一个函数,实现两分法查找算法。
两分法查找的思想是,首先假设在查找的数字数组中,是按照从小到大的顺序排列的,要查找的关键值首先同中间的元素比较,若相同则查找成功,否则根据这个中间的数与要查找的数的大小关系,保留一半,在数组的另外一半中进行查找,如此重复,每次都到中音的数据去进行比较,每次将减少一半的查找的数据,因此效率比较高。
编写的程序在数组ABC(1,2,3,4,5,6,7,8,9,10)中查找到其中任何数字.
提示:采用一个递归的算法来实现.
一定要用递归法来实现,只用两分法已搞定,代码如下:
Dim times As Long
Dim vbarr() As Long

Private Sub Command1_Click()
ReDim vbarr(Val(Text1.Text)) As Long
Dim i As Long
For i = 0 To Val(Text1.Text)
vbarr(i) = i + 1
Next i
i = search(vbarr, Text2.Text)
Text3.Text = "找到数字"
vbArray(i) Then
searchmin = i + 1
Else
searchmax = i - 1
End If
Loop While searchmax
= searchmin
End Function
Being a devil is the same as being a human. We should be kind. If we are kind, we are not a devil, we are a hybrid.
点赞0 收藏
我需要的是递归呀,上面用的是二分!递归该怎么做?
Being a devil is the same as being a human. We should be kind. If we are kind, we are not a devil, we are a hybrid.
点赞0 收藏
就是查到一个数组里的某个数,用二分法+递归
Being a devil is the same as being a human. We should be kind. If we are kind, we are not a devil, we are a hybrid.
点赞0 收藏
明白!

Dim times As Long
Dim vbarr() As Long
Dim find As Long

Private Sub Command1_Click()
ReDim vbarr(Val(Text1.Text)) As Long
Dim i As Long
For i = 0 To Val(Text1.Text)
vbarr(i) = i + 1
Next i
search vbarr, Text2.Text, CLng(0), CLng(Text1.Text)
Text3.Text = "找到数字"
= UBound(vbArray) Or i
vbArray(i) Then

searchmin = i + 1
Else
searchmax = i - 1
End If
search vbarr, Text2.Text, searchmin, searchmax
End If
End Function
Being a devil is the same as being a human. We should be kind. If we are kind, we are not a devil, we are a hybrid.
点赞0 收藏
感谢华哥
Being a devil is the same as being a human. We should be kind. If we are kind, we are not a devil, we are a hybrid.
点赞0 收藏
发表回复
发表回复