小吴为了研究冒泡排序过程中数据的”移动”情况,编写了一个VB程序,功能如下:在列表框List1中显示排序前数据(存储在数组a中),在文本框'Text1中输入初始位置(即下标值),单击“排序”按钮Command1后,在标签Labell中显示指定初始位置的数据在排序过程中的位置变化情况,排序后的数据显示在列表框List2中。程序运行界面如图所示。
实现上述功能的VB程序如下,但加框处代码有错,请改正。
Dim a(1 To 8)As Integer
Dim n As Integer
Private Sub Form_Load()
a(1)= 30:a(2)= 47:a(3)= 30:a(4)= 72
a(5)= 70:a(6)= 23:a(7)= 99:a(8)= 24
n = 8
For i = 1 To 8
List 1.Addltem a(i)
Next i
End Sub
Private Sub Command1_Click()
Dim i As Integer, j As Integer, k As Integer
Dim pos As Integer
Dim s As String
s = Text1.Text
pos = Val(Text1.Text)
For i = 1 To n- 1
For j = n To i+1 Step-1
If a(j)< a(j -1) Then
‘⑴
a(j-1)= a(j)
a(j)= k
'如果pos位置的数据参与交换,则更新pos值,记录pos变化位置
If pos = j Then
pos = j-1
s = s + " →" + Str(pos)
‘⑵
pos = j
s = s + " →" + Str(pos)
End If
End If
Next j
Next i
Label1.Caption = "位置变化情况:" +s
For i = 1 To n
List2.AddItem Str(a(i))
Next i
End Sub
⑴{#blank#}1{#/blank#} ⑵{#blank#}2{#/blank#}