Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 13 - Wednesday CS221.

Similar presentations


Presentation on theme: "Week 13 - Wednesday CS221."— Presentation transcript:

1 Week 13 - Wednesday CS221

2 Last time What did we talk about last time?
Priority queue implementation Heap sort Timsort Sort visualizations

3 Questions?

4 Project 4

5 Assignment 7

6 Counting Sort

7 Counting sort justification
Lets focus on an unusual sort that lets us (potentially) get better performance than O(n log n) But, I thought O(n log n) was the theoretical maximum!

8 Counting sort paradigm
You use counting sort when you know that your data is in a narrow range, like, the numbers between 1 and 10 or even 1 and 100 As long as the range of possible values is in the neighborhood of the length of your list, counting sort can do well Example: 150 integer grades between 1 and 100 Doesn’t work for sorting double or String values

9 Counting sort algorithm
Make an array with enough elements to hold every possible value in your range of values If you need 1 – 100, make an array with length 100 Sweep through your original list of numbers, when you see a particular value, increment the corresponding index in the value array To get your final sorted list, sweep through your value array and, for every entry with value k > 0, print its index k times

10 Counting sort example We know our values will be in the range [1,10]
Our example array: Our values array: The result: 6 2 10 1 7 1 3 2 4 5 6 7 8 9 10 1 2 6 7 10

11 Counting Sort Implementation

12 How long does it take? It takes O(n) time to scan through the original array But, now we have to take into account the number of values we expect So, let’s say we have m possible values It takes O(m) time to scan back through the value array, with O(n) additional updates to the original array Time: O(n + m)

13 Radix Sort

14 Radix sort We can “generalize” counting sort somewhat
Instead of looking at the value as a whole, we can look at individual digits (or even individual characters) For decimal numbers, we would only need 10 buckets (0 – 9) First, we bucket everything based on the least significant digits, then the second least, etc. The book discusses MSD and LSD string sorts, which are similar

15 Radix sort Pros: Best, worst, and average case running time of O(nk) where k is the number of digits Stable for least significant digit (LSD) version Simple implementation Cons: Requires a fixed number of digits to be checked Unstable for most significant digit (MSD) version Works poorly for floating point and non-digit based keys Not in-place

16 Radix sort algorithm For integers, make 10 buckets (0-9)
Each bucket contains a queue Starting with the 1’s place and going up a place each time: Enqueue each item into the bucket whose value matches the value of the item at a particular place Starting with bucket 0, and going up to bucket 9, dequeue all the items into the original array

17 Radix sort example 7 45 54 37 108 51 1 2 3 4 5 6 7 8 9 45 54 37 108 51 51 54 45 7 37 108

18 Radix sort example part 2
51 54 45 7 37 108 1 2 3 4 5 6 7 8 9 51 54 45 37 108 7 108 37 45 51 54

19 Radix sort example part 3
7 108 37 45 51 54 1 2 3 4 5 6 7 8 9 108 51 54 45 37 7 108 37 45 51 54

20 Radix sort implementation
The examples we have shown looked as if they were using a queue for each bucket Doing so is easy but slow A faster implementation does three passes for each digit: Count how many keys go into each bucket After doing so, add up values in the count array so that the starting point of each bucket in the final array is known Copy all values into their correct bucket ranges in a temporary array Copy all values back into the original array

21 Radix Sort Implementation

22 Upcoming

23 Next time… Tries Substring search Regular expressions

24 Reminders Work on Project 4 Read sections 6.4 and 6.6


Download ppt "Week 13 - Wednesday CS221."

Similar presentations


Ads by Google