实现上述功能的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
List1.AddItem str(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)
elseif ② then
pos = j
s = s + "→" + Str(pos)
End If
End If
Next j
Next i
Label1.Caption = "位置变化情况:" + s
List2.Clear
For i = 1 To n
List2.AddItem Str(a(i))
Next i
End Sub