Design and Analysis of Algorithms

Similar documents
Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

COT 6405 Introduction to Theory of Algorithms

Lecture: Analysis of Algorithms (CS )

How fast can we sort? Sorting. Decision-tree example. Decision-tree example. CS 3343 Fall by Charles E. Leiserson; small changes by Carola

Introduction to Algorithms

Introduction to Algorithms

Chapter 8 Sorting in Linear Time

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

Algorithms Chapter 8 Sorting in Linear Time

Chapter 8 Sort in Linear Time

Introduction to Algorithms 6.046J/18.401J

Lower bound for comparison-based sorting

Can we do faster? What is the theoretical best we can do?

Sorting. Popular algorithms: Many algorithms for sorting in parallel also exist.

CS 303 Design and Analysis of Algorithms

Lower Bound on Comparison-based Sorting

Sorting. CMPS 2200 Fall Carola Wenk Slides courtesy of Charles Leiserson with small changes by Carola Wenk

Can we do faster? What is the theoretical best we can do?

The complexity of Sorting and sorting in linear-time. Median and Order Statistics. Chapter 8 and Chapter 9

CPSC 311 Lecture Notes. Sorting and Order Statistics (Chapters 6-9)

Sorting Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

8 SortinginLinearTime

Other techniques for sorting exist, such as Linear Sorting which is not based on comparisons. Linear Sorting techniques include:

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

CSE 3101: Introduction to the Design and Analysis of Algorithms. Office hours: Wed 4-6 pm (CSEB 3043), or by appointment.

II (Sorting and) Order Statistics

EECS 2011M: Fundamentals of Data Structures

COMP Data Structures

Algorithms and Data Structures for Mathematicians

CS 5321: Advanced Algorithms Sorting. Acknowledgement. Ali Ebnenasir Department of Computer Science Michigan Technological University

Comparison Based Sorting Algorithms. Algorithms and Data Structures: Lower Bounds for Sorting. Comparison Based Sorting Algorithms

Algorithms and Data Structures: Lower Bounds for Sorting. ADS: lect 7 slide 1

Comparisons. Θ(n 2 ) Θ(n) Sorting Revisited. So far we talked about two algorithms to sort an array of numbers. What is the advantage of merge sort?

Data Structures and Algorithms Week 4

data structures and algorithms lecture 6

Comparisons. Heaps. Heaps. Heaps. Sorting Revisited. Heaps. So far we talked about two algorithms to sort an array of numbers

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems.

MA 252: Data Structures and Algorithms Lecture 9. Partha Sarathi Mandal. Dept. of Mathematics, IIT Guwahati

Sorting Algorithms. For special input, O(n) sorting is possible. Between O(n 2 ) and O(nlogn) E.g., input integer between O(n) and O(n)

Module 3: Sorting and Randomized Algorithms

Lecture 5: Sorting Part A

Next. 1. Covered basics of a simple design technique (Divideand-conquer) 2. Next, more sorting algorithms.

Question 7.11 Show how heapsort processes the input:

Data Structures and Algorithms Chapter 4

CS61B Lectures # Purposes of Sorting. Some Definitions. Classifications. Sorting supports searching Binary search standard example

Introduction to Algorithms and Data Structures. Lecture 12: Sorting (3) Quick sort, complexity of sort algorithms, and counting sort

Module 3: Sorting and Randomized Algorithms

Module 3: Sorting and Randomized Algorithms. Selection vs. Sorting. Crucial Subroutines. CS Data Structures and Data Management

Design and Analysis of Algorithms PART III

Fast Sorting and Selection. A Lower Bound for Worst Case

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Practical Session #11 Sorting in Linear Time

ECE608 - Chapter 8 answers

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

1. Covered basics of a simple design technique (Divideand-conquer) 2. Next, more sorting algorithms.

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

CS 561, Lecture 1. Jared Saia University of New Mexico

Algorithms Lab 3. (a) What is the minimum number of elements in the heap, as a function of h?

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING

CSci 231 Final Review

4. Sorting and Order-Statistics

The Limits of Sorting Divide-and-Conquer Comparison Sorts II

Sorting. Riley Porter. CSE373: Data Structures & Algorithms 1

Data Structures and Algorithms. Roberto Sebastiani

7 Sorting Algorithms. 7.1 O n 2 sorting algorithms. 7.2 Shell sort. Reading: MAW 7.1 and 7.2. Insertion sort: Worst-case time: O n 2.

Searching, Sorting. part 1

7. Sorting I. 7.1 Simple Sorting. Problem. Algorithm: IsSorted(A) 1 i j n. Simple Sorting

Sorting is ordering a list of objects. Here are some sorting algorithms

CSE 326: Data Structures Sorting Conclusion

University of Waterloo CS240, Winter 2010 Assignment 2

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Merge Sort & Quick Sort

Treaps. 1 Binary Search Trees (BSTs) CSE341T/CSE549T 11/05/2014. Lecture 19

2/26/2016. Divide and Conquer. Chapter 6. The Divide and Conquer Paradigm

CS : Data Structures

Exercise 1 : B-Trees [ =17pts]

Shell s sort. CS61B Lecture #25. Sorting by Selection: Heapsort. Example of Shell s Sort

Algorithms and Data Structures

The divide and conquer strategy has three basic parts. For a given problem of size n,

CSE 373 Lecture 19: Wrap-Up of Sorting

CS61B Lectures #28. Today: Lower bounds on sorting by comparison Distribution counting, radix sorts

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

DATA STRUCTURES AND ALGORITHMS

Lecture 2: Getting Started

Lecture 9: Sorting Algorithms

input sort left half sort right half merge results Mergesort overview

Lecture 5: Sorting in Linear Time CSCI Algorithms I. Andrew Rosenberg

SORTING AND SELECTION

Lecture 5. Counting Sort / Radix Sort

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

Selection (deterministic & randomized): finding the median in linear time

Lecture 3: Sorting 1

A data structure and associated algorithms, NOT GARBAGE COLLECTION

CS2351 Data Structures. Lecture 5: Sorting in Linear Time

08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58

Week 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT.

8. Sorting II. 8.1 Heapsort. Heapsort. [Max-]Heap 6. Heapsort, Quicksort, Mergesort. Binary tree with the following properties Wurzel

Run Times. Efficiency Issues. Run Times cont d. More on O( ) notation

Sorting Algorithms. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Sorting. Bubble Sort. Pseudo Code for Bubble Sorting: Sorting is ordering a list of elements.

Introduction. e.g., the item could be an entire block of information about a student, while the search key might be only the student's name

Transcription:

Design and Analysis of Algorithms CSE 5311 Lecture 8 Sorting in Linear Time Junzhou Huang, Ph.D. Department of Computer Science and Engineering CSE5311 Design and Analysis of Algorithms 1

Sorting So Far Insertion sort: Easy to code Fast on small inputs (less than ~50 elements) Fast on nearly-sorted inputs O(n 2 ) worst case O(n 2 ) average (equally-likely inputs) case O(n 2 ) reverse-sorted case Merge sort: Divide-and-conquer: Split array in half Recursively sort subarrays Linear-time merge step O(n lg n) worst case CSE5311 Design and Analysis of Algorithms 2

Sorting So Far Heap sort: Uses the very useful heap data structure Complete binary tree Heap property: parent key > children s keys O(n lg n) worst case Sorts in place Fair amount of shuffling memory around Quick sort: Divide-and-conquer: Partition array into two subarrays, recursively sort All of first subarray < all of second subarray No merge step needed! O(n lg n) average case Fast in practice O(n 2 ) worst case Naïve implementation: worst case on sorted input Address this with randomized quicksort CSE5311 Design and Analysis of Algorithms 3

How Fast Can We Sort? Lower bound Prove a Lower Bound for any comparison based algorithm for the Sorting Problem How? Decision trees help us. Observation: sorting algorithms so far are comparison sorts The only operation used to gain ordering information about a sequence is the pairwise comparison of two elements Theorem: all comparison sorts are Ω(n lg n) A comparison sort must do O(n) comparisons (why?) What about the gap between O(n) and O(n lg n) CSE5311 Design and Analysis of Algorithms 4

Decision-tree Example Sort a 1, a 2,, a n 1:2 2:3 123 1:3 132 312 1:3 213 2:3 231 321 Each internal node is labeled i:j for i, j {1, 2,, n}. The left subtree shows subsequent comparisons if a i a j. The right subtree shows subsequent comparisons if a i a j. CSE5311 Design and Analysis of Algorithms 5

Decision-tree Example Sort a 1, a 2, a 3 = 9, 4, 6 : 2:3 1:2 9 4 1:3 123 1:3 132 312 213 2:3 231 321 Each internal node is labeled i:j for i, j {1, 2,, n}. The left subtree shows subsequent comparisons if a i a j. The right subtree shows subsequent comparisons if a i a j. CSE5311 Design and Analysis of Algorithms 6

Decision-tree Example Sort a 1, a 2, a 3 = 9, 4, 6 : 2:3 1:2 1:3 9 6 123 1:3 132 312 213 2:3 231 321 Each internal node is labeled i:j for i, j {1, 2,, n}. The left subtree shows subsequent comparisons if a i a j. The right subtree shows subsequent comparisons if a i a j. CSE5311 Design and Analysis of Algorithms 7

Decision-tree Example Sort a 1, a 2, a 3 = 9, 4, 6 : 2:3 1:2 1:3 123 1:3 132 312 213 4 6 2:3 231 321 Each internal node is labeled i:j for i, j {1, 2,, n}. The left subtree shows subsequent comparisons if a i a j. The right subtree shows subsequent comparisons if a i a j. CSE5311 Design and Analysis of Algorithms 8

Decision-tree Example Sort a 1, a 2, a 3 = 9, 4, 6 : 2:3 1:2 1:3 123 1:3 213 2:3 132 312 231 321 4 6 9 Each leaf contains a permutation π(1), π(2),, π(n) to indicate that the ordering a π(1) a π(2) L a π(n) has been established. CSE5311 Design and Analysis of Algorithms 9

Decision-tree Example A decision tree can model the execution of any comparison sort: One tree for each input size n. View the algorithm as splitting whenever it compares two elements. The tree contains the comparisons along all possible instruction traces. The running time of the algorithm = the length of the path taken. Worst-case running time = height of tree. CSE5311 Design and Analysis of Algorithms 10

How? Any comparison sort can be turned into a Decision tree class InsertionSortAlgorithm { for (int i = 1; i < a.length; i++) { int j = i; while ((j > 0) && (a[j-1] > a[i])) { a[j] = a[j-1]; j--; } a[j] = B; }} 1:2 2:3 1:3 123 1:3 213 2:3 132 312 231 321 CSE5311 Design and Analysis of Algorithms 11

Lower Bound for Decision-tree Sorting Theorem. Any decision tree that can sort n elements must have height Ω(n lg n). Proof. The tree must contain n! leaves, since there are n! possible permutations. A height-h binary tree has 2 h leaves. Thus, n! 2 h. h lg(n!) lg ((n/e) n ) = n lg n n lg e = Ω(n lg n). (lg is mono. increasing) (Stirling s formula) n log n n < log(n!) < n log n CSE5311 Design and Analysis of Algorithms 12

Decision Tree Decision trees provide an abstraction of comparison sorts A decision tree represents the comparisons made by a comparison sort. Every thing else ignored What do the leaves represent? How many leaves must there be? Decision trees can model comparison sorts. For a given algorithm: One tree for each n Tree paths are all possible execution traces What s the longest path in a decision tree for insertion sort? For merge sort? What is the asymptotic height of any decision tree for sorting n elements? Answer: Ω(n lg n) (now let s prove it ) CSE5311 Design and Analysis of Algorithms 13

Lower Bound For Comparison Sorting Theorem: Any decision tree that sorts n elements has height Ω(n lg n) What s the minimum # of leaves? What s the maximum # of leaves of a binary tree of height h? Clearly the minimum # of leaves is less than or equal to the maximum # of leaves So we have n! 2 h ; Taking logarithms: lg (n!) h Stirling s approximation tells us: n!> n e n Thus h lg n e n = n lg n n lg e = Ω ( n lg n ) The minimum height of a decision tree is Ω(n lg n) CSE5311 Design and Analysis of Algorithms 14

Lower Bound For Comparison Sorting Thus the time to comparison sort n elements is Ω(n lg n) Corollary: Heapsort and Mergesort are asymptotically optimal comparison sorts But the name of this lecture is Sorting in linear time! How can we do better than Ω(n lg n)? CSE5311 Design and Analysis of Algorithms 15

Sorting In Linear Time Counting sort No comparisons between elements! But depends on assumption about the numbers being sorted We assume numbers are in the range 1 k The algorithm: Input: A[1..n], where A[j] {1, 2, 3,, k} Output: B[1..n], sorted (notice: not sorting in place) Also: Array C[1..k] for auxiliary storage CSE5311 Design and Analysis of Algorithms 16

Counting Sort 1 CountingSort(A, B, k) 2 for i=1 to k 3 C[i]= 0; 4 for j=1 to n 5 C[A[j]] += 1; 6 for i=2 to k 7 C[i] = C[i] + C[i-1]; 8 for j=n downto 1 9 B[C[A[j]]] = A[j]; 10 C[A[j]] -= 1; Work through example: A={4 1 3 4 3}, k = 4 CSE5311 Design and Analysis of Algorithms 17

Counting Sort 1 CountingSort(A, B, k) 2 for i=1 to k 3 C[i]= 0; 4 for j=1 to n 5 C[A[j]] += 1; 6 for i=2 to k 7 C[i] = C[i] + C[i-1]; 8 for j=n downto 1 9 B[C[A[j]]] = A[j]; 10 C[A[j]] -= 1; What will be the running time? Takes time O(k) Takes time O(n) CSE5311 Design and Analysis of Algorithms 18

Counting-sort Example 1 2 3 4 5 A: 4 1 3 4 3 C: 1 2 3 4 B: CSE5311 Design and Analysis of Algorithms 19

Loop 1 1 2 3 4 5 A: 4 1 3 4 3 1 2 3 4 C: 0 0 0 0 B: for i 1 to k do C[i] 0 CSE5311 Design and Analysis of Algorithms 20

Loop 2 1 2 3 4 5 A: 4 1 3 4 3 1 2 3 4 C: 0 0 0 1 B: for j 1 to n do C[A[ j]] C[A[ j]] + 1 C[i] = {key = i} CSE5311 Design and Analysis of Algorithms 21

Loop 2 1 2 3 4 5 A: 4 1 3 4 3 1 2 3 4 C: 1 0 0 1 B: for j 1 to n do C[A[ j]] C[A[ j]] + 1 C[i] = {key = i} CSE5311 Design and Analysis of Algorithms 22

Loop 2 1 2 3 4 5 A: 4 1 3 4 3 1 2 3 4 C: 1 0 1 1 B: for j 1 to n do C[A[ j]] C[A[ j]] + 1 C[i] = {key = i} CSE5311 Design and Analysis of Algorithms 23

Loop 2 1 2 3 4 5 A: 4 1 3 4 3 1 2 3 4 C: 1 0 1 2 B: for j 1 to n do C[A[ j]] C[A[ j]] + 1 C[i] = {key = i} CSE5311 Design and Analysis of Algorithms 24

Loop 2 1 2 3 4 5 A: 4 1 3 4 3 1 2 3 4 C: 1 0 2 2 B: for j 1 to n do C[A[ j]] C[A[ j]] + 1 C[i] = {key = i} CSE5311 Design and Analysis of Algorithms 25

Loop 3 1 2 3 4 5 A: 4 1 3 4 3 1 2 3 4 C: 1 0 2 2 B: C': 1 1 2 2 for i 2 to k do C[i] C[i] + C[i 1] C[i] = {key i} CSE5311 Design and Analysis of Algorithms 26

Loop 3 1 2 3 4 5 A: 4 1 3 4 3 1 2 3 4 C: 1 0 2 2 B: C': 1 1 3 2 for i 2 to k do C[i] C[i] + C[i 1] C[i] = {key i} CSE5311 Design and Analysis of Algorithms 27

Loop 3 1 2 3 4 5 A: 4 1 3 4 3 1 2 3 4 C: 1 0 2 2 B: C': 1 1 3 5 for i 2 to k do C[i] C[i] + C[i 1] C[i] = {key i} CSE5311 Design and Analysis of Algorithms 28

Loop 4 1 2 3 4 5 A: 4 1 3 4 3 1 2 3 4 C: 1 1 3 5 B: 3 C': 1 1 2 5 for j n downto 1 dob[c[a[ j]]] A[ j] C[A[ j]] C[A[ j]] 1 CSE5311 Design and Analysis of Algorithms 29

Loop 4 1 2 3 4 5 A: 4 1 3 4 3 1 2 3 4 C: 1 1 2 5 B: 3 4 C': 1 1 2 4 for j n downto 1 dob[c[a[ j]]] A[ j] C[A[ j]] C[A[ j]] 1 CSE5311 Design and Analysis of Algorithms 30

Loop 4 1 2 3 4 5 A: 4 1 3 4 3 1 2 3 4 C: 1 1 2 4 B: 3 3 4 C': 1 1 1 4 for j n downto 1 dob[c[a[ j]]] A[ j] C[A[ j]] C[A[ j]] 1 CSE5311 Design and Analysis of Algorithms 31

Loop 4 1 2 3 4 5 A: 4 1 3 4 3 1 2 3 4 C: 1 1 1 4 B: 1 3 3 4 C': 0 1 1 4 for j n downto 1 dob[c[a[ j]]] A[ j] C[A[ j]] C[A[ j]] 1 CSE5311 Design and Analysis of Algorithms 32

Loop 4 1 2 3 4 5 A: 4 1 3 4 3 1 2 3 4 C: 0 1 1 4 B: 1 3 3 4 4 C': 0 1 1 3 for j n downto 1 dob[c[a[ j]]] A[ j] C[A[ j]] C[A[ j]] 1 CSE5311 Design and Analysis of Algorithms 33

Analysis Θ(k) Θ(n) Θ(k) Θ(n) Θ(n + k) for i 1 to k do C[i] 0 for j 1 to n do C[A[ j]] C[A[ j]] + 1 for i 2 to k do C[i] C[i] + C[i 1] for j n downto 1 do B[C[A[ j]]] A[ j] C[A[ j]] C[A[ j]] 1 CSE5311 Design and Analysis of Algorithms 34

Counting Sort Total time: O(n + k) Usually, k = O(n) Thus counting sort runs in O(n) time But sorting is Ω(n lg n)! No contradiction--this is not a comparison sort (in fact, there are no comparisons at all!) Notice that this algorithm is stable Cool! Why don t we always use counting sort? Because it depends on range k of elements Could we use counting sort to sort 32 bit integers? Why or why not? Answer: no, k too large (2 32 = 4,294,967,296) CSE5311 Design and Analysis of Algorithms 35

Stable Sorting Counting sort is a stable sort: it preserves the input order among equal elements. A: 4 1 3 4 3 B: 1 3 3 4 4 Exercise: What other sorts have this property? CSE5311 Design and Analysis of Algorithms 36

Radix Sort Intuitively, you might sort on the most significant digit, then the second msd, etc. Problem: lots of intermediate piles of cards (read: scratch arrays) to keep track of Key idea: sort the least significant digit first RadixSort(A, d) for i=1 to d StableSort(A) on digit i Example: Fig 9.3 CSE5311 Design and Analysis of Algorithms 37

Radix Sort Can we prove it will work? Sketch of an inductive argument (induction on the number of passes): Assume lower-order digits {j: j<i}are sorted Show that sorting next digit i leaves array correctly sorted If two digits at position i are different, ordering numbers by that digit is correct (lower-order digits irrelevant) If they are the same, numbers are already sorted on the lower-order digits. Since we use a stable sort, the numbers stay in the right order CSE5311 Design and Analysis of Algorithms 38

Radix Sort What sort will we use to sort on digits? Counting sort is obvious choice: Sort n numbers on digits that range from 1..k Time: O(n + k) Each pass over n numbers with d digits takes time O(n+k), so total time O(dn+dk) When d is constant and k=o(n), takes O(n) time How many bits in a computer word? CSE5311 Design and Analysis of Algorithms 39

Radix Sort Problem: sort 1 million 64-bit numbers Treat as four-digit radix 2 16 numbers Can sort in just four passes with radix sort! Compares well with typical O(n lg n) comparison sort Requires approximate log n = 20 operations per number being sorted So why would we ever use anything but radix sort? In general, radix sort based on counting sort is Fast, Asymptotically fast (i.e., O(n)) Simple to code A good choice To think about: Can radix sort be used on floating-point numbers? CSE5311 Design and Analysis of Algorithms 40

Operation of Radix Sort 3 2 9 4 5 7 6 5 7 8 3 9 4 3 6 7 2 0 3 5 5 7 2 0 3 5 5 4 3 6 4 5 7 6 5 7 3 2 9 8 3 9 7 2 0 3 2 9 4 3 6 8 3 9 3 5 5 4 5 7 6 5 7 3 2 9 3 5 5 4 3 6 4 5 7 6 5 7 7 2 0 8 3 9 CSE5311 Design and Analysis of Algorithms 41

Correctness of Radix Sort Induction on digit position Assume that the numbers are sorted by their low-order t 1 digits. Sort on digit t 7 2 0 3 2 9 4 3 6 8 3 9 3 5 5 4 5 7 6 5 7 3 2 9 3 5 5 4 3 6 4 5 7 6 5 7 7 2 0 8 3 9 CSE5311 Design and Analysis of Algorithms 42

Correctness of Radix Sort Induction on digit position Assume that the numbers are sorted by their low-order t 1 digits. Sort on digit t Two numbers that differ in digit t are correctly sorted. 7 2 0 3 2 9 4 3 6 8 3 9 3 5 5 4 5 7 6 5 7 3 2 9 3 5 5 4 3 6 4 5 7 6 5 7 7 2 0 8 3 9 CSE5311 Design and Analysis of Algorithms 43

Correctness of Radix Sort Induction on digit position Assume that the numbers are sorted by their low-order t 1 digits. Sort on digit t Two numbers that differ in digit t are correctly sorted. Two numbers equal in digit t are put in the same order as the input correct order. 7 2 0 3 2 9 4 3 6 8 3 9 3 5 5 4 5 7 6 5 7 3 2 9 3 5 5 4 3 6 4 5 7 6 5 7 7 2 0 8 3 9 CSE5311 Design and Analysis of Algorithms 44

Analysis of Radix Sort Assume counting sort is the auxiliary stable sort. Sort n computer words of b bits each. Each word can be viewed as having b/r base-2 r digits. 8 8 8 8 Example: 32-bit word r = 8 b/r = 4 passes of counting sort on base-2 8 digits; or r = 16 b/r = 2 passes of counting sort on base-2 16 digits. How many passes should we make? CSE5311 Design and Analysis of Algorithms 45

Analysis of Radix Sort Recall: Counting sort takes Θ(n + k) time to sort n numbers in the range from 0 to k 1. If each b-bit word is broken into r-bit pieces, each pass of counting sort takes Θ(n + 2 r ) time. Since there are b/r passes, we have Θ( (b/r)n + 2 r ) Choose r to minimize T(n, b): Increasing r means fewer passes, but as r >> lg n, the time grows exponentially. CSE5311 Design and Analysis of Algorithms 46

Choosing r Minimize T(n, b) by differentiating and setting to 0. Or, just observe that we don t want 2 r > n, and there s no harm asymptotically in choosing r as large as possible subject to this constraint. Choosing r = lg n implies T(n, b) = Θ(b n/lg n). For numbers in the range from 0 to n d 1, we have b = d lg n radix sort runs in Θ(d n) time. CSE5311 Design and Analysis of Algorithms 47

Bucket Sort Assumption: uniform distribution Input numbers are uniformly distributed in [0,1). Suppose input size is n. Idea: Divide [0,1) into n equal-sized subintervals (buckets). Distribute n numbers into buckets Expect that each bucket contains few numbers. Sort numbers in each bucket (insertion sort as default). Then go through buckets in order, listing elements, CSE5311 Design and Analysis of Algorithms 48

BUCKET-SORT(A) 1. n length[a] 2. for i 1 to n 3. do insert A[i] into bucket B[ na[i] ] 4. for i 0 to n-1 5. do sort bucket B[i] using insertion sort 6. Concatenate bucket B[0],B[1],,B[n-1] CSE5311 Design and Analysis of Algorithms 49

Example of BUCKET-SORT CSE5311 Design and Analysis of Algorithms 50

Analysis of BUCKET-SORT(A) 1. n length[a] Ω(1) 2. for i 1 to n O(n) 3. do insert A[i] into bucket B[ na[i] ] Ω(1) (i.e. total O(n)) 4. for i 0 to n-1 O(n) 5. do sort bucket B[i] with insertion sort O(n i2 ) ( n-1 i=0 O(n i2 )) 6. Concatenate bucket B[0],B[1],,B[n-1] O(n) Where n i is the size of bucket B[i]. Thus T(n) = Θ(n) + i=0 n-1 O(n i2 ) = Θ(n) + n O(2-1/n) = Θ(n) CSE5311 Design and Analysis of Algorithms 51

Analysis of BUCKET-SORT(A) CSE5311 Design and Analysis of Algorithms 52