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)
MsgBox TheArray(i)
Next
i
End
Sub
联系客服