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

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

Introduction to Algorithms

Lower bound for comparison-based sorting

Lower Bound on Comparison-based Sorting

Design and Analysis of Algorithms

Introduction to Algorithms 6.046J/18.401J

Chapter 8 Sort in Linear Time

Introduction to Algorithms

Lecture: Analysis of Algorithms (CS )

Chapter 8 Sorting in Linear Time

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

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

How to Win Coding Competitions: Secrets of Champions. Week 3: Sorting and Search Algorithms Lecture 7: Lower bound. Stable sorting.

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

CMPS 2200 Fall 2017 Red-black trees Carola Wenk

CMPS 2200 Fall 2015 Red-black trees Carola Wenk

Question 7.11 Show how heapsort processes the input:

data structures and algorithms lecture 6

CS 3343 Fall 2007 Red-black trees Carola Wenk

1 Lower bound on runtime of comparison sorts

Dynamic tables. Amortized Analysis. Example of a dynamic table. Example of a dynamic table. CS Spring 2008

CMPS 2200 Fall Dynamic Programming. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk

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

Module 3: Sorting and Randomized Algorithms

Orthogonal range searching. Range Trees. Orthogonal range searching. 1D range searching. CS Spring 2009

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

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

II (Sorting and) Order Statistics

8 SortinginLinearTime

CS240 Fall Mike Lam, Professor. Quick Sort

Computational Geometry

Computer Science 302 Spring 2007 Practice Final Examination: Part I

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

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

CMPS 2200 Fall Amortized Analysis. Carola Wenk. Slides courtesy of Charles Leiserson with changes by Carola Wenk

Minimum Spanning Trees

Module 3: Sorting and Randomized Algorithms

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

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

CS Fall 2010 B-trees Carola Wenk

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

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

l Find a partner to bounce ideas off of l Assignment 2 l Proofs should be very concrete l Proving big-o, must satisfy definition

Properties of a heap (represented by an array A)

CS126 Final Exam Review

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

Solutions. (a) Claim: A d-ary tree of height h has at most 1 + d +...

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

Data Structures and Algorithms. Roberto Sebastiani

COSC 311: ALGORITHMS HW1: SORTING

COMP Analysis of Algorithms & Data Structures

COT 6405 Introduction to Theory of Algorithms

CPE702 Sorting Algorithms

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

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

Sorting Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

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

EECS 2011M: Fundamentals of Data Structures

Randomized Algorithms, Quicksort and Randomized Selection

CS 303 Design and Analysis of Algorithms

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

CSC Design and Analysis of Algorithms

Heaps Outline and Required Reading: Heaps ( 7.3) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

CMPS 2200 Fall 2017 B-trees Carola Wenk

Linked Structures Songs, Games, Movies Part III. Fall 2013 Carola Wenk

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

University of Waterloo CS240, Winter 2010 Assignment 2

COMP Analysis of Algorithms & Data Structures

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

Lecture 3. Recurrences / Heapsort

Data Structures and Algorithms Week 4

Data Structures and Algorithms Chapter 4

Theory and Frontiers of Computer Science. Fall 2013 Carola Wenk

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

COMP Analysis of Algorithms & Data Structures

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

CSci 231 Final Review

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

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

P and NP (Millenium problem)

Chapter 6 Heap and Its Application

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

403: Algorithms and Data Structures. Heaps. Fall 2016 UAlbany Computer Science. Some slides borrowed by David Luebke

Hiroki Yasuga, Elisabeth Kolp, Andreas Lang. 25th September 2014, Scientific Programming

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

SORTING AND SELECTION

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

Lecture 6: Analysis of Algorithms (CS )

CS 6463: AT Computational Geometry Spring 2006 Convex Hulls Carola Wenk

Data Structures and Algorithms CMPSC 465

Introduction to Algorithms October 12, 2005 Massachusetts Institute of Technology Professors Erik D. Demaine and Charles E. Leiserson Quiz 1.

COMP Data Structures

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

Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang

Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Acknowledgement. Outline

CS 161 Summer 2009 Homework #1 Sample Solutions

We don t have much time, so we don t teach them [students]; we acquaint them with things that they can learn. Charles E. Leiserson

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Transcription:

CS 3343 Fall 2007 Sorting Carola Wenk Slides courtesy of Charles Leiserson with small changes by Carola Wenk CS 3343 Analysis of Algorithms 1 How fast can we sort? All the sorting algorithms we have seen so far are comparison sorts: only use comparisons to determine the relative order of elements. E.g., insertion sort, merge sort, quicksort, heapsort. The best worst-case running time that we ve seen for comparison sorting is O(n log n). Is O(n log n) the best we can do? Decision trees can help us answer this question. CS 3343 Analysis of Algorithms 2 Sort a 1, a 2,, a n The left subtree shows subsequent comparisons if a i a j. The right subtree shows subsequent comparisons if a i a j. CS 3343 Analysis of Algorithms 3 The left subtree shows subsequent comparisons if a i a j. The right subtree shows subsequent comparisons if a i a j. CS 3343 Analysis of Algorithms 4

9 4 9 6 The left subtree shows subsequent comparisons if a i a j. The right subtree shows subsequent comparisons if a i a j. CS 3343 Analysis of Algorithms 5 The left subtree shows subsequent comparisons if a i a j. The right subtree shows subsequent comparisons if a i a j. CS 3343 Analysis of Algorithms 6 4 6 a 1 a 2 a 3 The left subtree shows subsequent comparisons if a i a j. The right subtree shows subsequent comparisons if a i a j. CS 3343 Analysis of Algorithms 7 a 3 a 1 a 2 a 2 a 3 a 1 a 3 a 2 a 1 a 1 a 3 a 2 a 2 a 3 a 1 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. CS 3343 Analysis of Algorithms 8

Decision-tree model A decision tree can model the execution of any comparison sorting algorithm: One tree for each input size n. The tree contains all possible comparisons (= if-branches) that could be executed for any input of size n. The tree contains all comparisons along all possible instruction traces (= control flows) for all inputs of size n. For one input, only one path to a leaf is executed. Running time = length of the path taken. Worst-case running time = height of tree. Lower bound for comparison sorting Theorem. Any decision tree that can sort n elements must have height Ω(n log 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 log(n!) (log is mono. increasing) log ((n/e) n ) (Stirling s formula) = n log n n log e = Ω(n log n). CS 3343 Analysis of Algorithms 9 CS 3343 Analysis of Algorithms 10 Lower bound for comparison sorting Corollary. Heapsort and merge sort are asymptotically optimal comparison sorting algorithms. Sorting in linear time Counting sort: No comparisons between elements. Input: A[1.. n], where A[ j] {1, 2,, k}. Output: B[1.. n], sorted. Auxiliary storage: C[1.. k]. CS 3343 Analysis of Algorithms 11 CS 3343 Analysis of Algorithms 12

Counting sort Counting-sort example 1. for i 1 to k do C[i] 0 2. for j 1 to n 3. for i 2 to k C[i] = {key i} C: CS 3343 Analysis of Algorithms 13 CS 3343 Analysis of Algorithms 14 Loop 1 Loop 2 C: 00 00 00 00 C: 00 00 00 11 1. for i 1 to k do C[i] 0 2. for j 1 to n CS 3343 Analysis of Algorithms 15 CS 3343 Analysis of Algorithms 16

Loop 2 Loop 2 C: 11 00 00 11 C: 11 00 11 11 2. for j 1 to n 2. for j 1 to n CS 3343 Analysis of Algorithms 17 CS 3343 Analysis of Algorithms 18 Loop 2 Loop 2 C: 11 00 11 22 C: 11 00 22 22 2. for j 1 to n 2. for j 1 to n CS 3343 Analysis of Algorithms 19 CS 3343 Analysis of Algorithms 20 2001 by Charles E. Leiserson; small changes by Carola

Loop 3 Loop 3 C: 11 00 22 22 C: 11 00 22 22 C': 11 11 22 22 C': 11 11 33 22 3. for i 2 to k C[i] = {key i} 3. for i 2 to k C[i] = {key i} CS 3343 Analysis of Algorithms 21 CS 3343 Analysis of Algorithms 22 Loop 3 C: 11 00 22 22 C: 11 11 33 55 C': 11 11 33 55 33 C': 11 11 33 55 3. for i 2 to k C[i] = {key i} CS 3343 Analysis of Algorithms 23 CS 3343 Analysis of Algorithms 24 2001 by Charles E. Leiserson; small changes by Carola

C: 11 11 33 55 C: 11 11 22 55 33 C': 11 11 22 55 33 44 C': 11 11 22 55 CS 3343 Analysis of Algorithms 25 CS 3343 Analysis of Algorithms 26 C: 11 11 22 55 C: 11 11 22 44 33 44 C': 11 11 22 44 33 33 44 C': 11 11 22 44 CS 3343 Analysis of Algorithms 27 CS 3343 Analysis of Algorithms 28 2001 by Charles E. Leiserson; small changes by Carola

C: 11 11 22 44 C: 11 11 11 44 33 33 44 C': 11 11 11 44 11 33 33 44 C': 11 11 11 44 CS 3343 Analysis of Algorithms 29 CS 3343 Analysis of Algorithms 30 C: 11 11 11 44 C: 00 11 11 44 11 33 33 44 C': 00 11 11 44 11 33 33 44 44 C': 00 11 11 44 CS 3343 Analysis of Algorithms 31 CS 3343 Analysis of Algorithms 32 2001 by Charles E. Leiserson; small changes by Carola

11 33 33 44 44 C: 00 11 11 44 C': 00 11 11 33 Analysis Θ(k) Θ(n) Θ(k) Θ(n) Θ(n + k) 1. for i 1 to k do C[i] 0 2. for j 1 to n 3. for i 2 to k CS 3343 Analysis of Algorithms 33 CS 3343 Analysis of Algorithms 34 Running time Stable sorting If k = O(n), then counting sort takes Θ(n) time. But, sorting takes Ω(n log n) time! Where s the fallacy? Answer: Comparison sorting takes Ω(n log n) time. Counting sort is not a comparison sort. In fact, not a single comparison between elements occurs! Counting sort is a stable sort: it preserves the input order among equal elements. 11 33 33 44 44 Exercise: What other sorts have this property? CS 3343 Analysis of Algorithms 35 CS 3343 Analysis of Algorithms 36