VBCommons/utility/z_QuickSort.bas

31 lines
847 B
QBasic
Raw Permalink Normal View History

2024-06-07 20:46:40 +03:00
Attribute VB_Name = "z_QuickSort"
Option Explicit
Public Function QuickSort(ByRef ids() As Variant, ByVal low&, ByVal high&, values As Collection)
Do While low < high
Dim pivot&: pivot = QPartition(ids, low, high, values)
Call QuickSort(ids, low, pivot - 1, values)
low = pivot + 1
Loop
End Function
Private Function QPartition(ByRef ids() As Variant, low&, high&, values As Collection) As Long
Dim pivot As Variant: pivot = ids(high)
Dim smallest&: smallest = low - 1
Dim n&
Dim tmp$
For n = low To high - 1
If values(CStr(ids(n))) < values(CStr(pivot)) Then
smallest = smallest + 1
tmp = ids(smallest)
ids(smallest) = ids(n)
ids(n) = tmp
End If
Next n
smallest = smallest + 1
tmp = ids(smallest)
ids(smallest) = ids(high)
ids(high) = tmp
QPartition = smallest
End Function