How to Win Coding Competitions: Secrets of Champions. Week 3: Sorting and Search Algorithms Lecture 7: Lower bound. Stable sorting.
|
|
- Julianna Cain
- 5 years ago
- Views:
Transcription
1 How to Win Coding Competitions: Secrets of Champions Week 3: Sorting and Search Algorithms Lecture 7: Lower bound. Stable sorting. Comparators Maxim Buzdalov Saint Petersburg 2016
2 Lower bound for comparison sorting Theorem. For every comparison-based sorting algorithm, there exists an input with N elements which requires at least b = Ω(N log N) comparisons. 2 / 6
3 Lower bound for comparison sorting Theorem. For every comparison-based sorting algorithm, there exists an input with N elements which requires at least b = Ω(N log N) comparisons. Proof Assume inputs are permutations. There are N! different permutations 2 / 6
4 Lower bound for comparison sorting Theorem. For every comparison-based sorting algorithm, there exists an input with N elements which requires at least b = Ω(N log N) comparisons. Proof Assume inputs are permutations. There are N! different permutations From a single comparison, any algorithm receives at most one bit of information 2 / 6
5 Lower bound for comparison sorting Theorem. For every comparison-based sorting algorithm, there exists an input with N elements which requires at least b = Ω(N log N) comparisons. Proof Assume inputs are permutations. There are N! different permutations From a single comparison, any algorithm receives at most one bit of information After making t comparisons, an algorithm can distinguish only 2 t cases 2 / 6
6 Lower bound for comparison sorting Theorem. For every comparison-based sorting algorithm, there exists an input with N elements which requires at least b = Ω(N log N) comparisons. Proof Assume inputs are permutations. There are N! different permutations From a single comparison, any algorithm receives at most one bit of information After making t comparisons, an algorithm can distinguish only 2 t cases Thus, for the lower bound b, it holds that 2 b N!, or b log 2 (N!) 2 / 6
7 Lower bound for comparison sorting Theorem. For every comparison-based sorting algorithm, there exists an input with N elements which requires at least b = Ω(N log N) comparisons. Proof Assume inputs are permutations. There are N! different permutations From a single comparison, any algorithm receives at most one bit of information After making t comparisons, an algorithm can distinguish only 2 t cases Thus, for the lower bound b, it holds that 2 b N!, or b log 2 (N!) By Stirling s approximation: ( ( n ) n ) log 2 (N!) log 2 2πn = n log e 2 n Θ(n) + O(log n) = Θ(n log n) 2 / 6
8 Lower bound for comparison sorting Theorem. For every comparison-based sorting algorithm, there exists an input with N elements which requires at least b = Ω(N log N) comparisons. Proof Assume inputs are permutations. There are N! different permutations From a single comparison, any algorithm receives at most one bit of information After making t comparisons, an algorithm can distinguish only 2 t cases Thus, for the lower bound b, it holds that 2 b N!, or b log 2 (N!) By Stirling s approximation: ( ( n ) n ) log 2 (N!) log 2 2πn = n log e 2 n Θ(n) + O(log n) = Θ(n log n) Mergesort is asymptotically optimal. 2 / 6
9 Lower bound for comparison sorting Theorem. For every comparison-based sorting algorithm, there exists an input with N elements which requires at least b = Ω(N log N) comparisons. Proof Assume inputs are permutations. There are N! different permutations From a single comparison, any algorithm receives at most one bit of information After making t comparisons, an algorithm can distinguish only 2 t cases Thus, for the lower bound b, it holds that 2 b N!, or b log 2 (N!) By Stirling s approximation: ( ( n ) n ) log 2 (N!) log 2 2πn = n log e 2 n Θ(n) + O(log n) = Θ(n log n) Mergesort is asymptotically optimal. Quicksort is asymptotically optimal on average. 2 / 6
10 Stable sorting I What is stable sorting? 3 / 6
11 Stable sorting I What is stable sorting? A sorting algorithm is stable if it preserves the order of equal elements 3 / 6
12 Stable sorting I What is stable sorting? A sorting algorithm is stable if it preserves the order of equal elements Why is it useful? 3 / 6
13 Stable sorting I What is stable sorting? A sorting algorithm is stable if it preserves the order of equal elements Why is it useful? Assume elements are already ordered by 1 You wish to sort them by 2, but keep equal elements ordered by 1 3 / 6
14 Stable sorting I What is stable sorting? A sorting algorithm is stable if it preserves the order of equal elements Why is it useful? Assume elements are already ordered by 1 You wish to sort them by 2, but keep equal elements ordered by 1 Solution: just use a stable sorting 3 / 6
15 Stable sorting I What is stable sorting? A sorting algorithm is stable if it preserves the order of equal elements Why is it useful? Assume elements are already ordered by 1 You wish to sort them by 2, but keep equal elements ordered by 1 Solution: just use a stable sorting Which sorting algorithms are stable? 3 / 6
16 Stable sorting I What is stable sorting? A sorting algorithm is stable if it preserves the order of equal elements Why is it useful? Assume elements are already ordered by 1 You wish to sort them by 2, but keep equal elements ordered by 1 Solution: just use a stable sorting Which sorting algorithms are stable? Insertion sort: 3 / 6
17 Stable sorting I What is stable sorting? A sorting algorithm is stable if it preserves the order of equal elements Why is it useful? Assume elements are already ordered by 1 You wish to sort them by 2, but keep equal elements ordered by 1 Solution: just use a stable sorting Which sorting algorithms are stable? Insertion sort: stable, as it never swaps A[i] and A[i + 1] if they are equal 3 / 6
18 Stable sorting I What is stable sorting? A sorting algorithm is stable if it preserves the order of equal elements Why is it useful? Assume elements are already ordered by 1 You wish to sort them by 2, but keep equal elements ordered by 1 Solution: just use a stable sorting Which sorting algorithms are stable? Insertion sort: stable, as it never swaps A[i] and A[i + 1] if they are equal Mergesort: 3 / 6
19 Stable sorting I What is stable sorting? A sorting algorithm is stable if it preserves the order of equal elements Why is it useful? Assume elements are already ordered by 1 You wish to sort them by 2, but keep equal elements ordered by 1 Solution: just use a stable sorting Which sorting algorithms are stable? Insertion sort: stable, as it never swaps A[i] and A[i + 1] if they are equal Mergesort: stable, as merge always picks the left element from two equal ones 3 / 6
20 Stable sorting I What is stable sorting? A sorting algorithm is stable if it preserves the order of equal elements Why is it useful? Assume elements are already ordered by 1 You wish to sort them by 2, but keep equal elements ordered by 1 Solution: just use a stable sorting Which sorting algorithms are stable? Insertion sort: stable, as it never swaps A[i] and A[i + 1] if they are equal Mergesort: stable, as merge always picks the left element from two equal ones Quicksort: 3 / 6
21 Stable sorting I What is stable sorting? A sorting algorithm is stable if it preserves the order of equal elements Why is it useful? Assume elements are already ordered by 1 You wish to sort them by 2, but keep equal elements ordered by 1 Solution: just use a stable sorting Which sorting algorithms are stable? Insertion sort: stable, as it never swaps A[i] and A[i + 1] if they are equal Mergesort: stable, as merge always picks the left element from two equal ones Quicksort: not stable. 3 / 6
22 Stable sorting I What is stable sorting? A sorting algorithm is stable if it preserves the order of equal elements Why is it useful? Assume elements are already ordered by 1 You wish to sort them by 2, but keep equal elements ordered by 1 Solution: just use a stable sorting Which sorting algorithms are stable? Insertion sort: stable, as it never swaps A[i] and A[i + 1] if they are equal Mergesort: stable, as merge always picks the left element from two equal ones Quicksort: not stable. In an array of two equal items, it will swap them 3 / 6
23 Stable sorting II How to make every sorting stable? 4 / 6
24 Stable sorting II How to make every sorting stable? Keep original indices with the elements 4 / 6
25 Stable sorting II How to make every sorting stable? Keep original indices with the elements Before: After: 5; 1 2; 2 4; 3 4; 4 5; 5 2; 6 4 / 6
26 Stable sorting II How to make every sorting stable? Keep original indices with the elements Before: After: 5; 1 2; 2 4; 3 4; 4 5; 5 2; 6 In the sorting, use a modified ordering : Given x; a and y; b If x y, return x y If x = y, return a b 4 / 6
27 Stable sorting II How to make every sorting stable? Keep original indices with the elements Before: After: 5; 1 2; 2 4; 3 4; 4 5; 5 2; 6 In the sorting, use a modified ordering : Given x; a and y; b If x y, return x y original ordering here If x = y, return a b 4 / 6
28 Stable sorting II How to make every sorting stable? Keep original indices with the elements Before: After: 5; 1 2; 2 4; 3 4; 4 5; 5 2; 6 In the sorting, use a modified ordering : Given x; a and y; b If x y, return x y original ordering here If x = y, return a b Pros: Can use any sorting, don t need to care of implementation 4 / 6
29 Stable sorting II How to make every sorting stable? Keep original indices with the elements Before: After: 5; 1 2; 2 4; 3 4; 4 5; 5 2; 6 In the sorting, use a modified ordering : Given x; a and y; b If x y, return x y original ordering here If x = y, return a b Pros: Can use any sorting, don t need to care of implementation Cons: Additional memory needed for storing the indices More complicated ordering, may further decrease performance 4 / 6
30 Comparators A comparator (as in java.util.comparator<t>) is a custom ordering for sorting certain objects for specific needs 5 / 6
31 Comparators A comparator (as in java.util.comparator<t>) is a custom ordering for sorting certain objects for specific needs Requirements for a comparator: Total (every two elements should be reported as either <, =, or > ) If it reports a = b, then a and b must be equal (in a problem-dependent sense) More specifically, if a = b, then b = a If a < b, then it should be that b > a (and vice versa) 5 / 6
32 Comparators A comparator (as in java.util.comparator<t>) is a custom ordering for sorting certain objects for specific needs Requirements for a comparator: Total (every two elements should be reported as either <, =, or > ) If it reports a = b, then a and b must be equal (in a problem-dependent sense) More specifically, if a = b, then b = a If a < b, then it should be that b > a (and vice versa) Implementation of a comparator: Java-way: return 0 if equal, negative if less, positive if greater C++-way: return true if less, false otherwise 5 / 6
33 A non-trivial comparator (0; 3) Sort the points counterclockwise around the red dot ( 2; 2) (1; 2) ( 1; 1) (0; 0) (2; 0) ( 2; 2) (1; 2) 6 / 6
34 A non-trivial comparator ( 2; 2) (0; 3) (1; 2) Sort the points counterclockwise around the red dot Need to break a circle: start at Ox ( 1; 1) (0; 0) (2; 0) ( 2; 2) (1; 2) 6 / 6
35 A non-trivial comparator ( 2; 2) (0; 3) (1; 2) Sort the points counterclockwise around the red dot Need to break a circle: start at Ox Idea: compare using oriented triangle area ( 1; 1) (0; 0) (2; 0) ( 2; 2) (1; 2) 6 / 6
36 A non-trivial comparator ( 2; 2) (0; 3) (1; 2) Sort the points counterclockwise around the red dot Need to break a circle: start at Ox Idea: compare using oriented triangle area ( 1; 1) (0; 0) (2; 0) ( 2; 2) (1; 2) function LessThan((x 1, y 1 ), (x 2, y 2 )) return x 1 y 2 x 2 y 1 > 0 end function 6 / 6
37 A non-trivial comparator (0; 3) ( 2; 2) (1; 2) ( 1; 1) Sort the points counterclockwise around the red dot Need to break a circle: start at Ox Idea: compare using oriented triangle area Wait, what happens there? (0; 0) (2; 0) ( 2; 2) (1; 2) function LessThan((x 1, y 1 ), (x 2, y 2 )) return x 1 y 2 x 2 y 1 > 0 end function 6 / 6
38 A non-trivial comparator (0; 3) ( 2; 2) (1; 2) ( 1; 1) Sort the points counterclockwise around the red dot Need to break a circle: start at Ox Idea: compare using oriented triangle area... but first check the halfplanes ( 2; 2) (0; 0) (2; 0) (1; 2) function LessThan((x 1, y 1 ), (x 2, y 2 )) h 1 0, h 2 0 if y 1 < 0 or (y 1 = 0 and x 1 < 0) then h 1 1 end if if y 2 < 0 or (y 2 = 0 and x 2 < 0) then h 2 1 end if if h 1 h 2 then return h 1 < h 2 end if return x 1 y 2 x 2 y 1 > 0 end function 6 / 6
39 A non-trivial comparator (0; 3) ( 2; 2) (1; 2) ( 1; 1) Sort the points counterclockwise around the red dot Need to break a circle: start at Ox Idea: compare using oriented triangle area... but first check the halfplanes Okay, but what happens for colinear points? ( 2; 2) (0; 0) (2; 0) (1; 2) function LessThan((x 1, y 1 ), (x 2, y 2 )) h 1 0, h 2 0 if y 1 < 0 or (y 1 = 0 and x 1 < 0) then h 1 1 end if if y 2 < 0 or (y 2 = 0 and x 2 < 0) then h 2 1 end if if h 1 h 2 then return h 1 < h 2 end if return x 1 y 2 x 2 y 1 > 0 end function 6 / 6
40 A non-trivial comparator (0; 3) ( 2; 2) (1; 2) ( 1; 1) Sort the points counterclockwise around the red dot Need to break a circle: start at Ox Idea: compare using oriented triangle area... but first check the halfplanes... and, from colinear points, favor closer ones. ( 2; 2) (0; 0) (2; 0) (1; 2) function LessThan((x 1, y 1 ), (x 2, y 2 )) h 1 0, h 2 0 if y 1 < 0 or (y 1 = 0 and x 1 < 0) then h 1 1 end if if y 2 < 0 or (y 2 = 0 and x 2 < 0) then h 2 1 end if if h 1 h 2 then return h 1 < h 2 end if z x 1 y 2 x 2 y 1 if z = 0 then return x1 2 + y 1 2 < x y 2 2 end if return z > 0 end function 6 / 6
How fast can we sort? Sorting. Decision-tree example. Decision-tree example. CS 3343 Fall by Charles E. Leiserson; small changes by Carola
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
More informationComparison Based Sorting Algorithms. Algorithms and Data Structures: Lower Bounds for Sorting. Comparison Based Sorting Algorithms
Comparison Based Sorting Algorithms Algorithms and Data Structures: Lower Bounds for Sorting Definition 1 A sorting algorithm is comparison based if comparisons A[i] < A[j], A[i] A[j], A[i] = A[j], A[i]
More informationAlgorithms and Data Structures: Lower Bounds for Sorting. ADS: lect 7 slide 1
Algorithms and Data Structures: Lower Bounds for Sorting ADS: lect 7 slide 1 ADS: lect 7 slide 2 Comparison Based Sorting Algorithms Definition 1 A sorting algorithm is comparison based if comparisons
More informationLower bound for comparison-based sorting
COMP3600/6466 Algorithms 2018 Lecture 8 1 Lower bound for comparison-based sorting Reading: Cormen et al, Section 8.1 and 8.2 Different sorting algorithms may have different running-time complexities.
More informationIntroduction to Algorithms
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 5 Prof. Erik Demaine How fast can we sort? All the sorting algorithms we have seen so far are comparison sorts: only use comparisons to determine
More informationDesign and Analysis of Algorithms
Design and Analysis of Algorithms CSE 5311 Lecture 8 Sorting in Linear Time Junzhou Huang, Ph.D. Department of Computer Science and Engineering CSE5311 Design and Analysis of Algorithms 1 Sorting So Far
More informationLecture: Analysis of Algorithms (CS )
Lecture: Analysis of Algorithms (CS583-002) Amarda Shehu Fall 2017 Amarda Shehu Lecture: Analysis of Algorithms (CS583-002) Sorting in O(n lg n) Time: Heapsort 1 Outline of Today s Class Sorting in O(n
More informationIntroduction to Algorithms
Introduction to Algorithms 6.046J/18.401J Lecture 6 Prof. Piotr Indyk Today: sorting Show that Θ (n lg n) is the best possible running time for a sorting algorithm. Design an algorithm that sorts in O(n)
More informationCS61BL. Lecture 5: Graphs Sorting
CS61BL Lecture 5: Graphs Sorting Graphs Graphs Edge Vertex Graphs (Undirected) Graphs (Directed) Graphs (Multigraph) Graphs (Acyclic) Graphs (Cyclic) Graphs (Connected) Graphs (Disconnected) Graphs (Unweighted)
More informationHow many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.
Chapter 8. Sorting in Linear Time Types of Sort Algorithms The only operation that may be used to gain order information about a sequence is comparison of pairs of elements. Quick Sort -- comparison-based
More information/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17
601.433/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17 5.1 Introduction You should all know a few ways of sorting in O(n log n)
More informationLower Bound on Comparison-based Sorting
Lower Bound on Comparison-based Sorting Different sorting algorithms may have different time complexity, how to know whether the running time of an algorithm is best possible? We know of several sorting
More informationProblem. 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.
Problem 5. Sorting Simple Sorting, Quicksort, Mergesort 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. 98 99 Selection Sort
More informationChapter 8 Sorting in Linear Time
Chapter 8 Sorting in Linear Time The slides for this course are based on the course textbook: Cormen, Leiserson, Rivest, and Stein, Introduction to Algorithms, 3rd edition, The MIT Press, McGraw-Hill,
More information08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58
08 A: Sorting III CS1102S: Data Structures and Algorithms Martin Henz March 10, 2010 Generated on Tuesday 9 th March, 2010, 09:58 CS1102S: Data Structures and Algorithms 08 A: Sorting III 1 1 Recap: Sorting
More informationSorting. CMPS 2200 Fall Carola Wenk Slides courtesy of Charles Leiserson with small changes by Carola Wenk
CMPS 2200 Fall 2014 Sorting Carola Wenk Slides courtesy of Charles Leiserson with small changes by Carola Wenk 11/11/14 CMPS 2200 Intro. to Algorithms 1 How fast can we sort? All the sorting algorithms
More informationIntroduction to Algorithms 6.046J/18.401J
Introduction to Algorithms 6.046J/18.401J Menu for Today Show that Θ (n lg n) is the best possible running time for a sorting algorithm. Design an algorithm that sorts in linear time. Hint: maybe the models
More informationHow to Win Coding Competitions: Secrets of Champions. Week 2: Computational complexity. Linear data structures Lecture 4: List
How to Win Coding Competitions: Secrets of Champions Week 2: Computational complexity. Linear data structures Lecture 4: List Pavel Krotkov Saint Petersburg 2016 Operations on list Let s define operations
More informationCS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics
CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics 1 Sorting 1.1 Problem Statement You are given a sequence of n numbers < a 1, a 2,..., a n >. You need to
More informationModule 3: Sorting and Randomized Algorithms
Module 3: Sorting and Randomized Algorithms CS 240 - Data Structures and Data Management Sajed Haque Veronika Irvine Taylor Smith Based on lecture notes by many previous cs240 instructors David R. Cheriton
More informationModule 3: Sorting and Randomized Algorithms. Selection vs. Sorting. Crucial Subroutines. CS Data Structures and Data Management
Module 3: Sorting and Randomized Algorithms CS 240 - Data Structures and Data Management Sajed Haque Veronika Irvine Taylor Smith Based on lecture notes by many previous cs240 instructors David R. Cheriton
More informationSorting. Dr. Baldassano Yu s Elite Education
Sorting Dr. Baldassano Yu s Elite Education Last week recap Algorithm: procedure for computing something Data structure: system for keeping track for information optimized for certain actions Good algorithms
More informationCSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014
CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting Aaron Bauer Winter 2014 The main problem, stated carefully For now, assume we have n comparable elements in an array and we want
More informationScribe: Sam Keller (2015), Seth Hildick-Smith (2016), G. Valiant (2017) Date: January 25, 2017
CS 6, Lecture 5 Quicksort Scribe: Sam Keller (05), Seth Hildick-Smith (06), G. Valiant (07) Date: January 5, 07 Introduction Today we ll study another sorting algorithm. Quicksort was invented in 959 by
More informationQuestion 7.11 Show how heapsort processes the input:
Question 7.11 Show how heapsort processes the input: 142, 543, 123, 65, 453, 879, 572, 434, 111, 242, 811, 102. Solution. Step 1 Build the heap. 1.1 Place all the data into a complete binary tree in the
More informationUniversity of Waterloo CS240, Winter 2010 Assignment 2
University of Waterloo CS240, Winter 2010 Assignment 2 Due Date: Wednesday, February 10, at 5:00pm Please read http://www.student.cs.uwaterloo.ca/~cs240/w10/guidelines.pdf for guidelines on submission.
More informationSorting Shabsi Walfish NYU - Fundamental Algorithms Summer 2006
Sorting The Sorting Problem Input is a sequence of n items (a 1, a 2,, a n ) The mapping we want is determined by a comparison operation, denoted by Output is a sequence (b 1, b 2,, b n ) such that: {
More informationModule 3: Sorting and Randomized Algorithms
Module 3: Sorting and Randomized Algorithms CS 240 - Data Structures and Data Management Reza Dorrigiv, Daniel Roche School of Computer Science, University of Waterloo Winter 2010 Reza Dorrigiv, Daniel
More informationChapter 8 Sort in Linear Time
Chapter 8 Sort in Linear Time We have so far discussed several sorting algorithms that sort a list of n numbers in O(nlog n) time. Both the space hungry merge sort and the structurely interesting heapsort
More information07 B: Sorting II. CS1102S: Data Structures and Algorithms. Martin Henz. March 5, Generated on Friday 5 th March, 2010, 08:31
Recap: Sorting 07 B: Sorting II CS1102S: Data Structures and Algorithms Martin Henz March 5, 2010 Generated on Friday 5 th March, 2010, 08:31 CS1102S: Data Structures and Algorithms 07 B: Sorting II 1
More informationDivide and Conquer Sorting Algorithms and Noncomparison-based
Divide and Conquer Sorting Algorithms and Noncomparison-based Sorting Algorithms COMP1927 16x1 Sedgewick Chapters 7 and 8 Sedgewick Chapter 6.10, Chapter 10 DIVIDE AND CONQUER SORTING ALGORITHMS Step 1
More informationEECS 2011M: Fundamentals of Data Structures
M: Fundamentals of Data Structures Instructor: Suprakash Datta Office : LAS 3043 Course page: http://www.eecs.yorku.ca/course/2011m Also on Moodle Note: Some slides in this lecture are adopted from James
More informationPractical Session #11 - Sort properties, QuickSort algorithm, Selection
Practical Session #11 - Sort properties, QuickSort algorithm, Selection Quicksort quicksort( A, low, high ) if( high > low ) pivot partition( A, low, high ) // quicksort( A, low, pivot-1 ) quicksort( A,
More informationOutline. Computer Science 331. Three Classical Algorithms. The Sorting Problem. Classical Sorting Algorithms. Mike Jacobson. Description Analysis
Outline Computer Science 331 Classical Sorting Algorithms Mike Jacobson Department of Computer Science University of Calgary Lecture #22 1 Introduction 2 3 4 5 Comparisons Mike Jacobson (University of
More informationProperties of a heap (represented by an array A)
Chapter 6. HeapSort Sorting Problem Input: A sequence of n numbers < a1, a2,..., an > Output: A permutation (reordering) of the input sequence such that ' ' ' < a a a > 1 2... n HeapSort O(n lg n) worst
More information1 (15 points) LexicoSort
CS161 Homework 2 Due: 22 April 2016, 12 noon Submit on Gradescope Handed out: 15 April 2016 Instructions: Please answer the following questions to the best of your ability. If you are asked to show your
More informationPresentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Merge Sort & Quick Sort
Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 Merge Sort & Quick Sort 1 Divide-and-Conquer Divide-and conquer is a general algorithm
More informationComputer Science Spring 2005 Final Examination, May 12, 2005
Computer Science 302 00 Spring 2005 Final Examination, May 2, 2005 Name: No books, notes, or scratch paper. Use pen or pencil, any color. Use the backs of the pages for scratch paper. If you need more
More informationCS/COE 1501 cs.pitt.edu/~bill/1501/ Introduction
CS/COE 1501 cs.pitt.edu/~bill/1501/ Introduction Meta-notes These notes are intended for use by students in CS1501 at the University of Pittsburgh. They are provided free of charge and may not be sold
More informationIntroduction to Algorithms and Data Structures. Lecture 12: Sorting (3) Quick sort, complexity of sort algorithms, and counting sort
Introduction to Algorithms and Data Structures Lecture 12: Sorting (3) Quick sort, complexity of sort algorithms, and counting sort Professor Ryuhei Uehara, School of Information Science, JAIST, Japan.
More information1 Lower bound on runtime of comparison sorts
1 Lower bound on runtime of comparison sorts So far we ve looked at several sorting algorithms insertion sort, merge sort, heapsort, and quicksort. Merge sort and heapsort run in worst-case O(n log n)
More informationComputer Science 302 Spring 2007 Practice Final Examination: Part I
Computer Science 302 Spring 2007 Practice Final Examination: Part I Name: This practice examination is much longer than the real final examination will be. If you can work all the problems here, you will
More informationAlgorithms in Systems Engineering ISE 172. Lecture 12. Dr. Ted Ralphs
Algorithms in Systems Engineering ISE 172 Lecture 12 Dr. Ted Ralphs ISE 172 Lecture 12 1 References for Today s Lecture Required reading Chapter 6 References CLRS Chapter 7 D.E. Knuth, The Art of Computer
More informationLecture 5: Sorting Part A
Lecture 5: Sorting Part A Heapsort Running time O(n lg n), like merge sort Sorts in place (as insertion sort), only constant number of array elements are stored outside the input array at any time Combines
More informationUnit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION
DESIGN AND ANALYSIS OF ALGORITHMS Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION http://milanvachhani.blogspot.in EXAMPLES FROM THE SORTING WORLD Sorting provides a good set of examples for analyzing
More informationLecture 8: Mergesort / Quicksort Steven Skiena
Lecture 8: Mergesort / Quicksort Steven Skiena Department of Computer Science State University of New York Stony Brook, NY 11794 4400 http://www.cs.stonybrook.edu/ skiena Problem of the Day Give an efficient
More informationCS/COE 1501
CS/COE 1501 www.cs.pitt.edu/~nlf4/cs1501/ Introduction Meta-notes These notes are intended for use by students in CS1501 at the University of Pittsburgh. They are provided free of charge and may not be
More informationSorting Algorithms Spring 2019 Mentoring 10: 18 April, Asymptotics Potpourri
CSM 61B Sorting Algorithms Spring 2019 Mentoring 10: 18 April, 2018 1 Asymptotics Potpourri Stability is a property of some sorting algorithms. Stability essentially means that if we have two elements
More informationCSE 373 NOVEMBER 8 TH COMPARISON SORTS
CSE 373 NOVEMBER 8 TH COMPARISON SORTS ASSORTED MINUTIAE Bug in Project 3 files--reuploaded at midnight on Monday Project 2 scores Canvas groups is garbage updated tonight Extra credit P1 done and feedback
More informationSelection (deterministic & randomized): finding the median in linear time
Lecture 4 Selection (deterministic & randomized): finding the median in linear time 4.1 Overview Given an unsorted array, how quickly can one find the median element? Can one do it more quickly than bysorting?
More informationSAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms
SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6 6.0 Introduction Sorting algorithms used in computer science are often classified by: Computational complexity (worst, average and best behavior) of element
More informationThe Limits of Sorting Divide-and-Conquer Comparison Sorts II
The Limits of Sorting Divide-and-Conquer Comparison Sorts II CS 311 Data Structures and Algorithms Lecture Slides Monday, October 12, 2009 Glenn G. Chappell Department of Computer Science University of
More informationCS 561, Lecture 1. Jared Saia University of New Mexico
CS 561, Lecture 1 Jared Saia University of New Mexico Quicksort Based on divide and conquer strategy Worst case is Θ(n 2 ) Expected running time is Θ(n log n) An In-place sorting algorithm Almost always
More informationdata structures and algorithms lecture 6
data structures and algorithms 2017 09 21 lecture 6 overview lower bound counting sort radix sort stacks queues material question we have seen for example insertion sort: worst-case in O(n 2 ) merge sort:
More information7. Sorting I. 7.1 Simple Sorting. Problem. Algorithm: IsSorted(A) 1 i j n. Simple Sorting
Simple Sorting 7. Sorting I 7.1 Simple Sorting Selection Sort, Insertion Sort, Bubblesort [Ottman/Widmayer, Kap. 2.1, Cormen et al, Kap. 2.1, 2.2, Exercise 2.2-2, Problem 2-2 19 197 Problem Algorithm:
More informationSearching, Sorting. part 1
Searching, Sorting part 1 Week 3 Objectives Searching: binary search Comparison-based search: running time bound Sorting: bubble, selection, insertion, merge Sorting: Heapsort Comparison-based sorting
More informationSorting. Task Description. Selection Sort. Should we worry about speed?
Sorting Should we worry about speed? Task Description We have an array of n values in any order We need to have the array sorted in ascending or descending order of values 2 Selection Sort Select the smallest
More informationCPSC 320 Midterm 2 Thursday March 13th, 2014
CPSC 320 Midterm 2 Thursday March 13th, 2014 [12] 1. Answer each question with True or False, and then justify your answer briefly. [2] (a) The Master theorem can be applied to the recurrence relation
More informationSorting race. https://www.toptal.com/developers/sortingalgorithms
Sorting race https://www.toptal.com/developers/sortingalgorithms CS 2230 CS II: Data structures Comparison sorting Brandon Myers University of Iowa Today s Learning Objectives Execute several comparison
More informationOutline. Computer Science 331. Heap Shape. Binary Heaps. Heap Sort. Insertion Deletion. Mike Jacobson. HeapSort Priority Queues.
Outline Computer Science 33 Heap Sort Mike Jacobson Department of Computer Science University of Calgary Lectures #5- Definition Representation 3 5 References Mike Jacobson (University of Calgary) Computer
More informationUnit Outline. Comparing Sorting Algorithms Heapsort Mergesort Quicksort More Comparisons Complexity of Sorting 2 / 33
Unit #4: Sorting CPSC : Basic Algorithms and Data Structures Anthony Estey, Ed Knorr, and Mehrdad Oveisi 0W Unit Outline Comparing Sorting Algorithms Heapsort Mergesort Quicksort More Comparisons Complexity
More informationReading for this lecture (Goodrich and Tamassia):
COMP26120: Algorithms and Imperative Programming Basic sorting algorithms Ian Pratt-Hartmann Room KB2.38: email: ipratt@cs.man.ac.uk 2017 18 Reading for this lecture (Goodrich and Tamassia): Secs. 8.1,
More informationCPSC 311 Lecture Notes. Sorting and Order Statistics (Chapters 6-9)
CPSC 311 Lecture Notes Sorting and Order Statistics (Chapters 6-9) Acknowledgement: These notes are compiled by Nancy Amato at Texas A&M University. Parts of these course notes are based on notes from
More informationLecture 5: Sorting in Linear Time CSCI Algorithms I. Andrew Rosenberg
Lecture 5: Sorting in Linear Time CSCI 700 - Algorithms I Andrew Rosenberg Last Time Recurrence Relations Today Sorting in Linear Time Sorting Sorting Algorithms we ve seen so far Insertion Sort - Θ(n
More informationDivide-and-Conquer CSE 680
Divide-and-Conquer CSE 680 1 Introduction Given an instance x of a problem, the divide-and-conquer method works as follows. function DAQ(x) if x is sufficiently small or simple then solve it directly else
More informationDivide and Conquer Algorithms: Advanced Sorting
Divide and Conquer Algorithms: Advanced Sorting (revisit) Properties of Growth-rate functions(1/3) 1. You can ignore low-order terms in an algorithm's growth-rate function. O(n 3 +4n 2 +3n) it is also
More informationCOSC 311: ALGORITHMS HW1: SORTING
COSC 311: ALGORITHMS HW1: SORTIG Solutions 1) Theoretical predictions. Solution: On randomly ordered data, we expect the following ordering: Heapsort = Mergesort = Quicksort (deterministic or randomized)
More informationIntroduction to Computers & Programming
16.070 Introduction to Computers & Programming Asymptotic analysis: upper/lower bounds, Θ notation Binary, Insertion, and Merge sort Prof. Kristina Lundqvist Dept. of Aero/Astro, MIT Complexity Analysis
More informationCS61B Lectures # Purposes of Sorting. Some Definitions. Classifications. Sorting supports searching Binary search standard example
Announcements: CS6B Lectures #27 28 We ll be runningapreliminarytestingrun for Project #2 on Tuesday night. Today: Sorting algorithms: why? Insertion, Shell s, Heap, Merge sorts Quicksort Selection Distribution
More informationData Structures. Sorting. Haim Kaplan & Uri Zwick December 2013
Data Structures Sorting Haim Kaplan & Uri Zwick December 2013 1 Comparison based sorting key a 1 a 2 a n info Input: An array containing n items Keys belong to a totally ordered domain Two keys can be
More informationSorting. Popular algorithms: Many algorithms for sorting in parallel also exist.
Sorting Popular algorithms: Selection sort* Insertion sort* Bubble sort* Quick sort* Comb-sort Shell-sort Heap sort* Merge sort* Counting-sort Radix-sort Bucket-sort Tim-sort Many algorithms for sorting
More informationCS 161 Summer 2009 Homework #1 Sample Solutions
CS 161 Summer 2009 Homework #1 Sample Solutions Regrade Policy: If you believe an error has been made in the grading of your homework, you may resubmit it for a regrade. If the error consists of more than
More informationCS 372: Computational Geometry Lecture 3 Line Segment Intersection
CS 372: Computational Geometry Lecture 3 Line Segment Intersection Antoine Vigneron King Abdullah University of Science and Technology September 9, 2012 Antoine Vigneron (KAUST) CS 372 Lecture 3 September
More informationCOSC242 Lecture 7 Mergesort and Quicksort
COSC242 Lecture 7 Mergesort and Quicksort We saw last time that the time complexity function for Mergesort is T (n) = n + n log n. It is not hard to see that T (n) = O(n log n). After all, n + n log n
More informationCS 532: 3D Computer Vision 14 th Set of Notes
1 CS 532: 3D Computer Vision 14 th Set of Notes Instructor: Philippos Mordohai Webpage: www.cs.stevens.edu/~mordohai E-mail: Philippos.Mordohai@stevens.edu Office: Lieb 215 Lecture Outline Triangulating
More informationComputer Science & Engineering 423/823 Design and Analysis of Algorithms
Computer Science & Engineering 423/823 Design and Analysis of s Lecture 01 Medians and s (Chapter 9) Stephen Scott (Adapted from Vinodchandran N. Variyam) 1 / 24 Spring 2010 Given an array A of n distinct
More informationCOMP Data Structures
COMP 2140 - Data Structures Shahin Kamali Topic 5 - Sorting University of Manitoba Based on notes by S. Durocher. COMP 2140 - Data Structures 1 / 55 Overview Review: Insertion Sort Merge Sort Quicksort
More informationDIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS
CHAPTER 11 SORTING ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM NANCY M. AMATO AND
More informationarxiv: v1 [cs.ds] 3 Jun 2014
How inefficient can a sort algorithm be? arxiv:1406.1077v1 [cs.ds] 3 Jun 2014 Miguel A. Lerma June 5, 2014 Abstract Here we find large lower bounds for a certain family of algorithms, and prove that such
More informationMA 252: Data Structures and Algorithms Lecture 9. Partha Sarathi Mandal. Dept. of Mathematics, IIT Guwahati
MA 252: Data Structures and Algorithms Lecture 9 http://www.iitg.ernet.in/psm/indexing_ma252/y12/index.html Partha Sarathi Mandal Dept. of Mathematics, IIT Guwahati More about quicksort We are lucky if
More informationHow to Win Coding Competitions: Secrets of Champions. Week 2: Computational complexity. Linear data structures Lecture 5: Stack. Queue.
How to Win Coding Competitions: Secrets of Champions Week 2: Computational complexity. Linear data structures Lecture 5: Stack. Queue. Deque Pavel Krotkov Saint Petersburg 2016 General overview Stack,
More informationCSC Design and Analysis of Algorithms
CSC 8301- Design and Analysis of Algorithms Lecture 6 Divide and Conquer Algorithm Design Technique Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide a problem instance into two
More information17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer
Module 2: Divide and Conquer Divide and Conquer Control Abstraction for Divide &Conquer 1 Recurrence equation for Divide and Conquer: If the size of problem p is n and the sizes of the k sub problems are
More informationCSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING
CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING ASSORTED MINUTIAE HW6 Out Due next Wednesday ASSORTED MINUTIAE HW6 Out Due next Wednesday Only two late days allowed ASSORTED MINUTIAE HW6 Out Due
More informationData Structures and Algorithms Chapter 4
Data Structures and Algorithms Chapter. About sorting algorithms. Heapsort Complete binary trees Heap data structure. Quicksort a popular algorithm very fast on average Previous Chapter Divide and conquer
More informationData Structures and Algorithms
Data Structures and Algorithms Session 24. Earth Day, 2009 Instructor: Bert Huang http://www.cs.columbia.edu/~bert/courses/3137 Announcements Homework 6 due before last class: May 4th Final Review May
More informationCSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer
CSC 8301- Design and Analysis of Algorithms Lecture 6 Divide and Conquer Algorithm Design Technique Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide a problem instance into two
More informationThe divide and conquer strategy has three basic parts. For a given problem of size n,
1 Divide & Conquer One strategy for designing efficient algorithms is the divide and conquer approach, which is also called, more simply, a recursive approach. The analysis of recursive algorithms often
More informationCS61B Lectures #28. Today: Lower bounds on sorting by comparison Distribution counting, radix sorts
CS61B Lectures #28 Today: Lower bounds on sorting by comparison Distribution counting, radix sorts Readings: Today: DS(IJ), Chapter 8; Next topic: Chapter 9. Last modified: Tue Jul 5 19:49:54 2016 CS61B:
More informationJana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides
Jana Kosecka Linear Time Sorting, Median, Order Statistics Many slides here are based on E. Demaine, D. Luebke slides Insertion sort: Easy to code Fast on small inputs (less than ~50 elements) Fast on
More informationAlgorithms Chapter 8 Sorting in Linear Time
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
More informationSorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort
Principles of Imperative Computation V. Adamchik CS 15-1 Lecture Carnegie Mellon University Sorting Sorting Sorting is ordering a list of objects. comparison non-comparison Hoare Knuth Bubble (Shell, Gnome)
More information8. Sorting II. 8.1 Heapsort. Heapsort. [Max-]Heap 6. Heapsort, Quicksort, Mergesort. Binary tree with the following properties Wurzel
Heapsort, Quicksort, Mergesort 8. Sorting II 8.1 Heapsort [Ottman/Widmayer, Kap. 2.3, Cormen et al, Kap. 6] 9 210 Heapsort [Max-]Heap 6 Binary tree with the following properties Wurzel Inspiration from
More informationCSE373: Data Structure & Algorithms Lecture 18: Comparison Sorting. Dan Grossman Fall 2013
CSE373: Data Structure & Algorithms Lecture 18: Comparison Sorting Dan Grossman Fall 2013 Introduction to Sorting Stacks, queues, priority queues, and dictionaries all focused on providing one element
More informationData Structures and Algorithms Week 4
Data Structures and Algorithms Week. About sorting algorithms. Heapsort Complete binary trees Heap data structure. Quicksort a popular algorithm very fast on average Previous Week Divide and conquer Merge
More informationDivide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang
Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang Acknowledgement The set of slides have use materials from the following resources Slides for textbook by Dr. Y.
More informationLine Arrangements. Applications
Computational Geometry Chapter 9 Line Arrangements 1 Line Arrangements Applications On the Agenda 2 1 Complexity of a Line Arrangement Given a set L of n lines in the plane, their arrangement A(L) is the
More informationDivide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Acknowledgement. Outline
Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang Acknowledgement The set of slides have use materials from the following resources Slides for textbook by Dr. Y.
More informationCSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.
CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings. Rules: The exam is closed-book, closed-note, closed calculator, closed electronics. Please stop promptly
More informationSorting Algorithms. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University
Sorting Algorithms CptS 223 Advanced Data Structures Larry Holder School of Electrical Engineering and Computer Science Washington State University 1 QuickSort Divide-and-conquer approach to sorting Like
More information