'插入排序
Sub Insertion(MyArray(), Optional ByVal SortByDesc As Boolean)
Dim Index
Dim temp
Dim NextElement
'先将已处理的元素个数为最小下标加1
NextElement = LBound(MyArray) + 1
'遍历每一个元素
While (NextElement <= UBound(MyArray))
'读取当前下标
Index = NextElement
Do
'如果当前下标大于最小下标,则处理
If Index > LBound(MyArray) Then
'根据是升序或降序进行分别处理
If SortByDesc = False Then
'升序:如果当前值小于上一个值,则互换
If MyArray(Index) < MyArray(Index - 1) Then
temp = MyArray(Index)
MyArray(Index) = MyArray(Index - 1)
MyArray(Index - 1) = temp
Index = Index - 1
Else
Exit Do
End If
ElseIf SortByDesc = True Then
'降序:如果当前值大于上一个值,则互换
If MyArray(Index) > MyArray(Index - 1) Then
temp = MyArray(Index)
MyArray(Index) = MyArray(Index - 1)
MyArray(Index - 1) = temp
Index = Index - 1
Else
Exit Do
End If
End If
Else
Exit Do
End If
Loop
NextElement = NextElement + 1
Wend
End Sub
03-15
1998
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交