当前位置:K88软件开发文章中心编程全书编程全书01 → 文章内容

使用VBA实现冒泡法排序

减小字体 增大字体 作者:佚名  来源:翔宇亭IT乐园  发布时间:2019-1-3 0:17:46

:2012-06-06 06:28:07

Public Sub BubbleSort(ByRef lngArray() As Long)
    Dim iOuter As Long
    Dim iInner As Long
    Dim iLBound As Long
    Dim iUBound As Long
    Dim iTemp As Long

    iLBound = LBound(lngArray)
    iUBound = UBound(lngArray)
 
     '冒泡排序
      For iOuter = iLBound To iUBound - 1
          For iInner = iLBound To iUBound - iOuter - 1

                  '比较相邻项
                    If lngArray(iInner) > lngArray(iInner + 1) Then
                         '交换值
                          iTemp = lngArray(iInner)
                          lngArray(iInner) = lngArray(iInner + 1)
                          lngArray(iInner + 1) = iTemp
                   End If

            Next iInner
      Next iOuter
End Sub


使用VBA实现冒泡法排序