4 Private Function Partition(ByRef A() As Variant, ByVal Lb As Long, ByVal Ub As Long) _
12 ' partition array[lb..ub]
14 ' select pivot and exchange with 1st element
15 p = Lb + (Ub - Lb) \ 2
19 ' sort Lb+1 .. Ub based on pivot
25 While j > i And A(j) > pivot
28 While i < j And A(i) < pivot
30 If i >= j Then Exit Do
38 ' pivot belongs in A(j)
44 Public Sub QuickSort(ByRef A() As Variant, ByVal Lb As Long, ByVal Ub As Long)
47 ' sort array A(lb..ub)
50 ' quickly sort short lists
51 If (Ub - Lb <= 12) Then
52 Call InsertSort(A, Lb, Ub)
56 ' partition into two segments
57 m = Partition(A, Lb, Ub)
59 ' sort the smallest partition to minimize stack requirements
60 If m - Lb <= Ub - m Then
61 Call QuickSort(A, Lb, m - 1)
64 Call QuickSort(A, m + 1, Ub)