Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 213 – Large Scale Programming Lecture 24: Radix & Bucket Sorts.

Similar presentations


Presentation on theme: "CSC 213 – Large Scale Programming Lecture 24: Radix & Bucket Sorts."— Presentation transcript:

1 CSC 213 – Large Scale Programming Lecture 24: Radix & Bucket Sorts

2 Today’s Goal Discuss two new ways with which to sort data Very different than other forms of sorts Can * be a much faster method of sorting Follows simple pattern, but confusing to learn

3 Bucket-Sort Uses, B, array of Sequences (e.g., buckets) Sorts Sequence, S, in two phases: 1. Remove first Entry,, in S and add to B[k] 2. For i  0, …, B.size ()-1, move entries from bucket B[i] to end of S

4 Bucket-Sort Example Suppose keys range from [0, 9] 7, d1, c3, a7, g3, b7, e 1, c3, a3, b7, d7, g7, e Phase 1 Phase 2 012345689 B 1, c7, d7, g3, b3, a7, e  S 7   S

5 Bucket-Sort Algorithm Algorithm bucketSort(Sequence S, Comparator c) B  new Sequence[c.getMaxKey()] // instantiate the Sequence at each index within B while  S.isEmpty() do // Phase 1 entry  S.removeFirst() B[c.compare(entry, null)].insertLast(entry) for i  0 to B.length - 1 // Phase 2 while  B[i].isEmpty() do entry  B[i].removeFirst() S.insertLast(entry) return S

6 Bucket-Sort Properties Keys indices into array Must be non-negative integers Does not require external Comparator Sort is stable Two entries with same key keep relative ordering Bubble-sort & Merge-sort also stable

7 Bucket-Sort Extensions Extend Bucket-sort with Comparator Specify maximum number of buckets C.compare(key, null) returns index for key For Integer keys from a – b: Comparator maps k to k – a For Boolean keys, Comparator returns: 0 when the key is false 1 when the key is true

8 Bucket-Sort Extensions Use Bucket-sort with any keys Keys must be from bounded set, D, of values D could be U.S. states, molecular structures, To- Whack items on assassins hit list… Comparator ranks each value in D Rank states alphabetically or by admission order Ranks used as index into bucket array, B

9 d -Tuples Combination of d keys (k 1, k 2, …, k d ) k i is “ i- th dimension of the tuple” Example: Point p = ( x, y ) is 2-tuple x is value of 1 st dimension y is value of 2 nd dimension

10 Lexicographic Order Order of d- tuples defined recursively : (x 1, x 2, …, x d )  (y 1, y 2, …, y d )  x 1  y 1  ( x 1   y 1  (x 2, …, x d )  (y 2, …, y d )) Lexicographic order of 2-tuples? (3, 4) (7, 8) (3, 2) (1, 4) (4, 8) (1, 4) (3, 2) (3, 4) (4, 8) (7, 8)

11 Lexicographic Sorting Uses d calls to stable sorting algorithm Each call sorts along single dimension of tuple So must sort from smallest dimension to largest Algorithm lexicographicSort(Sequence s, Comparator c, Sort stableSort) for i  c.size() downto 1 stableSort.sort(s, c, i) return s

12 Radix-Sort Lexicographical sort using Bucket-sort Good for tuples where each dimension made into index in the range [0, N -1] Compare each character in two Strings Compare each bit in two Integers Requires modification to comparator key still first parameter to compare But, dimension now passed as second parameter

13 Radix-Sort for Integers Represent Integer as a tuple of bits: 62 10 = 111110 2 04 10 = 000100 2 With decimal representation, need 10 buckets With binary representation, need 2 buckets Radix-sort runs in O(bn) time b is the length of longest element in input For 32-bit integers, b = 32 Takes O(32n) time ≈ O ( n ) time!

14 Radix-Sort for Integers Algorithm binaryRadixSort(Sequence S, Comparator c) for i  0 to c.size() bucketSort(S, 2, i, c) return S Value of the i th bit of Integer k is: (( k >> i ) & 1)

15 Example Sorting a sequence of 4-bit integers 1001 0010 1101 0001 1110 0010 1110 1001 1101 0001 1001 1101 0001 0010 1110 1001 0001 0010 1101 1110 0001 0010 1001 1101 1110

16 For Friday… Come with questions to review for Midterm Last chance to ask me about material Also have sort-based problems to discuss


Download ppt "CSC 213 – Large Scale Programming Lecture 24: Radix & Bucket Sorts."

Similar presentations


Ads by Google