Transcript Radix Sort

CS 261 – Data Structures
Hash Tables
Part III: Hash like sorting algorithms
Hash Tables: Sorting
• Can create very fast sort programs using hash tables
• Unfortunately, these sorts are not general purpose:
– Only work with positive integer values (or other data that is readily
mapped into positive integer values)
• Examples:
– Counting sort
– Radix sort
Hash Table Sorting: Counting Sort
• Quickly sort positive integer values from a limited range.
– Count (tally) the occurrences of each value
– Recreate sorted values according to tally
• Example:
– Sort 1,000 integer elements with values between 0 and 19
– Count (tally) the occurrences of each value:
0123-
47
92
12
14
4567-
32
114
16
37
8 - 41
9- 3
10 - 36
11 - 92
12 13 14 15 -
43
17
132
93
– Recreate sorted values according to tally:
47 zeros, 92 ones, 12 twos, …
16 17 18 19 -
12
15
63
89
Counting Sort: Implementation
void countingSort(int [] data, int n, int max) {
int * count = (int *) malloc((max + 1) * sizeof(int));
/* Array of all possible values.*/
assert(count != 0);
for (int i = 0; i < n; i++) // Count the occurrences
count[data[i]]++;
// of each value.
// Count holds the number of occurrences of numbers from 0 to max.
int i = 0;
// Now put values
for (int j = 0; j < max; j++)
// back into the array.
for (int k = count[j]; k > 0; k--) data[i++] = j;
} // End of method sort.
}
Radix Sorting
Another Specialized Sorting Algorithm with historical
ties to punchcards
QuickTime™ and a
Photo - JPE G decompressor
are needed to see this picture.
Sorting Punchcards
• It was far to easy to drop a tray of cards, which could be a
disaster
• Convention became to put a sequence number on card,
typically in positions 72-80.
• Could then be rebuilt by sorting on these positions.
• A machine called a sorted could be used for this purpose
A mechanical Sorter -sorted only 1 column
How to use a mechanical sorter
• First sort on column 80
• Then collect the piles, keeping them in order, and sort on
column 79.
• Repeat for each of the columns down to 72.
• At the end, the result is completely sorted.
• Don’t believe me? Let’s try it.
Hash Table Sorting: Radix Sort
• Sorts positive integer values over any range
• Hash table size of 10 (0 through 9)
• Values are hashed according to their least significant digit
(the “ones” digit)
• Values then rehashed according to the next significant
digit (the tens digit) while keeping their relative ordering
• Process is repeated until we run out of digits
• Can also sort by hashing on:
– Characters in a String  table size of 26 (‘A’ through ‘Z’)
– Bytes in an integer  table size of 256 (as opposed to 10 above)
Radix Sort: Example
Data:
624
762
852
426
197
987
146
415
301
730
78
593
Bucket
0
1
2
3
4
5
6
7
8
9
Pass1
730
301
762 - 852
593
624
415
426 - 146
197 - 987
78
269
Pass2
301
415
624 - 426
730
146
852
762 - 269
78
987
593 - 197
Pass3
_
78
146 - 197
269
301
415 - 426
593
624
730 - 762
852
987
269
Your turn
• Questions?
• Ok. Now you get to try this with a different set of values.