VB排序算法-选择排序

'选择排序
Sub Selection(MyArray(), Optional ByVal SortByDesc As Boolean)
    Dim Index
    Dim min
    Dim NextElement
    Dim temp '已处理的元素的个数置为0 NextElement = 0
    '遍历所有元素
    While (NextElement < UBound(MyArray))
        '读取最大下标,作为当前最小值下标
        min = UBound(MyArray)
        '取倒数第二个下标
        Index = min - 1
        '与所有元素比较
        While (Index >= NextElement)
            '根据是升序或降序进行分别处理
            If SortByDesc = False Then
                '根据比较结果重置最小下标
                If MyArray(Index) < MyArray(min) Then
                    
                    min = Index
                End If
            ElseIf SortByDesc = True Then
                '根据比较结果重置最小下标
                If MyArray(Index) > MyArray(min) Then
                    min = Index
                End If
            End If
            Index = Index - 1
        Wend
        '根据最小下,与当前值互换
        temp = MyArray(min)
        MyArray(min) = MyArray(NextElement)
        MyArray(NextElement) = temp
        NextElement = NextElement + 1
    Wend
End Sub

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值