|
以下是网上找到的函数
但是运行后 丢失一个数组元素
请高人看看咋修正一下
Function BubbleSort(TempArray As Variant)
Dim Temp As Variant
Dim i As Integer
Dim NoExchanges As Integer
' Loop until no more "exchanges" are made.
Do
NoExchanges = True
' Loop through each element in the array.
For i = 1 To UBound(TempArray) - 1
' If the element is greater than the element
' following it, exchange the two elements.
If TempArray(i) > TempArray(i + 1) Then
NoExchanges = False
Temp = TempArray(i)
TempArray(i) = TempArray(i + 1)
TempArray(i + 1) = Temp
End If
Next i
Loop While Not (NoExchanges)
End Function
Sub BubbleSortMyArray()
Dim TheArray As Variant
' Create the array.
TheArray = Array(15, 8, 11, 7, 33, 4, 46, 19, 20, 27, 43, 25, 36)
' Sort the Array and display the values in order.
BubbleSort TheArray
For i = 1 To UBound(TheArray)
Debug.Print TheArray(i) ' 注意 ,此处输出 后 比(15, 8, 11, 7, 33, 4, 46, 19, 20, 27, 43, 25, 36)少了一个
Next i
End Sub
|
|