Algorithms Chapter 8 Sorting in Linear Time

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

COT 6405 Introduction to Theory of Algorithms

Design and Analysis of Algorithms

Chapter 8 Sorting in Linear Time

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

Lecture: Analysis of Algorithms (CS )

Chapter 8 Sort in Linear Time

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

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

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

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

Lower bound for comparison-based sorting

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

Introduction to Algorithms

II (Sorting and) Order Statistics

8 SortinginLinearTime

Introduction to Algorithms

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

Count Sort, Bucket Sort, Radix Sort. CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

Introduction to Algorithms 6.046J/18.401J

ECE608 - Chapter 8 answers

Module 3: Sorting and Randomized Algorithms

Fast Sorting and Selection. A Lower Bound for Worst Case

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

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

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

Sorting Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

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

Linear Sorts. EECS 2011 Prof. J. Elder - 1 -

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

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

EECS 2011M: Fundamentals of Data Structures

data structures and algorithms lecture 6

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

Module 3: Sorting and Randomized Algorithms

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

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

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

Lower Bound on Comparison-based Sorting

Algorithms and Data Structures for Mathematicians

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

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

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

CS 303 Design and Analysis of Algorithms

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

CPE702 Sorting Algorithms

Practical Session #11 Sorting in Linear Time

We assume uniform hashing (UH):

Design and Analysis of Algorithms PART III

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

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

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

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

Lower and Upper Bound Theory. Prof:Dr. Adnan YAZICI Dept. of Computer Engineering Middle East Technical Univ. Ankara - TURKEY

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?

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

SORTING LOWER BOUND & BUCKET-SORT AND RADIX-SORT

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

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

Data Structures. Sorting. Haim Kaplan & Uri Zwick December 2013

Data Structures and Algorithms

Data Structures and Algorithms

MergeSort. Algorithm : Design & Analysis [5]

The Heap Data Structure

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

Chapter 4: Sorting. Spring 2014 Sorting Fun 1

Bucket-Sort and Radix-Sort

CSE 326: Data Structures Sorting Conclusion

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

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.

COMP Data Structures

CS S-11 Sorting in Θ(nlgn) 1. Base Case: A list of length 1 or length 0 is already sorted. Recursive Case:

TECH. Problem: Sorting. Application of Sorting. Insertion Sort. Insertion Sort: Algorithm. Insertion Sort: Specification for subroutine

Heapsort. Algorithms.

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)

CS2351 Data Structures. Lecture 5: Sorting in Linear Time

9/24/ Hash functions

Lecture 6: Analysis of Algorithms (CS )

Data Structures and Algorithms Chapter 4

Bucket-Sort and Radix-Sort

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

Partha Sarathi Manal

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

4. Sorting and Order-Statistics

Introduction to Algorithms

IS 709/809: Computational Methods in IS Research. Algorithm Analysis (Sorting)

Data Structures and Algorithms Week 4

Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort

Sample Solutions to Homework #2

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

CS S-08 Priority Queues Heaps 1

Algorithms and Data Structures (INF1) Lecture 7/15 Hua Lu

A data structure and associated algorithms, NOT GARBAGE COLLECTION

Outline. Definition. 2 Height-Balance. 3 Searches. 4 Rotations. 5 Insertion. 6 Deletions. 7 Reference. 1 Every node is either red or black.

Priority Queues, Heaps, and Heapsort

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

HOMEWORK 4 CS5381 Analysis of Algorithm

Linear Time Sorting. Venkatanatha Sarma Y. Lecture delivered by: Assistant Professor MSRSAS-Bangalore

COMP 352 FALL Tutorial 10

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

Transcription:

Algorithms Chapter 8 Sorting in Linear Time Assistant Professor: Ching Chi Lin 林清池助理教授 chingchi.lin@gmail.com Department of Computer Science and Engineering National Taiwan Ocean University

Outline Lower bounds for sorting Counting sort Radix sort Bucket sort

Overview Sort n numbers in O(nlgn) time Merge sort and heapsort achieve this upper bound in the worst case. Quicksort achieves it on average. For each of these algorithms, we can produce a sequence of n input numbers that causes the algorithm to run in Θ(nlgn) time. Comparison sorting The only operation that may be used to gain order information about a sequence is comparison of pairs of elements. All sorts seen so far are comparison sorts: insertion sort, selection sort, merge sort, quicksort, heapsort.

Lower bounds for sorting Lower bounds Ω(n)to examine all the input. All sorts seen so far are Ω(nlgn). We ll show that Ω(nlgn)is a lower bound for comparison sorts. Decision tree Abstraction of any comparison sort. A full binary tree. Represents comparisons made by a specific sorting algorithm on inputs of a given size. Control, data movement, and all other aspects of the algorithm are ignored.

Decision tree For insertion sort on elements: compare A[] to A[] A[] A[] : A[] > A[] > : A[] A[] : A[] > A[] > A[] > A[] > A[] > A[],, :,, : > > A[] A[] A[],,,,,,,, How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once. 5

Properties of decision trees / Lemma Any binary tree of height h has h leaves. Proof: By induction on h. Basis: h =. Tree is just one node, which is a leaf. h =. Inductive step: Assume true for height = h. Extend tree of height h by making as many new leaves as possible. Each leaf becomes parent to two new leaves. # of leaves for height h = (# of leaves for height h ) = h (ind. hypothesis) = h. 6

Properties of decision trees / Theorem Any decision tree that sorts n elements has height Ω(nlgn). Proof: l n!, where l = # of leaves. By lemma, n! l h or h n!. Take logs: h lg(n!). Use Stirling s approximation: n! >(n/e) n h >lg(n/e) n = nlg(n/e) = nlgn nlg e = Ω(nlgn). 7

Properties of decision trees / Corollary Heapsort and merge sort are asymptotically optimal comparison sorts. Proof: The O(nlgn) upper bounds on the running times for heapsort and merge sort match the Ω(nlgn) worst case lower bound from Theorem. 8

Outline Lower bounds for sorting Counting sort Radix sort Bucket sort 9

Counting sort Non comparison sorts. Depends on a key assumption: numbers to be sorted are integers in {,,..., k}. Input: A[.. n], where A[ j ] {,,..., k} for j =,,..., n. Array A and values n and k are given as parameters. Output: B[.. n], sorted. B is assumed to be already allocated and is given as a parameter. Auxiliary storage: C[.. k]. Worst case running time: Θ(n+k).

The COUNTING SORT procedure COUNTING SORT(A, B, k).. for i to k for j to length[a].. do C[i] do C[A[j]] C[A[j]] + Θ(k) Θ(n) 5. /* C[i] now contains the number of elements equal to i. */ 6. for i to k 7. do C[i] C[i] + C[i ] Θ(k) 8. /* C[i] now contains the number of elements less than or equal to i. */ 9. for j length[a] downto. do B[C[A[j]]] A[j] Θ(n). C[A[j]] C[A[j]] The running time: Θ(n+k).

A 8 7 6 5 5 C 5 C 7 5 8 7 B 8 7 6 5 C 6 5 8 7 B 8 7 6 5 C 6 5 8 7 B 8 7 6 5 C 5 5 8 7 B 8 7 6 5 5

Properties of counting sort A sorting algorithm is said to be stable if keys with same value appear in same order in output as they did in input. Counting sort is stable because of how the last loop works. Counting sort will be used in radix sort.

Outline Lower bounds for sorting Counting sort Radix sort Bucket sort

Radix sort Key idea: Sort least significant digits first. RADIX SORT(A, d). for i to d. do use a stable sort to sort array A on digit i An example: sorted 6 5 68 85 75 5 7 69 69 75 5 7 85 5 6 68 7 68 6 85 5 75 5 69 6 5 5 68 69 7 75 85 5

Correctness of radix sort Proof: By induction on number of passes (i in pseudocode). Basis: i =. There is only one digit, so sorting on that digit sorts the array. Inductive step: Assume digits,,, i are sorted. Show that a stable sort on digit i leaves digits,,, i sorted: If digits in position i are different, ordering by position i is correct, and positions,, i are irrelevant. If digits in position i are equal, numbers are already in the right order (by inductive hypothesis). The stable sort on digit i leaves them in the right order. 6

Time complexity of radix sort Assume that we use counting sort as the intermediate sort. When each digit is in the range to k, each pass over n d digit number takes time Θ(n + k). There are d passes, so the total time for radix sort is Θ(d(n + k)). If k = O(n), time = Θ(dn). Lemma : Given n d digit numbers in which each digit can take on up to k possible values, RADIXSORT correctly sorts these numbers in Θ(d(n + k)) time. 7

Break each key into digits / Lemma : Given n b bit numbers and any positive integer r b, RADIX SORT correctly sorts these numbers in Θ((b/r)(n + r )) time. Proof We view each key as having d = b/r digits of r bits each. Each digit is an integer in the range to r, so that we can use counting sort with k = r. Each pass of counting sort takes time Θ(n+k) = Θ(n+ r ). A total running time of Θ(d(n + r )) = Θ((b/r)(n + r )). For example: bit words, 8 bit digits. b =, r = 8, d = /8 =, k = 8 = 55. 8

Break each key into digits / Recall that the running time is Θ((b/r)(n + r )). How to choose r? Balance b/r and n + r. If b < lgn, then choosing r = b yields a running time of (b/b)(n + r ) = Θ(n). If b lgn, then choosing r lgn gives us b bn θ ( ( n + n)) = θ ( ). lg n lg n If r >lgn, then r term in the numerator increases faster than the r term in the denominator. If r <lgn, then b/r term increases, and n + r term remains at Θ(n). 9

The main reason How does radix sort violate the ground rules for a comparison sort? Using counting sort allows us to gain information about keys by means other than directly comparing keys. Used keys as array indices.

Outline Lower bounds for sorting Counting sort Radix sort Bucket sort

Bucket sort Assumes the input is generated by a random process that distributes elements uniformly over [, ). Key idea: Divide [, ) into n equal sized buckets. Distribute the n input values into the buckets. Sort each bucket. Then go through buckets in order, listing elements in each one. 5 6 7 8 9 A.78.7.9.6.7.9....68 5 6 7 8 9 B / / / /...9.68.7.9 / / /.7..78 / /.6 /

The BUCKET SORT procedure Input: A[.. n], where A[i] < for all i. Auxiliary array: B[..n ] of linked lists, each list initially empty. BUCKET SORT(A, n). for i to n. do insert A[i] into list B[ n A[i] ]. for i to n. do sort list B[i ] with insertion sort 5. concatenate lists B[], B[],..., B[n ] together in order 6. return the concatenated lists

Correctness of bucket sort Consider A[i], A[j]. Assume without loss of generality that A[i] A[j]. Then n A[i] n A[j]. So A[i] is placed into the same bucket as A[j] or into a bucket with a lower index. If same bucket, insertion sort fixes up. If earlier bucket, concatenation of lists fixes up.

Time complexity of bucket sort Relies on no bucket getting too many values. All lines of algorithm except insertion sorting take Θ(n) altogether. Intuitively, if each bucket gets a constant number of elements, it takes O() time to sort each bucket O(n)sort time for all buckets. We expect each bucket to have few elements, since the average is element per bucket. 5

Time complexity of bucket sort Define a random variable: n i = the number of elements placed in bucket B[i]. Because insertion sort runs in quadratic time, bucket sort time is Take expectations of both sides: + T ( n) = θ ( n) O( n i ). i= n E[ T( n)] = E θ ( n) + = θ( n) + = θ( n) + n i= n i= n i= O( n E[ O( n O(E[ n i i i ) )] ]) Claim that E[ n i ] = /n for I n. Therefore, E[ T( n)] = θ( n) + O( / n) i= = θ( n) + O( n) linearity of expectation E[aX] = ae[x] = θ( n). n 6

Proof of claim Claim: E[ n i ] = /nfor I n. Proof Pr{A[j] falls in bucket i} = p = /n. The probability that n i = k follows the binomial distribution b(k; n, p). So, E[n i ] = np = and variance Var[n i ] = np( p) = /n. For any random variable X, we have E[ n i ] = = = Var[ n ] + E + n. n i [ n i ] 7

Notes Again, not a comparison sort. Used a function of key values to index into an array. This is a probabilistic analysis. We used probability to analyze an algorithm whose running time depends on the distribution of inputs. Different from a randomized algorithm, where we use randomization to impose a distribution. With bucket sort, if the input isn t drawn from a uniform distribution on [, ), all bets are off (performance wise, but the algorithm is still correct). 8