'选择排序
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