a(1) |
a(2) |
a(3) |
…… |
a(n —2) |
a(n—1) |
a(n) |
3 |
25 |
38 |
…… |
55 |
31 |
12 |
依据对分查找思想,设计一个在数组a中查找数据key的程序。实现该功能的VB程序如下,但加框处代码有错,请改正。
Private Sub Command1_Click()
Const n = 6
Dim a(1 To n)As Integer,flag As Boolean
Dim i As Integer,j As Integer,m As Integer,key As Integer
'读取一组正整数,按上述规则存入数组a中,代码略。
key = VaKText1. Text)
i = 1
j = (n + 1) \ 2
flag = False
Do While And Not flag ‘①
m = (i + j) \ 2
If key = a(m) Then
flag = True
ElseIf key< a(m) Then
j = m - 1
Else
i = m + 1
End If
Loop
If Not flag And j > 0 Then
m = ‘②
If key = a(m) Then flag = True
End If
If flag Then
Text2. Text = Str(m)
Else
Text2. Text = "找不到"
End If
End Sub
① ②