Introduction to Algorithm - Summary of Chapter 8 - Sorting in Linear Time

Introduction of The Chapter

Comparison sorts : The sorted order is based only on comparison between the input elements.

Stable : Numbers with the same value appear in the output array in the same order as they do in the input array.
The property of stability is important only when there are the satellite data carried by the key element.

In place : During the execution of the sorting, only a constant number of elements of the input array are ever stored outside the array.

Any sort comparison must make Ω (n lg n) comparison in the worst case to sort n elements. The sort comparisons which we have learnt are insertion sort, merge sort, heap sort and quick sort.


Lower bounds for sorting

comparison sort algorithm only compare between element elements to gain order without inspecting the values of the elements or gaining order information about the in any other way.

The figue of decision-tree model

decision tree : A full binary tree that represents the comparison between elements that are performed by the particular sorting algorithm operating on an input of a given size.

The particular execution of the sorting algorithm corresponds to tracing a simple path from the root of the decision tree down to a leaf. So, each leaf corresponds a actual execution of the sorting algorithm.

The worst case number of comparisons for a given comparison sorting algorithm equals the height of its decision tree.

Theorem 8.1
Any comparison sort algorithm requires Ω (n lg n) comparison in the worst case.

Corollary 8.2
heap sort and merge sort are asymptomatically optimal comparison sort.


Counting sort

Counting sort : assumes that each of the n input elements is an integer in the range of 0 to k. It determines the number of element less than the each of the input integer x, and uses the information to place element x directly into to its position in the output array.The counting sort uses the actual values of the elements to index into an array.

The show of the counting sort execution

Counting-Sort(A, B, k)
    let C[0..k] be a new array
    for i=0 to k
        C[i] = 0
    for i=0 to A.length 
        C[A[i]] = C[A[i]] + 1
    //now C[i] contains the number of elements equal to i.
    for i=0 to k
        C[i] = C[i] + C[i-1]
    //now C[i] contains the number of elements equal or less than i.
    for i=A.length downto 1
        B[C[A[i]]] = A[i]
        C[A[i]] = C[A[i]] - 1

The counting sort time is Θ ( n + k ) = Θ (n).Not in place. stability .


Radix sort

Radix dort : regards the each of n input elements as a d-digit integer. It sort on the least-significant digit first, then it sort the entire deck again on the second-least significant digit. The process continues until elements have been sorted on all d digits.

The show of the radix sort execution

Radix-Sort (A, d)
    for i=1 to d
        use a stable sort to sort array A on digit i
    //digit 1 is the least significant digit

The Radix-Sort must be stable, but in place or not depend on choosing of the called sort algorithm.

Lemma 8.3
Given n d-digit numbers in which each digit can take on up to k possible values, Radix-Sort correctly sorts these numbers in Θ ( d ( n+k) ) time if the stable sort it uses takes Θ ( n+k ) time.

Lemma 8.4
Given n b-bit number and any positive integer rb , Radix-Sort sorts these numbers in Θ((b/r)(n+2r)) time if the stable sort it uses take on Θ(k+n) time for inputs in the range of 0 to k.

How to choose the value of r to make the expression ((b/r)(n+2r)) minimize ? If b<lgn , choosing r=b , else choosing r=lgn .


Bucket sort

Bucket sort : divides the interval [01) into n equal-sized subintervals, or buckets ,and then distributes the n input number into the buckets.To produce the output, we simple sort the numbers in each bucket and then go through the buckets in order, listing the elements in each.

The show of bucket sort execution

Bucket-Sort (A)
    let B[0..n-1] be a new array 
    n = A.length 
    for i=0 to n-1
        make B[i] an empty list
    for i=1 to n
        insert A[i] into list B[ |nA[i]| ] 
        //B[ $\lfloor nA[i] \rfloor$ ] 
    for i=0 to n-1
        sort list B[i] with insertion sort 
    concatenate the the list B[0..n-1] together in order 

The bucket sort expection of running time is Θ(n)+nO(21/n)=Θ(n) , but the worst-time is Θ(n2) .
Stability . Not in place .

Some of above content refere to “Introduction to Algorithm”.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值