Sorting algorithms

Straight insertion sort

    for j := 2 to n do
       i := j-1
       K := Kj
       while ( i ≥ 1 and K < Ki ) do
          Ki+1 := Ki
          i := i-1
       Ki+1 := K

C, M, T are Θ(n2)
S is Θ(1)

Binary insertion sort

Look for correct place by binary search.

C is Θ(n lg n)
M, T are Θ(n2)
S is Θ(1)

Shellsort

[ Knuth Vol 3, pp. 84-95; Baase pp. 197-200 ]

Divide input records into δ tables, and sort each table by straight insertion.
Do this for a decreasing sequence of values for δ with the last having value 1.

DELTA-SORT
    for table := 1 to δ do
       j := table + δ
       while (j ≤ n) do
          i := j - δ
          K := Kj
          while ( i ≥ 1 and K < Ki ) do
             Ki+δ := Ki
             i := i - δ
          Ki+δ := K
          j := j + δ

If δ = {2i-1} then T = Θ(n3/2)
If δ = {2p3q} then T = Θ(n lg2n)


Dan Hirschberg
Last modified: Oct 28, 2003