做法是比较中点与当前查找范围第1个数大小关系,用对分法使查找范围逼近拐点;当余下数组只有二个元素时,可根据此二元素大小关系得到拐点的位置。
依据上述描述设计了VB程序,界面如图所示。请回答下列问题:
Dim a(1 To 20) As Integer
Private Sub Command1_Click()
Dim Low As Integer, High As Integer
Low = 1
High = 20
Do While High > Low
If ① Then '当只余下两个元素时
If ② Then
Label1.Caption = "拐点元素为" + Str(a(1)) + ",下标为" + Str(1)
Else
Label1.Caption = "拐点元素为" + Str(a(High)) + ",下标为" + Str(High)
End If
Exit Do
End If
If a(m) >= a(Low) Then
Low = m
Else
③
End If
Loop
End Sub
Private Sub Form_Load()
'生成循环递增的数组a,代码略
End Sub
① ② ③