210 likes | 224 Views
Counting Sort is a stable sorting algorithm that efficiently sorts a list of records stored in an array based on their keys in the range of 1 to k.
E N D
Linear sorts Counting sort Radix sort cs333/cutler Counting sort
Pigeonhole-Sort( A, k)//keys range 1..k, A[1..n] fori 1 to k //initialize C C[i ] 0 forj 1 tolength[A] C[A[ j ] ] C[A[ j ] ] + 1 //Count keys q 1 forj 1to k //rewrite A while C[j]>0 A[q] j C[ j ] C[ j ]-1 q q+1 Pigeonhole Sort cs333/cutler Counting sort
Counting sort • Problem: Sort n records stored in A[1..n] • Each record contains a key and data • All keys are in the range of 1 to k • Main idea: • Count in C, number records with key = i, i = 1,…, k. • Use counts in C to compute the offset in sorted B of record with key i for i = 1,…, k. • Copy A into sorted B using and updating (decrementing) the computed offsets. To make the sort stable we start at last position of A. cs333/cutler Counting sort
Counting sort • Additional Space • The sorted list is stored in B • Additional array C of size k • Note: Pigeonhole sort does not require array B cs333/cutler Counting sort
How shall we compute the offsets? • Assume C[1]= 3 (then 3 records with key=1 should be stored in positions 1, 2, 3 in the sorted array B). We keep the offset for key 1 = 3. • Let C[2]=2 (then 2 records with key=2 should be in stored in positions 4, 5 in B). • We compute the offset for key 2 to be (C[2] + offset for key 1) = 2 +3 = 5 • In general offset for key i is (C[i] + offset for key i-1). cs333/cutler Counting sort
Counting-Sort( A, B, k) fori 1 to k //initialize C C[i ] 0 forj 1 tolength[A] C[A[ j ] ] C[A[ j ] ] + 1 //Count keys fori 2 tok C[i ] C[i ] +C[i -1] //Compute offset forj length[A] downto 1 //copy B [ C[A[ j ] ] ] A[ j ] C[A[ j ] ] ] C [A[ j ] ] –1//update offset Counting Sort cs333/cutler Counting sort
Counting sort B A C C C 3 Clinton 4 Smith 1 Xu 2 Adams 3 Dunn 4 Yi 2 Baum 1 Fu 3 Gold 1 Lu 1 Land 1 Lu 1 Land 3 Gold 1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8 9 10 11 0 0 0 0 4 2 3 2 (4)(3)2 6 (9)8 11 1 2 3 4 1 2 3 4 1 2 3 4 finalcounts "offsets" Original list Sorted list cs333/cutler Counting sort
Analysis: • O(k + n) time • What if k = O(n) • Requires k + n extra storage. • Stable sort: Preserves the original order of equal keys. • Is counting sort stable? • Is counting sort good for sorting records with 32 bit keys? cs333/cutler Counting sort
Radix Sort Some history The algorithm cs333/cutler Counting sort
Hollerith’s punched cards • Hollerith devised what was to become the computer punched card • Each card has 12 rows and 80 columns • Each column represents a single alphanumeric character or symbol. • The card punching machine punches holes in some of the 12 positions of each column cs333/cutler Counting sort
A punched card cs333/cutler Counting sort
Card punching machine IBM card punching machine cs333/cutler Counting sort
Hollerith’s tabulating machines • As the cards were fed through a "tabulating machine," pins passed through the positions where holes were punched completing an electrical circuit and subsequently registered a value. • The 1880 census in the U.S. took seven years to complete • With Hollerith's "tabulating machines" the 1890 census took the Census Bureau six weeks • Through mergers company’s name - IBM cs333/cutler Counting sort
Card sorting machine IBM’s card sorting machine cs333/cutler Counting sort
Radixsort • Main idea • Break key into “digit” representation key = id, id-1, …, i2, i1 • "digit" can be a number in any base, a character, etc • Radix sort: for i= 1 to d sort “digit” i using a stable sort • Analysis : (d (stable sort time)) where d is the number of “digit”s cs333/cutler Counting sort
Radix sort • Which stable sort? • Since the range of values of a digit is small the best stable sort to use is Counting Sort. • When counting sort is used the time complexity is (d (n +k )) where k is the range of a "digit". • When k O(n), (d n) cs333/cutler Counting sort
Radix sort- with decimal digits 910 321 572 294 326 178 368 139 139 178 294 321 326 368 572 910 1 2 3 4 5 6 7 8 178 139 326 572 294 321 910 368 910 321 326 139 368 572 178 294 Sorted list Input list cs333/cutler Counting sort
Lemma 8.4 • Given nb-bit numbers and any positive integer r<=b, radix sort correctly sorts these numbers in ((b/r)(n + 2r)) • Proof • Divide the number into b/r “digits”. • Each “digit” has r bits and a range 0 to 2r-1. • Radix sort executes b/r counting sorts. • Each counting sort is (n + 2r) • So the total is ((b/r)(n + 2r)) cs333/cutler Counting sort
Radix sort with unstable digit sort 13 17 1 2 17 13 17 13 Input list Since unstable and both keys equal to 1 List not sorted cs333/cutler Counting sort
Is Quicksort stable? 48 55 51 48 55 51 1 2 3 51 55 48 KeyData After partition of 0 to 2 After partition of 1 to 2 • Note that data is sorted by key • Since sort unstable cannot be used for radix sort cs333/cutler Counting sort
Is Heapsort stable? 51 55 51 51 55 1 2 Heap Sorted 55 KeyData Complete binary tree, and max heap After swap • Note that data is sorted by key • Since sort unstable cannot be used for radix sort cs333/cutler Counting sort