1 / 87

Sorting in Linear Time

Sorting in Linear Time. Lecture 5 Asst. Prof. Dr. İlker Kocabaş. How fast can we sort?. Decision-tree example. Decision-tree example. Decision - tree example. Decision-tree example. Decision-tree example. Decision-tree example. Lower Bound for decision - tree sorting.

evansscott
Download Presentation

Sorting in Linear Time

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. Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

  2. How fast can we sort?

  3. Decision-tree example

  4. Decision-tree example

  5. Decision-treeexample

  6. Decision-tree example

  7. Decision-tree example

  8. Decision-tree example

  9. LowerBoundfordecision-treesorting

  10. Lowerboundforcomparisonsorting

  11. Sorting in linear time • Counting something, likehistograms, can help us to sort an array. • clr = getcolor(x,y) • count[ clr ]++; Limitation: count[0..255]

  12. Sorting in linear time

  13. Counting sort COUNTING-SORT(A,B,k) // Initialize aux. store // Count how many times occurred. // Cumulative sums of aux. store // ???

  14. Counting-sort example

  15. Counting-sortexample

  16. Counting-sort example

  17. Counting-sort example

  18. Counting-sort example

  19. Counting-sort example

  20. Counting-sort example

  21. Counting-sort example

  22. Counting-sort example

  23. Counting-sort example

  24. Counting-sort example

  25. Counting-sort example

  26. Counting-sort example

  27. Counting-sort example

  28. Counting-sort example

  29. Analysis

  30. Running Time

  31. Stable sorting

  32. Countingsortarray size limitation problem • If we need sort 32 bits integers, size of count array 232 = 4GB ! • Unsorted array size is usually smaller than this (1KB << 4GB) • Solution... Use smaller parts like digits and sort these parts particularly : Radix sort

  33. Radix sort

  34. Operation of radix sort

  35. Analysis of radix sort • Given n d-digit number in which each digit can take on up to k-possible values, the running time of RADIX-SORT sorts these numbers is (d*(n+k))

  36. Analysis of radix sort • Assume counting sort is the auxilary sort. • Sort n computer words of b-bits each. • Each word can be viewed as having d=b/r (base-2r) digits. • Example: Each key having d-digits of r bits. • Let d=4, r=8 (b=32) then each digit is an integer in the range 0 to 2r-1 = 28-1=255.

  37. Analysis of radix sort: Choosing r • How many passes should we make? • Each word of length b=rd bit is broken into r-bit pieces. • For a given word length of b, the running time of an array of length n that is T(r|n,b) can be minimized with respect to r.

  38. Analysis of radix sort: Choosing r

  39. Analysis of radix sort: Choosing r

  40. Conclusions

  41. Whataboutfloatingpoints? • Integer can countable or divisible to parts. • How can speed up sorting array of floating point numbers. • We can group into k-parts • For numbers beetween 0.0 ≤ f ≤ 1.0 • k*f have an integer part like 8.71 • int(k*f) is part number. • k can be length of array.

  42. Bucket Sort

  43. Bucket Sort A B 1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 9 10

  44. Bucket Sort Running time:

  45. Bucket Sort

  46. Bucket Sort

  47. Bucket Sort

  48. Bucket Sort

  49. Bucket Sort

  50. Bucket Sort

More Related