Sub main()
Dim a()
a = Array(1, 3, 2, 5, 6, 7, 9, 8)
sort a
End Sub
' 冒泡排序
Sub sort(a)
Dim i As Integer
Dim j As Integer
If Not VBA.IsArray(a) Then Exit Sub
For i = LBound(a) To UBound(a)
For j = LBound(a) To UBound(a) - i - 1
If less(a, j + 1, j) Then exch a, j, j + 1
Next
Next
End Sub
Function less(a, i As Integer, j As Integer) As Boolean
less = a(i) < a(j)
End Function
Sub exch(a, i As Integer, j As Integer)
Dim temp
temp = a(i)
a(i) = a(j)
a(j) = temp
End Sub
vba 实现冒泡排序
最新推荐文章于 2022-10-26 21:05:11 发布