/
Bucket & Radix Sorts Bucket & Radix Sorts

Bucket & Radix Sorts - PowerPoint Presentation

tatiana-dople
tatiana-dople . @tatiana-dople
Follow
342 views
Uploaded On 2019-11-27

Bucket & Radix Sorts - PPT Presentation

Bucket amp Radix Sorts Efficient Sorts QuickSort O nlogn On 2 MergeSort O nlogn Coincidence Comparisons A decision tree to find correct ordering of 3 items Compare two items go left or right based on answer ID: 768205

sort bucket array count bucket sort count array 000 item nlogn temp radix digit log size identify work based

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Bucket & Radix Sorts" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Bucket & Radix Sorts

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

ComparisonsA decision tree to find correct ordering of 3 items: Compare two items, go left or right based on answer

ComparisonsN! possible orderings of N items N choices for first * (N - 1) choices for second * (N - 2) choices for third… Decision tree to reach N! states has depth log 2(N!)

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

Efficient Sorts To DateQuickSort : O( nlogn ) – O(n 2 ) MergeSort : O(nlogn) Coincidence? No. Comparison sorts can never beat nlogn Comparison based

Sorting Without Comparing

Sorting with BucketsEver used a sorting stick?

Bucket SortBucket Sort Sort items so they are grouped by “bucket” Items in a bucket retain original order

Bucket SortExample:Sort array by 10’s column All single digit numbers first Then 10’s, then 20’s, …

Bucket SortProcess: Make bucket count array One index for each bucket (0’s, 10’s, 20’s, 30’s)

Bucket SortProcess: For each array item Decide which bucket it belongs in Add to count for that bucket

Bucket SortProcess: For each array item Decide which bucket it belongs in Add to count for that bucket

Bucket Sort Process: For each array item Decide which bucket it belongs in Add to count for that bucket

Bucket SortProcess: For each array item Decide which bucket it belongs in Add to count for that bucket

Bucket SortTurn counts into cumulative: For each bucket i starting with 1 Count[ i] = Count[ i ] + Count[ i - 1]

Bucket Sort Turn counts into cumulative: For each bucket i starting with 1 Count[ i ] = Count[ i ] + Count[ i - 1]

Bucket Sort Turn counts into cumulative: For each bucket i starting with 1 Count[ i ] = Count[ i ] + Count[ i - 1]

Bucket SortArray: Count[x] now holds 1 + index where bucket x ends Ex: 10’s bucket will end at index 3 30’s bucket willend at index 6

Bucket SortSort: For each item i from size - 1 to 0 Identify bucket b Count[b]-- Temp[b] = Array[b]

Bucket SortSort: For each item i from size - 1 to 0 Identify bucket b Count[b]-- Temp[b] = Array[b]

Bucket Sort Sort: For each item i from size - 1 to 0 Identify bucket b Count[b]-- Temp[b] = Array[b]

Bucket Sort Sort: For each item i from size - 1 to 0 Identify bucket b Count[b]-- Temp[b] = Array[b]

Bucket Sort Sort: For each item i from size - 1 to 0 Identify bucket b Count[b]-- Temp[b] = Array[b]

Bucket Sort Sort: For each item i from size - 1 to 0 Identify bucket b Count[b]-- Temp[b] = Array[b]

Bucket Sort Sort: For each item i from size - 1 to 0 Identify bucket b Count[b]-- Temp[b] = Array[b]

Bucket Sort Sort: For each item i from size - 1 to 0 Identify bucket b Count[b]-- Temp[b] = Array[b]

Bucket SortCopy temp back to array Count now has start of each bucket:

Bucket SortBucket Sort: Make k item Count array For each item Add one to Count array bucket Turn Count Array into cumulative For each item Place into temp array, update counter Copy back temp array O(n) O(k)O(k) O(n) O(n) O(1) O(1)

Bucket SortBucket Sort : ~3n + 2k total work O(n + k) Efficient if k < is small relative to n

Bucket SortBucket Sort : O(n + k) Efficient if k < is small relative to n Sort 4 million people in OR by Zip Code Bucket n = 4,000,000 k = 1000 (less than 1000 zips in OR) Work ~4,001,000 NLogN sort Work = 4,000,000 * log(4,000,000) ~ 88,000,000

Bucket SortBucket Sort : O(n + k) Efficient if k < is small relative to n Sort 30,000 integers perfectly Bucket n = 30,000 k = 4,000,000,000 (4 billion+ different buckets) Work ~4,000,030,000 NLogN sort Work = 30,000* log(30,000) ~ 450,000

SortSorting a real big pile alphabetically

SortSorting 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…

Radix SortRadix Sort Sort digital data Bucket sort based on each digit successively

LSD RadixLSD – Least Significant Digit LSD Radix Sort : Work from smallest digit to largest Do bucket sort on that digit

How does it work?LSD Radix Sort 3 digit integers: Sort based on 1’s digits Then 10’s Then 100’s http://www.cs.usfca.edu/~galles/visualization/RadixSort.html

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

MSD Radix SortMSD – Most Significant DigitMSD Radix Sort Partition list based on first digit Recursively sort on next digit

MSD AdvantagesMay not examine all keys Works on variable lengths: Little extra space

Radix vs nlogn RadixSort : O(R*N) Where R = num digits If num digits is constant then O(R*N) == O( constant * N ) == O(N)??? But…

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 So O(R * N) = O( log k M * N)Only better then nlogn for specific situations where range of distinct values (M) known and much less than N

Radix SummaryFor 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