Const maxn = 1000
Dim a(0 To maxn) As Long
Private Sub Form_Load()
′产生maxn个不相同的数据,并把他存储在a数组中,代码略
End Sub
Private Sub Command1_Click()
Dim i As Long, j As Long, tmp As Long, cnt As Long
Dim last_exchange As Integer ′记录最后一次交换位置
Dim tmp_pos As Integer ′本次交换的位置
cnt = 0: i = 1
last_exchange = 0: tmp_pos = 1
Do While ′改错
last_exchange = tmp_pos
For j = maxn To last_exchange + 1 Step -1
If a(j) < a(j - 1) Then
tmp = a(j): a(j) = a(j - 1): a(j - 1) = tmp
cnt = cnt + 1
End If
Next j
i = tmp_pos
Loop
′输出,代码略
End Sub