代码:
Private Sub Command1_Click()
Dim a(500) As Integer
For i = LBound(a) To UBound(a)
a(i) = Int(Rnd * 100)
Next i
qsort a, LBound(a), UBound(a)
For i = LBound(a) To UBound(a)
Print a(i);
Next i
End Sub
Sub swap(a As Integer, b As Integer)
t = a: a = b: b = t
End Sub
‘快速排序
Sub qsort(a() As Integer, ByVal p As Integer, ByVal r As Integer)
Dim q As Integer
If p >= r Then Exit Sub
q = p
For i = p To r - 1
If a(i) < a(r) Then swap a(i), a(q): q = q + 1
Next i
swap a(q), a(r)
qsort a, p, q - 1
qsort a, q + 1, r
End Sub