实现该功能的VB程序如下,但加框处代码有错,请改正。
Const n = 10
Dim a(1 To n) As Integer
Private Sub Form_Load()
随机产生 n 个不重复的整数存数组 a,并在列表框 list1 中显示。代码略。
End Sub
Private Sub Command1_Click()
Dim i As Integer, j As Integer, t As Integer
Dim imin1 As Integer, imin2 As Integer
For i = 1 To n \ 2
imin1 = i: imin2 = i + 1
If a(imin1) > a(imin2) Then t = imin1: imin1 = imin2: imin2 = t
For j = i + 2 To n - i + 1
If a(j) < a(imin1) Then
imin2 = imin1: imin1 = j
'①
imin2 = j
End If
Next j
If i <> imin1 Then t = a(i): a(i) = a(imin1): a(imin1) = t
If imin2 = i Then '②
If n - i + 1 <> imin2 Then
t = a(n - i + 1): a(n - i + 1) = a(imin2): a(imin2) = t
End if
Next i
For i = 1 To n
Text2.Text = Text2.Text + Str(a(i))
Next i
End Sub
① ②