{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Bucket-Sort and Radix-Sort 1 Bucket-Sort and Radix-Sort 0 1 2 3 4 5 6 7 8 9 B 1, c 7, d 7, g 3, b 3, a 7, e Bucket-Sort and Radix-Sort 2 Bucket-Sort (§10.5.1) Let be S be a sequence of n (key, element) items with keys in the range [0, N 1] Bucket-sort uses the keys as indices into an auxiliary array B of sequences (buckets) Phase 1 : Empty sequence S by moving each item ( k , o ) into its bucket B [ k ] Phase 2 : For i = 0, , N 1 , move the items of bucket B [ i ] to the end of sequence S Analysis: Phase 1 takes O ( n ) time Phase 2 takes O ( n + N ) time Bucket-sort takes O ( n + N ) time Algorithm bucketSort ( S, N ) Input sequence S of (key, element) items with keys in the range [0, N 1] Output sequence S sorted by increasing keys B array of N empty sequences while ¬ S.isEmpty () f S.first () ( k , o ) S.remove ( f ) B [ k ] .insertLast (( k , o )) for i 0 to N 1 while ¬ B [ i ] .isEmpty () f B [ i ] .first () ( k , o ) B [ i ] .remove ( f ) S.insertLast (( k , o )) Bucket-Sort and Radix-Sort 3 Example Key range [0, 9] 7, d 1, c 3, a 7, g 3, b 7, e 1, c 3, a 3, b 7, d 7, g 7, e Phase 1 Phase 2 0 1

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}