BubleSort




Dim inArray() As String ' Input array
'Redim the array To the size of the list count items

ReDim inArray(lstItems.ListCount - 1)
For ic = 0 To lstItems.ListCount - 1
' Put all the values from the list box To the array

inArray(ic) = lstItems.List(ic)
Next
cBubbleSort inArray ' Sort array
lstItems.Clear ' Clear list
For ic = 0 To UBound(inArray)
' Put the sorted items from the array

lstItems.AddItem inArray(ic)
Next
Public Sub cBubbleSort(inputArray As Variant)
Dim lDown As Long, lUp As Long
For lDown = UBound(inputArray) To LBound(inputArray) Step -1
For lUp = LBound(inputArray) + 1 To lDown
If inputArray(lUp - 1) > inputArray(lDown) Then _
SwapValues inputArray(lUp - 1), inputArray(lDown)
Next lUp
Next lDown
End Sub

Public Sub SwapValues(firstValue As Variant, secondValue As Variant)
Dim tmpValue As Variant
tmpValue = firstValue
firstValue = secondValue
secondValue = tmpValue
End Sub

Public Sub cBubbleSort(inputArray As Variant)
Dim lDown As Long, lUp As Long
' Two variables that will be used In the fors

For lDown = UBound(inputArray) To LBound(inputArray) Step -1
' One variable will go from the upper bound of the array

For lUp = LBound(inputArray) + 1 To lDown
' and the second one will go from the lowest bound to the top

If inputArray(lUp - 1) > inputArray(lDown) Then _
SwapValues inputArray(lUp - 1), inputArray(lDown)
' This line check if the value from the up-to-down For is

' higher than the value from the down-to-up for, if so the

' Sub call a swap sub that switches the values places

Next lUp
' Continue To the next value from down-to-up

Next lDown
' Continue To the next value from up-to-down

End Sub

Public Sub SwapValues(firstValue As Variant, secondValue As Variant) ' This sub switches the values
Dim tmpValue As Variant
' Temp variable To store the first value

tmpValue = firstValue
' put the first value into a temp variable

firstValue = secondValue
' put the second value into the first

secondValue = tmpValue
' and Then put the first value, that stored In a temp variable, into the second

End Sub

Ccreate una listbox nominatela lstItems
copiate il codice code su un command button










( bublesort.html )- by Paolo Puglisi - Modifica del 17/12/2023