Sorting Lower Bound Andreas Klappenecker based on slides by Prof. Welch 1.

Slides:



Advertisements
Similar presentations
CPSC 411 Design and Analysis of Algorithms Set 2: Sorting Lower Bound Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 2 1.
Advertisements

Introduction to Algorithms Quicksort
Lower Bounds for Sorting, Searching and Selection
QuickSort Average Case Analysis An Incompressibility Approach Brendan Lucier August 2, 2005.
Heapsort O(n lg n) worst case Another design paradigm –Use of a data structure (heap) to manage information during execution of algorithm Comparision-based.
Analysis of Algorithms CS 477/677 Linear Sorting Instructor: George Bebis ( Chapter 8 )
Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
Lower bound for sorting, radix sort COMP171 Fall 2005.
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
CS 171: Introduction to Computer Science II Quicksort.
© 2004 Goodrich, Tamassia Sorting Lower Bound1. © 2004 Goodrich, Tamassia Sorting Lower Bound2 Comparison-Based Sorting (§ 10.3) Many sorting algorithms.
Lower bound for sorting, radix sort COMP171 Fall 2006.
CS 253: Algorithms Chapter 8 Sorting in Linear Time Credit: Dr. George Bebis.
Sorting Heapsort Quick review of basic sorting methods Lower bounds for comparison-based methods Non-comparison based sorting.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 4 Comparison-based sorting Why sorting? Formal analysis of Quick-Sort Comparison.
More sorting algorithms: Heap sort & Radix sort. Heap Data Structure and Heap Sort (Chapter 7.6)
2 -1 Analysis of algorithms Best case: easiest Worst case Average case: hardest.
© 2004 Goodrich, Tamassia Sorting Lower Bound1. © 2004 Goodrich, Tamassia Sorting Lower Bound2 Comparison-Based Sorting (§ 10.3) Many sorting algorithms.
CPSC 411, Fall 2008: Set 2 1 CPSC 411 Design and Analysis of Algorithms Set 2: Sorting Lower Bound Prof. Jennifer Welch Fall 2008.
CPSC 411, Fall 2008: Set 2 1 CPSC 311 Analysis of Algorithms Sorting Lower Bound Prof. Jennifer Welch Fall 2009.
Sorting. Introduction Assumptions –Sorting an array of integers –Entire sort can be done in main memory Straightforward algorithms are O(N 2 ) More complex.
Divide and Conquer Sorting
CSE 326: Data Structures Sorting Ben Lerner Summer 2007.
Analysis of Algorithms CS 477/677
CSC 2300 Data Structures & Algorithms March 20, 2007 Chapter 7. Sorting.
1 Today’s Material Lower Bounds on Comparison-based Sorting Linear-Time Sorting Algorithms –Counting Sort –Radix Sort.
DAST 2005 Week 4 – Some Helpful Material Randomized Quick Sort & Lower bound & General remarks…
Sorting Chapter 6 Chapter 6 –Insertion Sort 6.1 –Quicksort 6.2 Chapter 5 Chapter 5 –Mergesort 5.2 –Stable Sorts Divide & Conquer.
MergeSort Source: Gibbs & Tamassia. 2 MergeSort MergeSort is a divide and conquer method of sorting.
Sorting Lower Bound1. 2 Comparison-Based Sorting (§ 4.4) Many sorting algorithms are comparison based. They sort by making comparisons between pairs of.
Lower Bounds for Comparison-Based Sorting Algorithms (Ch. 8)
Computer Algorithms Lecture 11 Sorting in Linear Time Ch. 8
Sorting in Linear Time Lower bound for comparison-based sorting
CSE 373 Data Structures Lecture 15
1 More Sorting radix sort bucket sort in-place sorting how fast can we sort?
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
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 2 Sorting. Sorting Problem Insertion Sort, Merge Sort e.g.,
Fall 2015 Lecture 4: Sorting in linear time
Computer Sciences Department1. Sorting algorithm 3 Chapter 6 3Computer Sciences Department Sorting algorithm 1  insertion sort Sorting algorithm 2.
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
Sorting Conclusions David Kauchak cs302 Spring 2013.
CSC 213 Lecture 13: Writing Code & Sorting Lowest Bound.
Midterm Review 1. Midterm Exam Thursday, October 15 in classroom 75 minutes Exam structure: –TRUE/FALSE questions –short questions on the topics discussed.
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,
Heaps and basic data structures David Kauchak cs161 Summer 2009.
Sorting 1. Insertion Sort
Data Structures Haim Kaplan & Uri Zwick December 2013 Sorting 1.
CSCE 411H Design and Analysis of Algorithms Set 10: Lower Bounds Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 10 1 * Slides adapted.
Internal and External Sorting External Searching
Sorting Fundamental Data Structures and Algorithms Aleks Nanevski February 17, 2004.
Sorting Lower Bounds n Beating Them. Recap Divide and Conquer –Know how to break a problem into smaller problems, such that –Given a solution to the smaller.
SORTING AND ASYMPTOTIC COMPLEXITY Lecture 13 CS2110 – Fall 2009.
1 Chapter 8-1: Lower Bound of Comparison Sorts. 2 About this lecture Lower bound of any comparison sorting algorithm – applies to insertion sort, selection.
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
David Kauchak cs062 Spring 2010
Sorting Lower Bound 4/25/2018 8:49 PM
CPSC 411 Design and Analysis of Algorithms
CPSC 411 Design and Analysis of Algorithms
CSCE 411 Design and Analysis of Algorithms
(2,4) Trees 11/15/2018 9:25 AM Sorting Lower Bound Sorting Lower Bound.
CSCE 411 Design and Analysis of Algorithms
(2,4) Trees 12/4/2018 1:20 PM Sorting Lower Bound Sorting Lower Bound.
CS200: Algorithm Analysis
(2,4) Trees 2/28/2019 3:21 AM Sorting Lower Bound Sorting Lower Bound.
CPSC 411 Design and Analysis of Algorithms
David Kauchak cs302 Spring 2012
CS 583 Analysis of Algorithms
Presentation transcript:

Sorting Lower Bound Andreas Klappenecker based on slides by Prof. Welch 1

2 Insertion Sort Review How it works: incrementally build up longer and longer prefix of the array of keys that is in sorted order take the current key, find correct place in sorted prefix, and shift to make room to insert it Finding the correct place relies on comparing current key to keys in sorted prefix Worst-case running time is Θ(n 2 )

3 Insertion Sort Demo

4 Heapsort Review How it works: put the keys in a heap data structure repeatedly remove the min from the heap Manipulating the heap involves comparing keys to each other Worst-case running time is Θ(n log n)

5 Heapsort Demo

6 Mergesort Review How it works: split the array of keys in half recursively sort the two halves merge the two sorted halves Merging the two sorted halves involves comparing keys to each other Worst-case running time is Θ(n log n)

7 Mergesort Demo

8 Quicksort Review How it works: choose one key to be the pivot partition the array of keys into those keys < the pivot and those ≥ the pivot recursively sort the two partitions Partitioning the array involves comparing keys to the pivot Worst-case running time is Θ(n 2 )

9 Quicksort Demo

10 Comparison-Based Sorting All these algorithms are comparison-based the behavior depends on relative values of keys, not exact values behavior on [1,3,2,4] is same as on [9,25,23,99] Fastest of these algorithms was O(n log n). We will show that's the best you can get with comparison- based sorting.

11 Decision Tree Consider any comparison based sorting algorithm Represent its behavior on all inputs of a fixed size with a decision tree Each tree node corresponds to the execution of a comparison Each tree node has two children, depending on whether the parent comparison was true or false Each leaf represents correct sorted order for that path

12 Decision Tree Diagram first comparison: check if a i ≤ a j second comparison if a i ≤ a j : check if a k ≤ a l second comparison if a i > a j : check if a m ≤ a p third comparison if a i ≤ a j and a k ≤ a l : check if a x ≤ a y YES NO

13 Insertion Sort for j := 2 to n to key := a[j] i := j-1 while i > 0 and a[i] > key do a[i+1] := a[i] i := i -1 endwhile a[i+1] := key endfor comparison

14 Insertion Sort for n = 3 a 1 ≤ a 2 ? a 2 ≤ a 3 ?a 1 ≤ a 3 ? a 2 ≤ a 3 ? NOYES a 1 a 2 a 3 a 1 a 3 a 2 a 3 a 1 a 2 a 2 a 3 a 1 a 3 a 2 a 1 a 2 a 1 a 3 NO YES

15 Insertion Sort for n = 3 a 1 ≤ a 2 ? NO a 2 ≤ a 3 ?a 1 ≤ a 3 ? a 2 ≤ a 3 ? YES a 1 a 2 a 3 a 1 a 3 a 2 a 3 a 1 a 2 a 2 a 3 a 1 a 3 a 2 a 1 a 2 a 1 a 3 NO YES

16 How Many Leaves? Must be at least one leaf for each permutation of the input otherwise there would be a situation that was not correctly sorted Number of permutations of n keys is n!. Idea: since there must be a lot of leaves, but each decision tree node only has two children, tree cannot be too shallow depth of tree is a lower bound on running time

17 Key Lemma Height of a binary tree with n! leaves is  (n log n). Proof: The maximum number of leaves in a binary tree with height h is 2 h. h = 1, 2 1 leaves h = 2, 2 2 leaves h = 3, 2 3 leaves

18 Proof of Lemma Let h be the height of decision tree, so it has at most 2 h leaves. The actual number of leaves is n!, hence 2 h ≥ n! h ≥ log(n!) = log(n(n-1)(n-1)…(2)(1)) ≥ (n/2)log(n/2) by algebra = (n log n)

19 Finishing Up Any binary tree with n! leaves has height (n log n). Decision tree for any c-b sorting alg on n keys has height (n log n). Any c-b sorting alg has at least one execution with (n log n) comparisons Any c-b sorting alg has (n log n) worst-case running time.