CS221: Algorithms and Data Structures. Sorting Takes Priority. Steve Wolfman (minor tweaks by Alan Hu)

Similar documents
Unit Outline. Comparing Sorting Algorithms Heapsort Mergesort Quicksort More Comparisons Complexity of Sorting 2 / 33

Sorting. CPSC 259: Data Structures and Algorithms for Electrical Engineers. Hassan Khosravi

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

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

Sorting and Selection

Divide and Conquer Sorting Algorithms and Noncomparison-based

Chapter 7 Sorting. Terminology. Selection Sort

4. Sorting and Order-Statistics

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

Outline. Quadratic-Time Sorting. Linearithmic-Time Sorting. Conclusion. Bubble/Shaker Sort Insertion Sort Odd-Even Sort

algorithm evaluation Performance

Sorting Algorithms. + Analysis of the Sorting Algorithms

Data Structures and Algorithms

COMP Data Structures

Sorting Algorithms Day 2 4/5/17

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CSE 373 NOVEMBER 8 TH COMPARISON SORTS

Sorting. Task Description. Selection Sort. Should we worry about speed?

CSE373: Data Structure & Algorithms Lecture 18: Comparison Sorting. Dan Grossman Fall 2013

CSE 373: Data Structures and Algorithms

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

DATA STRUCTURES AND ALGORITHMS

CS61BL. Lecture 5: Graphs Sorting

CSE 143. Two important problems. Searching and Sorting. Review: Linear Search. Review: Binary Search. Example. How Efficient Is Linear Search?

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

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

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

A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 3 Parallel Prefix, Pack, and Sorting

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

Programming II (CS300)

Sorting. Bringing Order to the World

Programming II (CS300)

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING

PRAM Divide and Conquer Algorithms

Mergesort again. 1. Split the list into two equal parts

Sorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort

EECS 2011M: Fundamentals of Data Structures

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

Algorithms in Systems Engineering ISE 172. Lecture 12. Dr. Ted Ralphs

Lecture 8: Mergesort / Quicksort Steven Skiena

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

CSE 373: Data Structures and Algorithms

// walk through array stepping by step amount, moving elements down for (i = unsorted; i >= step && item < a[i-step]; i-=step) { a[i] = a[i-step];

Merge Sort

11/8/2016. Chapter 7 Sorting. Introduction. Introduction. Sorting Algorithms. Sorting. Sorting

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

CS : Data Structures

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

Learning Goals. CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues. Today s Outline. Back to Queues. Priority Queue ADT

Outline. Computer Science 331. Three Classical Algorithms. The Sorting Problem. Classical Sorting Algorithms. Mike Jacobson. Description Analysis

CHAPTER 7 Iris Hui-Ru Jiang Fall 2008

Sorting. CSE 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation. CSE143 Au

ECE 2574: Data Structures and Algorithms - Basic Sorting Algorithms. C. L. Wyatt

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

QuickSort

Design and Analysis of Algorithms

CS240 Fall Mike Lam, Professor. Quick Sort

Comparison Sorts. Chapter 9.4, 12.1, 12.2

Overview of Sorting Algorithms

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

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

CSCI 2132 Software Development Lecture 18: Implementation of Recursive Algorithms

Sorting. CSC 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation CSC Picture

Searching and Sorting

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

Merge Sort. Algorithm Analysis. November 15, 2017 Hassan Khosravi / Geoffrey Tien 1

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

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

Better sorting algorithms (Weiss chapter )

University of Waterloo CS240 Winter 2018 Assignment 2. Due Date: Wednesday, Jan. 31st (Part 1) resp. Feb. 7th (Part 2), at 5pm

COSC 311: ALGORITHMS HW1: SORTING

07 B: Sorting II. CS1102S: Data Structures and Algorithms. Martin Henz. March 5, Generated on Friday 5 th March, 2010, 08:31

We can use a max-heap to sort data.

Table ADT and Sorting. Algorithm topics continuing (or reviewing?) CS 24 curriculum

Mergesort again. 1. Split the list into two equal parts

CIS 121 Data Structures and Algorithms with Java Spring Code Snippets and Recurrences Monday, January 29/Tuesday, January 30

Analyze the obvious algorithm, 5 points Here is the most obvious algorithm for this problem: (LastLargerElement[A[1..n]:

Divide and Conquer Algorithms: Advanced Sorting

Priority Queues, Heaps, and Heapsort

CSE 373: Data Structures & Algorithms More Sor9ng and Beyond Comparison Sor9ng

CS 361 Data Structures & Algs Lecture 9. Prof. Tom Hayes University of New Mexico

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

SORTING AND SELECTION

SORTING, SETS, AND SELECTION

Quicksort (Weiss chapter 8.6)

Lower bound for comparison-based sorting

Practical Session #11 - Sort properties, QuickSort algorithm, Selection

Lecture Notes on Quicksort

Cosc 241 Programming and Problem Solving Lecture 17 (30/4/18) Quicksort

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

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

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

CS125 : Introduction to Computer Science. Lecture Notes #40 Advanced Sorting Analysis. c 2005, 2004 Jason Zych

CSE 373: Data Structures and Algorithms

CS Sorting Terms & Definitions. Comparing Sorting Algorithms. Bubble Sort. Bubble Sort: Graphical Trace

II (Sorting and) Order Statistics

Lecture Notes on Quicksort

Lecture 7 Quicksort : Principles of Imperative Computation (Spring 2018) Frank Pfenning

Transcription:

CS221: Algorithms and Data Structures Sorting Takes Priority Steve Wolfman (minor tweaks by Alan Hu) 1

Today s Outline Sorting with Priority Queues, Three Ways 2

How Do We Sort with a Priority Queue? You have a bunch of data. You want to sort by priority. You have a priority queue. WHAT DO YOU DO? G(9) insert F(7) E(5) D(100) A(4) B(6) deletemin C(3) 3

PQSort Sort(elts): pq = new PQ for each elt in elts: pq.insert(elt); sortedelts = new array of size elts.length for i = 0 to elts.length 1: sortedelts[i] = pq.deletemin return sortedelts What sorting algorithm is this? a. Insertion Sort b. Selection Sort c. Heap Sort d. Merge Sort e. None of these 4

PQSort Sort(elts): pq = new PQ for each elt in elts: pq.insert(elt); sortedelts = new array of size elts.length for i = 0 to elts.length 1: sortedelts[i] = pq.deletemin return sortedelts Abstract Data Type vs. Data Structure That Implements It What sorting algorithm is this? a. Insertion Sort b. Selection Sort c. Heap Sort d. Merge Sort e. None of these 5

Reminder: Naïve Priority Q Data Structures Unsorted list: insert: worst case O(1) deletemin: worst case O(n) Sorted list: insert: worst case O(n) deletemin: worst case O(1) 6

PQSort deletemins with Unsorted List PQ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 5 9 4 8 1 6 10 12 13 2 3 14 20 7 PQ

PQSort deletemins with Unsorted List PQ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 5 9 4 8 1 6 10 12 13 2 3 14 20 7 PQ

PQSort deletemins with Unsorted List PQ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 5 9 4 8 6 10 12 13 2 3 14 20 7 1 PQ

PQSort deletemins with Unsorted List PQ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 5 9 4 8 6 10 12 13 2 3 14 20 7 1 PQ

PQSort deletemins with Unsorted List PQ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 5 9 4 8 6 10 12 13 3 14 20 7 1 2 PQ

PQSort deletemins with Unsorted List PQ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 5 9 4 8 6 10 12 13 14 20 7 1 2 3 PQ

PQSort deletemins with Unsorted List PQ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 5 9 8 6 10 12 13 14 20 7 1 2 3 4 PQ

Two PQSort Tricks 1) Use the array to store both your results and your PQ. No extra memory needed! 2) Use a max-heap to sort in increasing order (or a min-heap to sort in decreasing order) so your heap doesn t move during deletions. 14

5 PQSort deletemaxes with Unsorted List MAX-PQ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 9 4 8 1 6 10 12 13 2 3 14 20 7 5 5 5 PQ 9 4 8 1 6 10 12 13 2 3 14 7 20 PQ Result 9 4 8 1 6 10 12 13 2 3 7 14 20 PQ Result 9 4 8 1 6 10 12 7 2 3 13 14 20 PQ 15 Result

PQSort deletemaxes with Unsorted List MAX-PQ How long does build take? No time at all! How long do the deletions take? Worst case: O(n 2 ) What algorithm is this? a. Insertion Sort b. Selection Sort c. Heap Sort d. Merge Sort e. None of these 5 9 4 8 1 6 10 12 7 2 3 13 14 20 PQ 16 Result

5 PQSort insertions with Sorted List MAX-PQ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 9 4 8 1 6 10 12 13 2 3 14 20 7 PQ 5 5 5 9 4 8 1 6 10 12 13 2 3 14 7 20 PQ 9 4 8 1 6 10 12 13 2 3 7 14 20 PQ 9 4 8 1 6 10 12 13 2 3 7 14 20 PQ 17

PQSort insertions with Sorted List MAX-PQ How long does build take? Worst case: O(n 2 ) How long do the deletions take? No time at all! What algorithm is this? a. Insertion Sort b. Selection Sort c. Heap Sort d. Merge Sort e. None of these 5 9 4 8 1 6 10 12 13 2 3 7 14 20 18 PQ

PQSort Build with Heap MAX-PQ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 5 9 4 8 1 6 10 12 13 2 3 14 20 7 Floyd s Algorithm 20 13 14 12 3 6 10 9 8 2 1 4 5 7 PQ Takes only O(n) time! 19

PQSort deletemaxes with Heap MAX-PQ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 20 14 13 12 13 14 12 3 6 10 9 8 2 1 4 5 7 PQ 13 10 12 3 6 7 9 8 2 1 4 5 20 PQ 12 10 9 3 6 7 5 8 2 1 4 14 20 PQ 9 10 8 3 6 7 5 4 2 1 13 14 20 PQ 20 Totally incomprehensible as an array!

PQSort deletemaxes with Heap MAX-PQ 5 9 4 8 1 6 10 12 13 2 3 14 20 7 5 9 4 8 1 6 10 12 13 2 3 21 14 20 7

PQSort deletemaxes with Heap MAX-PQ 5 20 9 4 13 14 8 1 6 10 12 3 6 10 12 13 2 3 14 20 7 9 8 2 1 4 5 7 Build Heap 22 Note: 9 ends up being perc d down as well since its invariant is violated by the time we reach it.

PQSort deletemaxes with Heap MAX-PQ 20 14 13 13 14 13 10 12 10 12 3 6 10 12 3 6 7 9 3 6 7 9 8 2 1 4 5 7 9 8 2 1 4 5 20 5 8 2 1 4 14 20 9 10 12 8 7 9 7 9 10 5 3 6 1 8 3 6 1 8 3 6 7 2 4 10 12 13 14 20 5 4 2 12 13 14 20 5 4 2 1 13 14 20

PQSort with Heap MAX-PQ How long does build take? Worst case: O(n) How long do the deletions take? Worst case: O(n lg n) What algorithm is this? a. Insertion Sort b. Selection Sort c. Heap Sort d. Merge Sort e. None of these 8 5 2 4 9 7 3 6 1 10 12 13 14 20 9 8 7 5 3 6 1 2 4 10 12 13 14 20 PQ Result 24

PQSort Sort(elts): What sorting algorithm is this? a. Insertion Sort b. Selection Sort c. Heap Sort d. Merge Sort e. None of these pq = new PQ for each elt in elts: pq.insert(elt); sortedelts = new array of size elts.length for i = 0 to elements.length 1: sortedelts[i] = pq.deletemin return sortedelts 25

CS221: Algorithms and Data Structures Sorting Things Out (slides stolen from Steve Wolfman with minor tweaks by Alan Hu) 26

Today s Outline Categorizing/Comparing Sorting Algorithms PQSorts as examples MergeSort QuickSort More Comparisons Complexity of Sorting 27

Categorizing Sorting Algorithms Computational complexity Average case behaviour: Why do we care? Worst/best case behaviour: Why do we care? How often do we resort sorted, reverse sorted, or almost sorted (k swaps from sorted where k << n) lists? Stability: What happens to elements with identical keys? Memory Usage: How much extra memory is used? 28

Comparing our PQSort Algorithms Computational complexity Selection Sort: Always makes n passes with a triangular shape. Best/worst/average case Θ(n 2 ) Insertion Sort: Always makes n passes, but if we re lucky (and do linear search from left), only constant work is needed on each pass. Best case Θ(n); worst/average case: Θ(n 2 ) Heap Sort: Always makes n passes needing O(lg n) on each pass. Best/worst/average case: Θ(n lg n). 29 Note: best cases assume distinct elements. With identical elements, Heap Sort can get Θ(n) performance.

Insertion Sort Best Case 0 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 1 1 2 3 4 5 6 7 8 9 10 11 12 13 PQ 14 2 3 4 5 6 7 8 9 10 11 12 13 PQ 14 PQ 2 3 4 5 6 7 8 9 10 11 12 13 14 If we do linear search from the left: constant time per pass! 30 PQ

Comparing PQSort Algorithms Stability Selection: Easily made stable (when building from the right, prefer the rightmost of identical biggest keys). Insertion: Easily made stable (when building from the right, find the leftmost slot for a new element). Heap: Unstable Memory use: All three are essentially in-place algorithms with small O(1) extra space requirements. Cache access: Not detailed in 221, but algorithms that don t jump around tend to perform better in modern memory systems. Which of these jumps around? 31

T(n)=100 n 2 Comparison of growth... nlgn n n=100 32

Today s Outline Categorizing/Comparing Sorting Algorithms PQSorts as examples MergeSort QuickSort More Comparisons Complexity of Sorting 33

MergeSort Mergesort belongs to a class of algorithms known as divide and conquer algorithms (your recursion sense should be tingling here...). The problem space is continually split in half, recursively applying the algorithm to each half until the base case is reached. 34

MergeSort Algorithm 1. If the array has 0 or 1 elements, it s sorted. Else 2. Split the array into two halves 3. Sort each half recursively (i.e., using mergesort) 4. Merge the sorted halves to produce one sorted result: 1. Consider the two halves to be queues. 2. Repeatedly compare the fronts of the queues. Whichever is smaller (or, if one is empty, whichever is left), dequeue it and insert it into the result. 35

MergeSort Performance Analysis 1. If the array has 0 or 1 elements, it s sorted. Else 2. Split the array into two halves 3. Sort each half recursively (i.e., using mergesort) 4. Merge the sorted halves to produce one sorted result: 1. Consider the two halves to be queues. T(1) = 1 2*T(n/2) 2. Repeatedly compare the fronts of the queues. Whichever is smaller (or, if one is empty, whichever is left), dequeue it and insert it into the result. n 36

MergeSort Performance Analysis T(1) = 1 T(n) = 2T(n/2) + n = 4T(n/4) + 2(n/2) + n = 8T(n/8) + 4(n/4) + 2(n/2) + n = 8T(n/8) + n + n + n = 8T(n/8) + 3n = 2 i T(n/2 i ) + in. Let i = lg n T(n) = nt(1) + n lg n = n + n lg n Θ(n lg n) We ignored floors/ceilings. To prove performance formally, we d use 37 this as a guess and prove it with floors/ceilings by induction.

Consider the following array of integers: 3-4 7 5 9 6 2 1 3-4 7 5 9 6 2 1 3-4 7 5 9 6 2 1 3-4 7 5 9 6 2 1-4 3 5 7 6 9 1 2-4 3 5 7 1 2 6 9-4 1 2 3 5 6 7 9 38

Mergesort: void msort(int x[], int lo, int hi, int tmp[]) { if (lo >= hi) return; int mid = (lo+hi)/2; msort(x, lo, mid, tmp); msort(x, mid+1, hi, tmp); merge(x, lo, mid, hi, tmp); } void mergesort(int x[], int n) { int *tmp = new int[n]; msort(x, 0, n-1, tmp); delete[] tmp; } 39

Merge: void merge(int x[],int lo,int mid,int hi, int tmp[]) { int a = lo, b = mid+1; for( int k = lo; k <= hi; k++ ) { if( a <= mid && (b > hi x[a] < x[b]) ) tmp[k] = x[a++]; else tmp[k] = x[b++]; } for( int k = lo; k <= hi; k++ ) x[k] = tmp[k]; } 40

merge( x, 0, 0, 1, tmp ); // step * x: tmp: x: 3-4 7 5 9 6 2 1-4 3-4 3 7 5 9 6 2 1 merge( x, 4, 5, 7, tmp ); // step ** x: tmp: x: -4 3 5 7 6 9 1 2 1 2 6 9-4 3 5 7 1 2 6 9 merge( x, 0, 3, 7, tmp ); // will be the final step 41

Today s Outline Categorizing/Comparing Sorting Algorithms PQSorts as examples MergeSort QuickSort More Comparisons Complexity of Sorting 42

QuickSort In practice, one of the fastest sorting algorithms is Quicksort, developed in 1961 by C.A.R. Hoare. Comparison-based: examines elements by comparing them to other elements Divide-and-conquer: divides into halves (that may be very unequal) and recursively sorts 43

QuickSort algorithm Pick a pivot Reorder the list such that all elements < pivot are on the left, while all elements >= pivot are on the right Recursively sort each side Are we missing a base 44 case?

Partitioning The act of splitting up an array according to the pivot is called partitioning Consider the following: -4 1-3 2 3 5 4 7 left partition pivot right partition 45

QuickSort Visually P P P P P P P P Sorted! 46

QuickSort (by Jon Bentley): void qsort(int x[], int lo, int hi) { int i, p; if (lo >= hi) return; p = lo; for( i=lo+1; i <= hi; i++ ) if( x[i] < x[lo] ) swap(x[++p], x[i]); swap(x[lo], x[p]); qsort(x, lo, p-1); qsort(x, p+1, hi); } void quicksort(int x[], int n) { qsort(x, 0, n-1); } 47

QuickSort (by Jon Bentley): (Loop invariant by Alan!) void qsort(int x[], int lo, int hi) { int i, p; if (lo >= hi) return; p = lo; for( i=lo+1; i <= hi; i++ ) // x[lo+1..p] contains all elements of // x[lo+1..i-1] that are less than x[lo] if( x[i] < x[lo] ) swap(x[++p], x[i]); swap(x[lo], x[p]); qsort(x, lo, p-1); qsort(x, p+1, hi); } 48

QuickSort Example (using Bentley s Algorithm) 2-4 6 1 5-3 3 7 49

QuickSort: Complexity In our partitioning task, we compared each element to the pivot Thus, the total number of comparisons is N As with MergeSort, if one of the partitions is about half (or any constant fraction of) the size of the array, complexity is Θ(n lg n). In the worst case, however, we end up with a partition with a 1 and n-1 split 50

QuickSort Visually: Worst case P P P P 51

QuickSort: Worst Case In the overall worst-case, this happens at every step Thus we have N comparisons in the first step N-1 comparisons in the second step N-2 comparisons in the third step : n + (n 1) + +... 2 +1= n(n +1) 2 = n 2 2 + n 2 or O(n 2) 52

QuickSort: Average Case (Intuition) Clearly pivot choice is important It has a direct impact on the performance of the sort Hence, QuickSort is fragile, or at least attackable So how do we pick a good pivot? 53

QuickSort: Average Case (Intuition) Let s assume that pivot choice is random Half the time the pivot will be in the centre half of the array Thus at worst the split will be n/4 and 3n/4 54

QuickSort: Average Case (Intuition) We can apply this to the notion of a good split Every good split: 2 partitions of size n/4 and 3n/4 Or divides N by 4/3 Hence, we make up to log4/3(n) splits Expected # of partitions is at most 2 * log4/3(n) O(logN) Given N comparisons at each partitioning step, we have Θ(N log N) 55

Today s Outline Categorizing/Comparing Sorting Algorithms PQSorts as examples MergeSort QuickSort More Comparisons Complexity of Sorting 56

How Do Quick, Merge, Heap, Insertion, and Selection Sort Compare? Complexity Best case: Insert < Quick, Merge, Heap < Select Average case: Quick, Merge, Heap < Insert, Select Worst case: Merge, Heap < Quick, Insert, Select Usually on real data: Quick < Merge < Heap < I/S On very short lists: quadratic sorts may have an advantage (so, some quick/merge implementations bottom out to these as base cases) (not asymptotic) Some details depend on implementation! (E.g., an initial check whether the last elt of the left sublist is less 57 than first of the right can make merge s best case linear.)

How Do Quick, Merge, Heap, Insertion, Stability and Selection Sort Compare? Easily Made Stable: Insert, Select, Merge (prefer the left of the two sorted sublists on ties) Unstable: Heap Challenging to Make Stable: Quick Memory use: Insert, Select, Heap < Quick < Merge 58 How much stack space does recursive QuickSort use? In the worst case? Could we make it better?

Today s Outline Categorizing/Comparing Sorting Algorithms PQSorts as examples MergeSort QuickSort More Comparisons Complexity of Sorting 59

Complexity of Sorting Using Comparisons as a Problem Each comparison is a choice point in the algorithm. You can do one thing if the comparison is true and another if false. So, the whole algorithm is like a binary tree yes x < y no a < b a < d yes no yes no sorted! c < d z < c sorted! yes no yes no 60

Complexity of Sorting Using Comparisons as a Problem The algorithm spits out a (possibly different) sorted list at each leaf. What s the maximum number of leaves? yes x < y no a < b a < d yes no yes no sorted! c < d z < c sorted! yes no yes no 61

Complexity of Sorting Using Comparisons as a Problem There are n! possible permutations of a sorted list (i.e., input orders for a given set of input elements). How deep must the tree be to distinguish those input orderings? yes x < y no a < b a < d yes no yes no sorted! c < d z < c sorted! yes no yes no 62

Complexity of Sorting Using Comparisons as a Problem If the tree is not at least lg(n!) deep, then there s some pair of orderings I could feed the algorithm which the algorithm does not distinguish. So, it must not successfully sort one of those two orderings. yes x < y no a < b a < d yes no yes no sorted! c < d z < c sorted! yes no yes no 63

Complexity of Sorting Using Comparisons as a Problem QED: The complexity of sorting using comparisons is Ω(n lg n) in the worst case, regardless of algorithm! In general, we can lower-bound but not upper-bound the complexity of problems. (Why not? Because I can give as crappy an algorithm as I please to solve any problem.) 64