1 CSC401 – Analysis of Algorithms Lecture Notes 9 Radix Sort and Selection Objectives  Introduce no-comparison-based sorting algorithms: Bucket-sort and.

Slides:



Advertisements
Similar presentations
Bucket-Sort and Radix-Sort B 1, c7, d7, g3, b3, a7, e 
Advertisements

1 Sorting in Linear Time How can we do better?  CountingSort  RadixSort  BucketSort.
CSC 213 – Large Scale Programming. Today’s Goals  Review discussion of merge sort and quick sort  How do they work & why divide-and-conquer?  Are they.
Data Structures and Algorithms
Goodrich, Tamassia Sorting, Sets and Selection1 Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
Data Structures Lecture 9 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
Merge Sort 4/15/2017 4:30 PM Merge Sort 7 2   7  2  2 7
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
© 2004 Goodrich, Tamassia QuickSort1 Quick-Sort     29  9.
Quick-Sort     29  9.
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
© 2004 Goodrich, Tamassia Selection1. © 2004 Goodrich, Tamassia Selection2 The Selection Problem Given an integer k and n elements x 1, x 2, …, x n, taken.
CSC 213 – Large Scale Programming Lecture 24: Radix & Bucket Sorts.
TTIT33 Algorithms and Optimization – Dalg Lecture 2 HT TTIT33 Algorithms and optimization Lecture 2 Algorithms Sorting [GT] 3.1.2, 11 [LD] ,
© 2004 Goodrich, Tamassia Merge Sort1 Quick-Sort     29  9.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4 Modified by: Daniel Gomez-Prado, University of Massachusetts Amherst.
Selection1. 2 The Selection Problem Given an integer k and n elements x 1, x 2, …, x n, taken from a total order, find the k-th smallest element in this.
© 2004 Goodrich, Tamassia Merge Sort1 Chapter 8 Sorting Topics Basics Merge sort ( 合併排序 ) Quick sort Bucket sort ( 分籃排序 ) Radix sort ( 基數排序 ) Summary.
CSC 213 Lecture 15: Sets, Union/Find, and the Selection Problem.
Sorting.
1 More Sorting radix sort bucket sort in-place sorting how fast can we sort?
CSC 213 Lecture 12: Quick Sort & Radix/Bucket Sort.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
Sorting Fun1 Chapter 4: Sorting     29  9.
Analysis of Algorithms CS 477/677
LECTURE 40: SELECTION CSC 212 – Data Structures.  Sequence of Comparable elements available  Only care implementation has O(1) access time  Elements.
© 2004 Goodrich, Tamassia Bucket-Sort and Radix-Sort B 1, c7, d7, g3, b3, a7, e 
Randomized Algorithms CSc 4520/6520 Design & Analysis of Algorithms Fall 2013 Slides adopted from Dmitri Kaznachey, George Mason University and Maciej.
COSC 3101A - Design and Analysis of Algorithms 6 Lower Bounds for Sorting Counting / Radix / Bucket Sort Many of these slides are taken from Monica Nicolescu,
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
1 Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
Towers of Hanoi Move n (4) disks from pole A to pole B such that a larger disk is never put on a smaller disk A BC ABC.
QuickSort by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data.
1 COMP9024: Data Structures and Algorithms Week Eight: Sortings and Sets Hui Wu Session 1, 2016
Advanced Sorting 7 2  9 4   2   4   7
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Merge Sort 7 2  9 4   2   4   7 2  2 9  9 4  4.
Merge Sort 1/12/2018 9:39 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Quick-Sort 2/18/2018 3:56 AM Selection Selection.
Bucket-Sort and Radix-Sort
COMP9024: Data Structures and Algorithms
COMP9024: Data Structures and Algorithms
Quick-Sort 9/12/2018 3:26 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Quick-Sort 9/13/2018 1:15 AM Quick-Sort     2
Selection Selection 1 Quick-Sort Quick-Sort 10/30/16 13:52
Radish-Sort 11/11/ :01 AM Quick-Sort     2 9  9
Objectives Introduce different known sorting algorithms
Bucket-Sort and Radix-Sort
Bucket-Sort and Radix-Sort
Quick-Sort 11/14/2018 2:17 PM Chapter 4: Sorting    7 9
Bucket-Sort and Radix-Sort
Quick-Sort 11/19/ :46 AM Chapter 4: Sorting    7 9
Bucket-Sort and Radix-Sort
Merge Sort 12/4/ :32 AM Merge Sort 7 2   7  2   4  4 9
Sorting Recap & More about Sorting
Sorting Recap & More about Sorting
Sorting, Sets and Selection
Quick-Sort 2/23/2019 1:48 AM Chapter 4: Sorting    7 9
Quick-Sort 2/25/2019 2:22 AM Quick-Sort     2
Copyright © Aiman Hanna All rights reserved
Quick-Sort 4/8/ :20 AM Quick-Sort     2 9  9
Bucket-Sort and Radix-Sort
Quick-Sort 4/25/2019 8:10 AM Quick-Sort     2
Quick-Sort 5/7/2019 6:43 PM Selection Selection.
Quick-Sort 5/25/2019 6:16 PM Selection Selection.
CS203 Lecture 15.
Bucket-Sort and Radix-Sort
Divide-and-Conquer 7 2  9 4   2   4   7
Chapter 11 Sets, and Selection
Presentation transcript:

1 CSC401 – Analysis of Algorithms Lecture Notes 9 Radix Sort and Selection Objectives  Introduce no-comparison-based sorting algorithms: Bucket-sort and Radix-sort  Analyze and design selection algorithms

2 Bucket-Sort  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))

3 Example  Key range [0, 9] 7, d1, c3, a7, g3, b7, e 1, c3, a3, b7, d7, g7, e Phase 1 Phase B 1, c7, d7, g3, b3, a7, e 

4 Properties and Extensions  Key-type Property –The keys are used as indices into an array and cannot be arbitrary objects –No external comparator  Stable Sort Property –The relative order of any two items with the same key is preserved after the execution of the algorithm Extensions –Integer keys in the range [a, b]  Put item (k, o) into bucket B[k  a] –String keys from a set D of possible strings, where D has constant size (e.g., names of the 50 U.S. states)  Sort D and compute the rank r(k) of each string k of D in the sorted sequence  Put item (k, o) into bucket B[r(k)]

5 Lexicographic Order  A d- tuple is a sequence of d keys (k 1, k 2, …, k d ), where key k i is said to be the i- th dimension of the tuple  Example: –The Cartesian coordinates of a point in space are a 3- tuple  The lexicographic order of two d- tuples is recursively defined as follows (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 ) I.e., the tuples are compared by the first dimension, then by the second dimension, etc.

6 Lexicographic-Sort  Let C i be the comparator that compares two tuples by their i- th dimension  Let stableSort(S, C) be a stable sorting algorithm that uses comparator C  Lexicographic-sort sorts a sequence of d- tuples in lexicographic order by executing d times algorithm stableSort, one per dimension  Lexicographic-sort runs in O(dT(n)) time, where T(n) is the running time of stableSort Algorithm lexicographicSort(S) Input sequence S of d-tuples Output sequence S sorted in lexicographic order for i  d downto 1 stableSort(S, C i ) Example: (7,4,6) (5,1,5) (2,4,6) (2, 1, 4) (3, 2, 4) (2, 1, 4) (3, 2, 4) (5,1,5) (7,4,6) (2,4,6) (2, 1, 4) (5,1,5) (3, 2, 4) (7,4,6) (2,4,6) (2, 1, 4) (2,4,6) (3, 2, 4) (5,1,5) (7,4,6)

7 Radix-Sort  Radix-sort is a specialization of lexicographic-sort that uses bucket-sort as the stable sorting algorithm in each dimension  Radix-sort is applicable to tuples where the keys in each dimension i are integers in the range [0, N  1]  Radix-sort runs in time O(d( n  N)) Algorithm radixSort(S, N) Input sequence S of d-tuples such that (0, …, 0)  (x 1, …, x d ) and (x 1, …, x d )  (N  1, …, N  1) for each tuple (x 1, …, x d ) in S Output sequence S sorted in lexicographic order for i  d downto 1 bucketSort(S, N)

8 Radix-Sort for Binary Numbers  Consider a sequence of n b -bit integers x  x b  … x 1 x 0  We represent each element as a b -tuple of integers in the range [0, 1] and apply radix-sort with N  2  This application of the radix-sort algorithm runs in O(bn) time  For example, we can sort a sequence of 32-bit integers in linear time Algorithm binaryRadixSort(S) Input sequence S of b-bit integers Output sequence S sorted replace each element x of S with the item (0, x) for i  0 to b  1 replace the key k of each item (k, x) of S with bit x i of x bucketSort(S, 2)

9 Example  Sorting a sequence of 4-bit integers

10 The Selection Problem  Given an integer k and n elements x 1, x 2, …, x n, taken from a total order, find the k- th smallest element in this set.  Of course, we can sort the set in O(n log n) time and then index the k-th element.  Can we solve the selection problem faster?  k=3

11 Quick-Select  Quick-select is a randomized selection algorithm based on the prune-and-search paradigm: –Prune: pick a random element x (called pivot) and partition S into  L elements less than x  E elements equal x  G elements greater than x –Search: depending on k, either answer is in E, or we need to recurse in either L or G x x L G E k < |L| |L| < k < |L|+|E| (done) k > |L|+|E| k’ = k - |L| - |E|

12 Partition  We partition an input sequence as in the quick- sort algorithm: –We remove, in turn, each element y from S and –We insert y into L, E or G, depending on the result of the comparison with the pivot x  Each insertion and removal is at the beginning or at the end of a sequence, and hence takes O(1) time  Thus, the partition step of quick-select takes O(n) time Algorithm partition(S, p) Input sequence S, position p of pivot Output subsequences L, E, G of the elements of S less than, equal to, or greater than the pivot, resp. L, E, G  empty sequences x  S.remove(p) while  S.isEmpty() y  S.remove(S.first()) if y < x L.insertLast(y) else if y = x E.insertLast(y) else { y > x } G.insertLast(y) return L, E, G

13 Quick-Select Visualization  An execution of quick-select can be visualized by a recursion path –Each node represents a recursive call of quick-select, and stores k and the remaining sequence k=5, S=( ) 5 k=2, S=( ) k=2, S=( ) k=1, S=(7 6 5)

14 Expected Running Time  Consider a recursive call of quick-select on a sequence of size s –Good call: the sizes of L and G are each less than 3s  4 –Bad call: one of L and G has size greater than 3s  4  A call is good with probability 1  2 –1/2 of the possible pivots cause good calls:  Good callBad call Good pivotsBad pivots

15 Expected Running Time, Part 2  Probabilistic Fact #1: The expected number of coin tosses required in order to get one head is two  Probabilistic Fact #2: Expectation is a linear function: –E(X + Y ) = E(X ) + E(Y ) –E(cX ) = cE(X )  Let T(n) denote the expected running time of quick-select.  By Fact #2, –T(n) < T(3n/4) + bn*(expected # of calls before a good call)  By Fact #1, –T(n) < T(3n/4) + 2bn  That is, T(n) is a geometric series: –T(n) < 2bn + 2b(3/4)n + 2b(3/4) 2 n + 2b(3/4) 3 n + …  So T(n) is O(n).  We can solve the selection problem in O(n) expected time.

16 Deterministic Selection  We can do selection in O(n) worst-case time.  Main idea: recursively use the selection algorithm itself to find a good pivot for quick- select: –Divide S into n/5 sets of 5 each –Find a median in each set –Recursively find the median of the “baby” medians.  See Exercise C-4.24 for details of analysis Min size for L Min size for G