编写一个VB程序,实现程序功能如下:随机产生10个1~20之间的整数存放在数组a,在列表框List1中显示,单击“排序”按钮Command1后,在列表框List2中显示升序排序后的结果。具体算法描述如下:引入数组index,index(i)存储i位置应放置的数组元素的下标。排序完毕,a(index(i))成为升序序列,即a(index(1))≤a(index(2))≤a(index(3))≤……≤a(index(i))。在数组a的所有元素中找出最小元素,将其所在位罝存放在数组元素index(1)中,然后在余下的元素中找出最小元素,将其所在位置存放在数组元素index(2)中,以此类推,直到完成所有元素排序。如n=5时,数组a排序后各变量值如下表所示。
i
|
1
|
2
|
3
|
4
|
5
|
a(i)
|
17
|
19
|
9
|
13
|
6
|
index(i)
|
5
|
3
|
4
|
1
|
2
|
a(index(i))
|
6
|
9
|
13
|
17
|
19
|
实现上述功能的VB程序如下,但加框处代码有错,请改正。
Const n = 10
Const maxn = 20
Dim a(1 To n) As Integer,
index(1 To n) As Integer, flag(1 To n) As Boolean
Private Sub Form_Load()
Dim i As Integer
Randomize
For i = 1 To n
flag(i) = False
a(i) = Int(Rnd() * maxn) + 1
List1.AddItem Str(a(i))
Next i
End Sub
Private Sub Command1_Click()
Dim i As Integer, j As
Integer
Dim k As Integer
For i = 1 To n
For j = 1 To n
If flag(j) = False Then k = j: Exit For
Next j
For m = k + 1 To n
If Then k = m ‘⑴
Next m
index(i) = k
flag(k) = True
Next i
For i = 1 To n
List2.AddItem ‘⑵
Next i
End sub
⑴{#blank#}1{#/blank#} ⑵{#blank#}2{#/blank#}