Dim stackSize As Long
Dim top As Long
Const LowPosition As Integer = 0
Const HighPosition As Integer = 1
Function quickSortNonRecursion(ByRef listToSort() As Long) As Boolean
Dim stack(100, 2) As Long
Dim low As Long, high As Long, tempLow As Long, tempHigh As Long:::::::
low = (LBound(listToSort)): high = (UBound(listToSort))
Dim placeMeRight As Long, exchangeTemp As Long
stack(top, LowPosition) = low: stack(top, HighPosition) = high
While (top >= 0)
placeMeRight = listToSort(stack(top, LowPosition))
low = stack(top, LowPosition)
high = stack(top, HighPosition)
Do
Do While (listToSort(stack(top, LowPosition)) < placeMeRight)
stack(top, LowPosition) = stack(top, LowPosition) + 1
Loop
Do While (listToSort(stack(top, HighPosition)) > placeMeRight)
stack(top, HighPosition) = stack(top, HighPosition) - 1
Loop
If (stack(top, LowPosition) <= stack(top, HighPosition)) Then
exchangeTemp = listToSort(stack(top, LowPosition))
listToSort(stack(top, LowPosition)) = listToSort(stack(top, HighPosition))
listToSort(stack(top, HighPosition)) = exchangeTemp
stack(top, LowPosition) = stack(top, LowPosition) + 1
stack(top, HighPosition) = stack(top, HighPosition) - 1
End If
Loop While (stack(top, LowPosition) <= stack(top, HighPosition))
tempLow = stack(top, LowPosition)
tempHigh = stack(top, HighPosition)
top = top - 1
If (tempLow < high) Then
top = top + 1
stack(top, LowPosition) = tempLow
stack(top, HighPosition) = high
End If
If (tempHigh > low) Then
top = top + 1
stack(top, LowPosition) = low
stack(top, HighPosition) = tempHigh
End If
Wend
End Function
Function bubbleSort(ByRef listToSort() As Long) As Boolean
Dim i As Long, j As Long, endOfList As Long, exchangeTemp As Long
endOfList = UBound(listToSort)
For i = LBound(listToSort) To endOfList
For j = i To endOfList
If (listToSort(i) > listToSort(j)) Then
exchangeTemp = listToSort(i)
listToSort(i) = listToSort(j)
listToSort(j) = exchangeTemp
End If
Next j
Next i
End Function
Private Sub Class_Initialize()
stackSize = 90: top = 0
End Sub
Function quickSortRecursion(ByRef listToSort() As Long, ByVal First As Long, ByVal Last As Long) As Boolean
Dim low As Long, high As Long, placeMeRight As Long, exchangeTemp As Long
low = First: high = Last
placeMeRight = listToSort(low)
Do
Do While (listToSort(low) < placeMeRight)
low = low + 1
Loop
Do While (listToSort(high) > placeMeRight)
high = high - 1
Loop
If (low <= high) Then
exchangeTemp = listToSort(low)
listToSort(low) = listToSort(high)
listToSort(high) = exchangeTemp
low = low + 1
high = high - 1
End If
Loop While (low <= high)
If (First < high) Then quickSortRecursion listToSort(), First, high
If (low < Last) Then quickSortRecursion listToSort(), low, Last
End Function