vba 实现冒泡排序

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:改进版3按照空行分割工作表并保存为独立工作簿


下一篇:非计算机专业人员的程序之路