Public Sub QSort(first As Integer, last As Integer, maindata() As Variant)
Dim pivot As Variant
'Sort Elements First to Last of maindata
If first < last Then
'assign low and high
Low = first
High = last + 1
'loop begins by reducing high
pivot = maindata(Low) 'maindata(low) may now empty
Do
Do
High = High - 1 'reduce high
If Low = High Or maindata(High) < pivot Then Exit Do
Loop
If Low = High Then Exit Do 'finished
maindata(Low) = maindata(High) 'maindata(high) now empty
Do
Low = Low + 1 'increment low
If Low = High Or maindata(Low) > pivot Then Exit Do
Loop
If Low = High Then Exit Do 'finished
maindata(High) = maindata(Low) 'maindata(low) now empty
Loop
maindata(High) = pivot 'place pivot in proper position
'Call QuickSort recursively to sub divide the two partitions
'below and above the pivot.
Call QSort(first, High - 1, maindata())
Call QSort(High + 1, last, maindata())
End If
End Sub |