Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bucket & Radix Sorts. Efficient Sorts QuickSort : O(nlogn) – O(n 2 ) MergeSort : O(nlogn) Coincidence?

Similar presentations


Presentation on theme: "Bucket & Radix Sorts. Efficient Sorts QuickSort : O(nlogn) – O(n 2 ) MergeSort : O(nlogn) Coincidence?"— Presentation transcript:

1 Bucket & Radix Sorts

2 Efficient Sorts QuickSort : O(nlogn) – O(n 2 ) MergeSort : O(nlogn) Coincidence?

3 Comparisons N! possible orderings of N items – Represent as decision tree Decision tree to reach N! states has depth log(N!)

4 Comparisons min height ≥ Log(N!) ≥ Log(1 * 2 * 3 * … * N) ≥ Log(1) + Log(2) + … +Log(N) ≥ Log(n/2) + … + Log(N) ≥ (n/2)Log(n/2) ≥ (n/2)(Logn – Log2) = (n/2)(Logn – 1) ≥ (nLogn – n)/2 ≥  (nLogn) Just drop first half All n/2 logs are ≥ n/2 Omega– asymptotic lower bound

5 Efficient Sorts To Date QuickSort : O(nlogn) – O(n 2 ) MergeSort : O(nlogn) Coincidence? Comparison sorts can never beat nlogn Comparison based

6 Sorting with Buckets Ever used a sorting stick?

7 Bucket Sort Bucket Sort: For each item Pick correct bucket and inert item Make new List For each bucket Add contents to List Return List

8 Bucket Sort Bucket Sort: For each item Pick correct bucket and inert item Make new List For each bucket Add contents to List Return List O(1)

9 Bucket Sort Bucket Sort: For each item Pick correct bucket and inert item Make new List For each bucket Add contents to List Return List O(1) O(k) – k = num buckets O(n) – n = num items

10 Bucket Sort Bucket Sort : – O(n + k) – Sort granularity limited by buckets Perfect sort, k = range of values

11 Bucket Sort Bucket Sort : – O(n + k) – Sort granularity limited by buckets Perfect sort, k = range of values – Sort 30,000 integers perfectly 30,000 + 4,000,000,000 VS n log n 30,000 log 30,000 ≈ 450,000 4 billion buckets to represent all ints

12 Bucket Sort Bucket Sort : – O(n + k) – Sort granularity limited by buckets Perfect sort, k = range of values – Efficient if k < in relation to n Sort 4 million people in OR by Zip Code – Bucket » n = 4,000,000 k = 1000 (less than 1000 zips in OR) » Time ~4,001,000 – NLogN » 4,000,000 * log(4,000,000) ~ 4,000,000 * 22

13 Bucket Sort Perfect sort with limited buckets: – BucketSort – InsertionSort each bucket Best Case : O(N + K) Worst Case : O(N + N 2 ) – One bucket gets everything

14 Sort Sorting a real big pile alphabetically

15 Sort Sorting a real big pile alphabetically – Sort A-Z – Set aside each pile – Sort the A's by second letter Then B's, C's… – Then take AA's Sort by third letter…

16 Radix Sort Radix : Base Radix Sort – Sort digital data – Bucket sort based on each digit successively

17 Radix Sort MSD – Most Significant Digit MSD Radix Sort – Partition list based on first digit

18 Radix Sort MSD – Most Significant Digit MSD Radix Sort – Partition list based on first digit – Recursively sort on next digit

19 MSD Advantages May not examine all keys Works on variable lengths: Little extra space

20 How does it work? Radix Exchange – MSD radix sort – Partition like QuickSort, but swap on misplaced digits – Unstable http://www.cse.hut.fi/en/research/SVG/TRAKLA2/exercises/TrueRecursiveRadixExchangeSort-25.html

21 LSD Radix LSD – Least Significant Digit Work from smallest digit to largest – Hard with variable lengths – Stable!

22 How does it work? Iterative Radix Sort – Buckets used as counters – Goal is to find starting/ending point of each value

23 How does it work? Iterative Radix Sort – Buckets used as counters – Check each item add one to appropriate bucket – Compute cumulative totals from buckets – Place each item in temp array Use bucket value as index Decrement counter as we go http://www.cs.usfca.edu/~galles/visualization/RadixSort.html

24 So it wins? RadixSort : O(R*N) where R = num digits – Num digits is constant… O(N)!!

25 So it wins? RadixSort : O(R*N) where R = num digits – Num digits isn't constant in general If M distinct values and base k R = log k M O(R * N) = O(log k M * N) Only better then nlogn for specific situations where range of distinct values known and less than logN

26 Radix Summary For specific problems, runs in linear time Always depends on particulars of data – No general RadixSort algorithm Right tool for big jobs on specific sets of data


Download ppt "Bucket & Radix Sorts. Efficient Sorts QuickSort : O(nlogn) – O(n 2 ) MergeSort : O(nlogn) Coincidence?"

Similar presentations


Ads by Google