35 - 04/20/09 20:17:40 1 35 CS 61B: Lecture 36 Monday,...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 04/20/09 20:17:40 1 35 CS 61B: Lecture 36 Monday, April 20, 2009 Counting Sort------------- If the items we sort are naked keys, with no associated values, bucket sort can be simplified to become _counting_sort_. In counting sort, we don’t need queues at all; we need merely keep a count of how many copies of each key we have encountered. Suppose we sort 6 7 3 0 3 1 5 0 3 7: 0 1 2 3 4 5 6 7----------------------------------------------------------------- counts | 2 | 1 | 0 | 3 | 0 | 1 | 1 | 2 |----------------------------------------------------------------- When we are finished counting, it is straightforward to reconstruct the sorted keys from the counts: 0 0 1 3 3 3 5 6 7 7. Counting Sort with Complete Items--------------------------------- Now let’s go back to the case where we have complete items (key plus associated value). We can use a more elaborate version of counting sort. The trick is to use the counts to find the right index to move each item to. Let x be an input array of objects with keys (and perhaps other information). 0 1 2 3 4 5 6 7 8 9 ----------------------------------------------------------------------- x | . | . | . | . | . | . | . | . | . | . |----|------|------|------|------|------|------|------|------|------|--- v v v v v v v v v v ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- | 6 | | 7 | | 3 | | 0 | | 3 | | 1 | | 5 | | 0 | | 3 | | 7 |----- ----- ----- ----- ----- ----- ----- ----- ----- ----- Begin by counting the keys in x. for (i = 0; i < x.length; i++) { counts[x[i].key]++; } Next, do a _scan_ of the "counts" array so that counts[i] contains the number of keys _less_than_ i. 0 1 2 3 4 5 6 7----------------------------------------------------------------- counts | 0 | 2 | 3 | 3 | 6 | 6 | 7 | 8 |----------------------------------------------------------------- total = 0; for (j = 0; j < counts.length; j++) { c = counts[j]; counts[j] = total; total = total + c; } Let y be the output array, where we will put the sorted objects. counts[i] tells us the first index of y where we should put items with key i. Walk through the array x and copy each item to its final position in y. When you copy an item with key k, you must increment counts[k] to make sure that the next item with key k goes into the next slot. for (i = 0; i < x.length; i++) { y[counts[x[i].key]] = x[i]; counts[x[i].key]++; }--------------------- --------------------------------- y |.|.|.|.|.|.|.|.|.|.| counts | 0 | 2 | 3 | 3 | 6 | 6 | 8 | 8 |---------------|----- --------------------------------- v 6---------------------...
View Full Document

This note was uploaded on 02/21/2010 for the course CS 61B taught by Professor Canny during the Spring '01 term at Berkeley.

Page1 / 2

35 - 04/20/09 20:17:40 1 35 CS 61B: Lecture 36 Monday,...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online