' shell sort Private Function ShellSeq(ByRef h() As Long, ByVal n As Long) ' establish increment sequence (see Knuth, Vol 3) Dim p1 As Long Dim p2 As Long Dim p3 As Long Dim s As Long p1 = 1 p2 = 1 p3 = 1 s = -1 Do s = s + 1 If s Mod 2 Then h(s) = 8 * p1 - 6 * p2 + 1 Else h(s) = 9 * p1 - 9 * p3 + 1 p2 = 2 * p2 p3 = 2 * p3 End If p1 = 2 * p1 Loop While 3 * h(s) < n If s > 0 Then s = s - 1 ShellSeq = s End Function Public Sub ShellSort(ByRef A() As Variant, ByVal Lb As Long, ByVal Ub As Long) Dim n As Long Dim h As Long Dim i As Long Dim j As Long Dim s As Long Dim t As Variant Dim seq(28) As Long ' sort array[lb..ub] s = ShellSeq(seq, Ub - Lb + 1) Do While s >= 0 ' sort by insertion in increments of h h = seq(s) For i = Lb + h To Ub t = A(i) For j = i - h To Lb Step -h If A(j) <= t Then Exit For A(j + h) = A(j) Next j A(j + h) = t Next i s = s - 1 Loop End Sub