4 Public Sub QSort(ByRef A() As Variant, ByVal Lb As Long, ByVal Ub As Long)
5 Dim lbStack(32) As Long
6 Dim ubStack(32) As Long
7 Dim sp As Long ' stack pointer
8 Dim lbx As Long ' current lower-bound
9 Dim ubx As Long ' current upper-bound
11 Dim p As Long ' index to pivot
14 Dim t As Variant ' temp used for exchanges
25 ' select pivot and exchange with 1st element
26 p = lbx + (ubx - lbx) \ 2
33 ' partition into two segments
38 If A(lbx) <= A(i) Then Exit Do
43 If A(j) <= A(lbx) Then Exit Do
47 If i >= j Then Exit Do
58 ' pivot belongs in A[j]
66 ' keep processing smallest segment, and stack largest
67 If m - lbx <= ubx - m Then