Sorting in linear timeA-PDF Text Replace DEMO: Purchase from to remove the watermark
2Design And Analysis of AlgorithmsDecision-tree modelA decision tree can model the execution of any comparison sort:•One tree for each input size n. •View the algorithm as splitting whenever it compares two elements.•The tree contains the comparisons along all possible instruction traces.•The running time of the algorithm =the length of the path taken.•Worst-case running time =height of tree.
Design And Analysis of Algorithms6)How many leaves does a decision tree have?
4Design And Analysis of AlgorithmsTheorem : Any decision tree that sorts n elements has height Ω(n lg n).•Proof:There are n! possible outcomes. Thus there are at least n! leaves in the decision tree.A binary tree of height h has at most 2hleaves.Thus, 2h ≥n!. We have h≥lg(n!) ≥Θ(n lg n).Note : Heapsort and mergesort are asymtotically optimal comparison sorts.
5Design And Analysis of AlgorithmsCounting sort •No comparison!•Depends on an assumption about the members to be sorted. (eg. in the range 1..k)•Time cost: O(k+n)
6Design And Analysis of AlgorithmsCOUNTING-SORT(A,B,k)1.for i←0 to k2.do C[i] ←0 3.for j←1 to length[A]4.do C[A[j]] ←C[A[j]]+15.// C[i] contains number of elements equal to i.