数组元素a(1)至a(5)依次为5,22,28,42,55,在a(3)处分成两段,交换位置后为:
a(1) | a(2) | a(3) | a(4) | a(5) |
28 | 42 | 55 | 5 | 22 |
旋转数组a的最小元素是5。
数组d(1)至d(5)依次为0,1,1,1,1,在d(5)处分成两段,交换位置后为:
d(1) | d(2) | d(3) | d(4) | d(5) |
1 | 0 | 1 | 1 | 1 |
旋转数组d的最小元素是0。
小明依据查找算法思想,设计一个查找旋转数组d最小元素的程序,功能如下:运行程序时,利用“添加”按钮Command1和文本框Text1依次输入旋转数组d的值,在列表List1中显示旋转数组,单击“求解”按钮Command2,在标签Label2中显示旋转数组d最小元素值。运行效果如图1所示,实现该功能的VB程序如下:
图1
Dim n As Integer
Dim d(1 To 100) As Integer '数组 d 存储待排序数据
Private Sub Command1_Click()
'按旋转数组规则将数据存入数组d中,代码略
End Sub
Private Sub Command2_Click()
Dim mid As Integer, L As Integer, R As Integer
L = 1: R = n
mid = L
Do While d(L) >= d(R)
If Then '改错
mid = R
Exit Do
End If
mid = (L + R) \ 2
If d(L)=d(R) And d(mid)=d(R) Then
mid =______________ '填空
Exit Do
End If
If d(L) < d(mid) Then
L = mid
ElseIf d(mid) < d(R) Then
R = mid
End If
Loop
Label2.Caption = "最小值为" + Str(d(mid))
End Sub
End If
Next i
End Function