首页 > 课堂 > 技术开发 > 正文

VBA中实现选择排序算法

2023-06-15 12:14:57
字体:
来源:转载
供稿:网友

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

    iLBound = LBound(lngArray)
    iUBound = UBound(lngArray)

    '选择排序
    For iOuter = iUBound To iLBound + 1 Step -1

        iMax = 0

        '得到最大值得索引
        For iInner = iLBound To iOuter
            If lngArray(iInner) > lngArray(iMax) Then iMax = iInner
        Next iInner

        '值交换
        iTemp = lngArray(iMax)
        lngArray(iMax) = lngArray(iOuter)
        lngArray(iOuter) = iTemp

    Next iOuter
End Sub

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表