Copyright © Aiman Hanna All rights reserved

Slides:



Advertisements
Similar presentations
110/6/2014CSE Suprakash Datta datta[at]cse.yorku.ca CSE 3101: Introduction to the Design and Analysis of Algorithms.
Advertisements

Lower Bounds for Sorting, Searching and Selection
© The McGraw-Hill Companies, Inc., Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 24 Sorting.
© 2004 Goodrich, Tamassia Sorting Lower Bound1. © 2004 Goodrich, Tamassia Sorting Lower Bound2 Comparison-Based Sorting (§ 10.3) Many sorting algorithms.
2 -1 Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
© 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 311 Analysis of Algorithms Sorting Lower Bound Prof. Jennifer Welch Fall 2009.
Sorting Lower Bound Andreas Klappenecker based on slides by Prof. Welch 1.
DAST 2005 Week 4 – Some Helpful Material Randomized Quick Sort & Lower bound & General remarks…
Sorting Lower Bound1. 2 Comparison-Based Sorting (§ 4.4) Many sorting algorithms are comparison based. They sort by making comparisons between pairs of.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 26 Sorting.
CSC 213 Lecture 13: Writing Code & Sorting Lowest Bound.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.
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.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 25 Sorting.
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
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.
Comp 248 Introduction to Programming Chapter 6 Arrays Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 26 Sorting.
What is the runtime of the best possible (comparison based) sorting algorithm? 1.O(log n) 2.O(n) 3.O(n log n) 4.O(n 2 ) 5.None of the above.
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
Decision trees Polynomial-Time
CPSC 411 Design and Analysis of Algorithms
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
CPSC 411 Design and Analysis of Algorithms
More on Merge Sort CS 244 This presentation is not given in class
Objectives Introduce different known sorting algorithms
CSCE 411 Design and Analysis of Algorithms
Quick-Sort 11/14/2018 2:17 PM Chapter 4: Sorting    7 9
Bucket-Sort and Radix-Sort
(2,4) Trees 11/15/2018 9:25 AM Sorting Lower Bound Sorting Lower Bound.
Lecture 5 Algorithm Analysis
Copyright © Aiman Hanna All rights reserved
Quick-Sort 11/19/ :46 AM Chapter 4: Sorting    7 9
The Complexity of Algorithms and the Lower Bounds of Problems
Ch8: Sorting in Linear Time Ming-Te Chi
Breadth-First Search (BFS)
Lecture 5 Algorithm Analysis
© 2013 Goodrich, Tamassia, Goldwasser
Copyright © Aiman Hanna All rights reserved
(2,4) Trees 12/4/2018 1:20 PM Sorting Lower Bound Sorting Lower Bound.
Ch. 8 Priority Queues And Heaps
Ordered Maps & Dictionaries
Copyright © Aiman Hanna All rights reserved
Copyright © Aiman Hanna All rights reserved
Linear Sorting Sorting in O(n) Jeff Chastine.
Copyright © Aiman Hanna All rights reserved
Quick-Sort 2/23/2019 1:48 AM Chapter 4: Sorting    7 9
Lecture 5 Algorithm Analysis
(2,4) Trees 2/28/2019 3:21 AM Sorting Lower Bound Sorting Lower Bound.
Copyright © Aiman Hanna All rights reserved
Copyright © Aiman Hanna All rights reserved
Linked Lists & Iterators
Copyright © Aiman Hanna All rights reserved
Topic 5: Heap data structure heap sort Priority queue
Copyright © Aiman Hanna All rights reserved
CPSC 411 Design and Analysis of Algorithms
High-level Synchronization (Continue)
Sorting We have actually seen already two efficient ways to sort:
Lecture 5 Algorithm Analysis
Presentation transcript:

Copyright © 2011-2016 Aiman Hanna All rights reserved Sorting Lower Bound Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal, Canada These slides has been extracted, modified and updated from original slides of : Data Structures and Algorithms in Java, 5th edition. John Wiley& Sons, 2010. ISBN 978-0-470-38326-1. Data Structures and the Java Collections Framework by William J. Collins, 3rdedition, ISBN 978-0-470-48267-4. Both books are published by Wiley. Copyright © 2010-2011 Wiley Copyright © 2010 Michael T. Goodrich, Roberto Tamassia Copyright © 2011 William J. Collins Copyright © 2011-2016 Aiman Hanna All rights reserved

Coverage Sorting Lower Bound Sorting Lower Bound

Comparison-Based Sorting Many sorting algorithms are comparison based. They sort by making comparisons between pairs of objects Examples: bubble-sort, selection-sort, insertion-sort, heap-sort, merge-sort, quick-sort, ... Some of these algorithms were able to provide a performance of O(n log n). This question can then be asked: Can we sort any faster than O(n log n)? In other words, what is the lower bound (best case) that we can achieve when sorting (that is actually Ω())? Sorting Lower Bound

Comparison-Based Sorting Let us start by deriving a lower bound on the running time of any algorithm that uses comparisons to sort n elements, x1, x2, …, xn. Let us then count only the number of comparisons for the sake of finding the lower bound. Is xi < xj? yes no Sorting Lower Bound

Counting Comparisons Suppose, we are given a sequence S = {x1, x2, …, xn} that we wish to sort. Whether the sequence is implemented using an arrays or a list is irrelevant since we are counting comparisons. Each time we compare two elements, xi < xj, the result is either yes or no. Based on this answer, some internal computation may be performed (which we ignore here), then another comparison is conducted, which will again have one of two possible outcomes. Sorting Lower Bound

Counting Comparisons Consequently, we can represent a comparison-based sorting with a decision tree. Each possible run of the algorithm corresponds to a root-to-leaf path in that tree. Sorting Lower Bound

Counting Comparisons It is important to note that the algorithm may have no explicit knowledge of the tree; rather, it simply represent all the possible sequences of comparisons that the application might make. Each leaf hence represents one possible permutation (sorted sequence) of the elements to be compared. The number of leaves can then conclude the height of the tree. Given n values to compare, That number of leaves (possible permutations) is actually n! Sorting Lower Bound

Counting Comparisons Example: Assume a sequence S with elements {x1, x2, x3} is to be sorted. There are 3! possible sorted sequences as follows: (x1 x2 x3), (x1 x3 x2), (x2 x1 x3), (x2 x3 x1), (x3 x1 x2), and (x3 x2 x1). The following tree illustrates the possible comparisons: x1 < x2 x1 < x3 x2 < x3 Y N x1 x2 x3 x1 x3 x2 x3 x1 x2 x3 x2 x1 x2 x1 x3 x2 x3 x1 Sorting Lower Bound

Decision Tree Height The height of the decision tree is a lower bound on the running time. Since there are n! leaves, the height of the tree is at least log (n!). Sorting Lower Bound

The Lower Bound Any comparison-based sorting algorithms takes at least log (n!) time Therefore, any such algorithm takes, at least, time That is, any comparison-based sorting algorithm must run in at least W(n log n) time. In other words, we comparison-based algorithms, we cannot achieve any better performance than n log n. However, can we do any better if the algorithm is not a comparison-based then? Are such algorithms possible? Notes: You should notice that this time is not for every element. This time is already based on the number of elements to compare (which is n), which was the reason we have this tree with n! leaves. In short, this is the time needed to sort n elements using comparisons. In other words, if following any root-to-leaf path will result in the final sorting. Since this root-to-leaf path is limited to the height of the tree, which is log n!, sorting is o(log n!). This is already bigger than O(nlog n), which means we cannot sort in any less than nlogn  (n log n) Sorting Lower Bound