1 / 23

Lecture 6

Lecture 6. Linear Time Sorting. Agenda. Review sorting Algorithms (How fast can we sort ?) Decision Tree model Counting Sort Algorithm Example Analysis Radix Sort Algorithm Example Analysis. Review.

michi
Télécharger la présentation

Lecture 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 6 Linear Time Sorting

  2. Agenda • Review sorting Algorithms • (How fast can we sort?) • Decision Tree model • Counting Sort Algorithm • Example • Analysis • Radix Sort Algorithm • Example • Analysis

  3. Review • All the sorting algorithms we have seen so far are comparison sorts: only use comparisons to determine the relative order of elements. • The best worst-case running time that we’ve seen for comparison sorting is Q(nlgn).

  4. Is O(nlgn)the best we can do? • Each internal node is labeled i:jfor i, j ∈{1, 2,…, n}. • The left subtree shows subsequent comparisons if ai≤aj. • The right subtree shows subsequent comparisons if ai≥aj.

  5. Example Sort <9,4,6>

  6. Decision-tree model • A 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.

  7. Decision-tree model • Theorem: • Any decision tree that can sort n elements must have height Ω(nlgn).

  8. Sorting in linear time • Counting sort: • No comparisons between elements. • Input: A[1 . . n], where A[j]∈{1, 2, …, k}. • Output: B[1 . . n], sorted. • Auxiliary storage: C[1 . . k].

  9. Counting sort

  10. LOOP1

  11. LOOP2

  12. LOOP3

  13. LOOP4

  14. Analysis

  15. Analysis • If k= O(n), then counting sort takes Θ(n) time. • But, sorting takes Ω(nlgn) time! • Where’s the fallacy? Answer: • Comparison sorting takes Ω(nlgn) time. • Counting sort is not a comparison sort. • In fact, not a single comparison between elements occurs!

  16. Radix sort • Digit by digit Sort • Idea: Sort on least significant digit first with auxiliary stable sort

  17. Rules to sort • Assume that the numbers are sorted by their low-order t –1digits. • Two numbers that differ in digit are correctly sorted. • Two numbers equal in digit are put in the same order as the input ⇒correct order.

  18. Example Sort {329,457,657,839,436,720,355}

  19. Analysis for Radix Sort • Assume counting sort is the auxiliary stable sort. • Sort ncomputer words of b bits each. • Each word can be viewed as having b/r base-2rdigits. • Example:32-bit word • r= 8 ⇒ b/r=4 passes of counting sort on base-28 digits; • or r= 16⇒b/r=2 passes of counting sort on base-216 digits.

  20. Analysis for Radix Sort • Recall: Counting sort takes Θ(n + k)time to sort n numbers in the range from 0to k –1. • If each b-bit word is broken into r-bit pieces, each pass of counting sort takes Θ(n + 2r)time. • Since there are b/r passes, we have • Chooser to minimize T(n,b) • Increasing r means fewer passes, • but as r > lg n, the time grows exponentially.>

  21. Analysis for Radix Sort • Minimize T(n,b) by differentiating and setting to 0. • Or just observe that we don’t want 2r> n, and there’s no harm asymptotically in choosing r as large as possible subject to this constraint. • Choosing r= lg n implies T(n,b)=Θ(bn/lgn). • For numbers in the range from 0 to nd–1, we have b=d lgn⇒radix sort runs in Θ(dn) time.

  22. Conclusion • In practice, radix sort is fast for large inputs, as well as simple to code and maintain.

  23. Thanks

More Related