【加试题】编写一个 VB 程序实现数据左右交替上升排序。功能如下:随机产生n个不重复的整数存数组a,并在列表框list1中显示,单击“排序”按钮Command1,在列表框list2中显示排序后的数据。某遍程序运行后,数组a中存储的左右交替上升排序的n个正整数,如下表所示:
实现该功能的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
①{#blank#}1{#/blank#} ②{#blank#}2{#/blank#}