Chapter 5 Sort in linear time

Sort in linear time

  • How fast can we sort ?
    It depends on model of what you can do with the elements.

  • Familiar sort algorithm

1)quicksort(Θ(nlgn))(unstable)
2)heapsort(Θ(nlgn))(unstable)
3)merge sortr(Θ(nlgn))(stable)
4)Insertion sort(Θ(n^2)) (stable)

Question: Can we do better than Θ(nlgn)?

Comparison sort(model)

In this model, we only use comparison to determine relative order of elements.

  • e.x. sort A(a1,a2,a3)
    We can draw an tree like follows:
    在这里插入图片描述
    The oval denotes a comparison, go to the left child node if a<b for a:b.The rectangle denetes the final result.

In general ,<a1,a2,a3,…,an> has following futures:

  1. Each internal node, so every non-leaf node has a label i:j(i,j={1,2,…,n}).
  2. Compare ai versus aj.
  3. Left subtree gives subsequent conparison if ai<=aj.
  4. Right subtree gives subsequent comparison if ai>aj.
  5. Each leaf node gives a comparison <Π(1),Π(2),…Π(n)>,such that aΠ(1)<= aΠ(2) <= aΠ(3) <= …<=aΠ(n).
  • Decision tree & Conparison sorts
  1. One tree for each n.
  2. View algorithms as folks spliting whenever if makes a comparison.
  3. Tree exists comparison along aw possible institution traces.
  4. Running time (#comparison/nums of comparison) = length of path
    Worst-case time = height of the tree
  • Lower bound in decision-tree sort
    Any decision tree sorting n elements has height Ω(nlgn).
Proof: - #leaves must be >= n
       - for a binary tree ,height h->#leaves<=2^h -> n!<=2^h ->  h>=nlgn!
       - Stirling formula:  h>=nlgn!>= lg(n/e)^n = nlog(n/e) = nlgn - n = Ω(nlgn)

What’s more, merge sort and heapsort are asymptotic optimal.Randomized quicksort is too,in exception.

Counting sort - sort in linear time

  • Counting sort
Input: A[1...n] each A[i] = {1,2,3,4,..,k},which means we konw that A[i] has an upper bound.
Output:B[1...n]= Sorting of A
We need auxiliary storage:C[1,2,...k]
  • Futures:
    • apply for samll-range numners
    • a stable algorithm

Pseudocode:

for i ← 1 to k            //Initial C
            do C[i]0
        for j ← 1 to n           。// Count the number
            do C[A[j]] ← C[A[j]]+1
        for i ← 2 to k            
            do C[i] ← C[i]+C[i-1]
        for j ← n downto 1       // for stability
            do B[C[A[j]]] ← A[j]
                C[A[j]] ← C[A[j]]-1
    Ex:
        A=4 1 3 4 3         C=0 0 0 0
                            C=1 0 2 2
                            C‘=1 1 3 5
        B=0 0 3 0 0         C‘=1 1 2 5
        B=0 0 3 0 4         C‘=1 1 2 4
        B=0 3 3 0 4         C‘=1 1 1 4
        B=1 3 3 0 4         C‘=0 1 1 4
        B=1 3 3 4 4         C‘=0 1 1 3

T(n) = θ(k+n), apply for some arrays with small k.

Radix Sort(基数排序)

  • futures
    • apply for large-range sort
    • stable
  • e.x.
    329 720 720 329
    457 355 329 355
    657 436 436 436
    839 457 839 457
    436 657 355 657
    720 329 457 720
    355 839 657 839
  • Proof of correctness - induct on digit position t
  1. Assume by induction sorted on low-order t-1 digits.
  2. Sort on digit t
    -> if two elems have same Tth digit.
    stability ->same order -> sorted order
    -> if two elems have different Tth digit.
    sortedd order
  • Analysis
  1. Use counting sort for each turn
  2. suppose n integers, each n bits.(range 0~2^b-1)
  3. Split into b/r “digit”, each r bits long.
    Running time:O(b/r·(n+k))=O(b/r·(n+2^r))
    Take the derivative to get the miniunm, we kenow that T(n) = O(bn/lgn) when r is lgn bits long .
发布了80 篇原创文章 · 获赞 332 · 访问量 70万+

猜你喜欢

转载自blog.csdn.net/qq_40527086/article/details/103210563