Presentation is loading. Please wait.

Presentation is loading. Please wait.

Noncomparison Based Sorting

Similar presentations


Presentation on theme: "Noncomparison Based Sorting"— Presentation transcript:

1 Noncomparison Based Sorting
All the sorting algorithms we have seen assume binary comparisons as the basic primative Is x before y?  Suppose we had a set of n integers that range from 1…n, no two integers the same, and an array of length n. How long would it take to sort this array? What if we had a set of integers, all between 1…n in value, and we had duplicates? How can we tell how many of each integer we have? Can we write an algorithm that runs in O(n)?

2 Bucketsort Suppose we are sorting n numbers from 0 to m, where we know the numbers are approximately uniformly distributed.   Interval = high – low + 1 / number of buckets We can set up n buckets, each responsible for an interval of m/n numbers from 1 to m Given an input number x, it belongs in bucket number x – low /interval.

3 Now to find which bucket: (arr[n] – arr[smallest] )/ bucketrange
Example: Array of numbers: 5,18,13,8,11, numbers, so 6 buckets Range each bucket will hold is calculated as follows: Bucket range: (high – low + 1)/n or ( )/6, so bucket range is 3 [ ] [ ] [ ] [ ] [ ] [ ] Now to find which bucket: (arr[n] – arr[smallest] )/ bucketrange (5-1)/3 (round down) (18-1)/3 (13-1)/3… [ 1 ] [ 5 ] [8 ] [11 ] [13 ] [18 ]

4 Analysis If we use an array of buckets, each item gets mapped to the right bucket in O(1) time. With uniformly distributed keys, the expected number of items per bucket is 1. Thus sorting each bucket takes O(1) time! The total effort of bucketing, sorting buckets, and concatenating the sorted buckets together is O(n). And no comparisons!

5 Bucket sort with multiple entries in a bucket
What if we have multiple entries with the same key? For example, what if we were sorting names? All Smiths would end up in the same bucket We have no idea how many Smiths there will be. Solution? What would be a worst case for bucketSort number-wise?

6 Bucket Sort with Linked Lists
We make an array of linked lists. We move each new element into the list in the appropriate order. (What type of sort is this?) Then, when done, we just walk down the array and append each list. Result – sorted array When does BucketSort work best? Worst?

7 Algorithm BUCKET_SORT (A, range, B) n = length [A] m = range
for (int i = 1; i<n; i++) {     Insert A[i] into B[(A[i]*n)/m] using Insertion sort } Concatenate the lists B[0], B[1], . . B[n-1] together in order.

8 Radix Sort What if we have very large key values?
Think about the decimal representation of a number: x = a + 10*b + 100*c +1000*d +… a,b,c,d,… are all single digit integers (0…9) Now we can do a bucketsort on a,b,c,d We do a bucketsort on a, then we do a bucketsort on b, then c, then d, etc. (smallest to largest). Why smallest first?

9 Radix Sort in action Let’s try it: 427, 496, 834, 222, 333, 444, 595, 582, 767, 294 First pass: 222, ,444, ,767 Second Pass: 222, , ,594,496 Third Pass: 222, , 444, ,

10 Try: (missing left digits are 0s)
647, 315, 16, 14,359, 453,203,235 First Pass: 453, , Second Pass: ,315, ,359 Third Pass: 14, , ,359,

11 Radix Sort Analysis The algorithm takes O(n) time per sort.
There are k = digit length bucket sorts (3 digits) So the total time is O(n*k)


Download ppt "Noncomparison Based Sorting"

Similar presentations


Ads by Google