Divide & Conquer Sorting

Slides:



Advertisements
Similar presentations
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Data Structures Lecture 9 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
CSCE 3110 Data Structures & Algorithm Analysis
1 Merge Sort Review of Sorting Merge Sort. 2 Sorting Algorithms Selection Sort uses a priority queue P implemented with an unsorted sequence: –Phase 1:
Quicksort Quicksort     29  9.
Merge Sort1 Part-G1 Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
Merge Sort1 7 2  9 4   2  2 79  4   72  29  94  4.
CSC2100B Quick Sort and Merge Sort Xin 1. Quick Sort Efficient sorting algorithm Example of Divide and Conquer algorithm Two phases ◦ Partition phase.
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
© 2004 Goodrich, Tamassia QuickSort1 Quick-Sort     29  9.
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 4 Some of the sides are exported from different sources.
Quick-Sort     29  9.
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
CS 206 Introduction to Computer Science II 04 / 27 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 12 / 09 / 2009 Instructor: Michael Eckmann.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
CS 206 Introduction to Computer Science II 12 / 05 / 2008 Instructor: Michael Eckmann.
General Computer Science for Engineers CISC 106 James Atlas Computer and Information Sciences 10/23/2009.
CS 206 Introduction to Computer Science II 12 / 03 / 2008 Instructor: Michael Eckmann.
© 2004 Goodrich, Tamassia Merge Sort1 Quick-Sort     29  9.
CS 206 Introduction to Computer Science II 12 / 08 / 2008 Instructor: Michael Eckmann.
CSCE 3110 Data Structures & Algorithm Analysis Sorting (I) Reading: Chap.7, Weiss.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Sorting Fun1 Chapter 4: Sorting     29  9.
Main Index Contents 11 Main Index Contents Building a Ruler: drawRuler() Building a Ruler: drawRuler() Merge Algorithm Example (4 slides) Merge Algorithm.
Randomized Algorithms CSc 4520/6520 Design & Analysis of Algorithms Fall 2013 Slides adopted from Dmitri Kaznachey, George Mason University and Maciej.
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
1 Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
Towers of Hanoi Move n (4) disks from pole A to pole B such that a larger disk is never put on a smaller disk A BC ABC.
Sorting Quick, Merge & Radix Divide-and-conquer Technique subproblem 2 of size n/2 subproblem 1 of size n/2 a solution to subproblem 1 a solution to.
QuickSort by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Merge Sort 1/12/2018 5:48 AM Merge Sort 7 2   7  2  2 7
Advanced Sorting 7 2  9 4   2   4   7
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Merge Sort 1/12/2018 9:44 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Quick-Sort 2/18/2018 3:56 AM Selection Selection.
Divide-and-Conquer 6/30/2018 9:16 AM
Quick Sort and Merge Sort
Quick-Sort 9/12/2018 3:26 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Quick-Sort 9/13/2018 1:15 AM Quick-Sort     2
Chapter 7 Sorting Spring 14
Quick-Sort To understand quick-sort, let’s look at a high-level description of the algorithm 1) Divide : If the sequence S has 2 or more elements, select.
Selection Selection 1 Quick-Sort Quick-Sort 10/30/16 13:52
Algorithm Design Methods
Radish-Sort 11/11/ :01 AM Quick-Sort     2 9  9
Objectives Introduce different known sorting algorithms
Quick Sort (11.2) CSE 2011 Winter November 2018.
Divide-and-Conquer 7 2  9 4   2   4   7
Merge Sort 2/23/ :15 PM Merge Sort 7 2   7  2   4  4 9
EE 312 Software Design and Implementation I
Merge Sort 2/24/2019 6:07 PM Merge Sort 7 2   7  2  2 7
Quick-Sort 2/25/2019 2:22 AM Quick-Sort     2
Copyright © Aiman Hanna All rights reserved
Copyright © Aiman Hanna All rights reserved
Quick-Sort 4/8/ :20 AM Quick-Sort     2 9  9
Divide-and-Conquer 7 2  9 4   2   4   7
Merge Sort 4/10/ :25 AM Merge Sort 7 2   7  2   4  4 9
Quick-Sort 4/25/2019 8:10 AM Quick-Sort     2
Quick-Sort 5/7/2019 6:43 PM Selection Selection.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Quick-Sort 5/25/2019 6:16 PM Selection Selection.
Divide-and-Conquer 7 2  9 4   2   4   7
Merge Sort 5/30/2019 7:52 AM Merge Sort 7 2   7  2  2 7
CSCE 3110 Data Structures & Algorithm Analysis
CS203 Lecture 15.
Divide-and-Conquer 7 2  9 4   2   4   7
Chapter 11 Sets, and Selection
Presentation transcript:

Divide & Conquer Sorting Merge Sort Divide & Conquer Sorting 7 2  9 4  2 4 7 9 7  2  2 7 9  4  4 9 7  7 2  2 9  9 4  4 Divide and Conquer Sorting

Divide and Conquer Sorting Divide-and conquer is a general algorithm design paradigm: Divide: divide the input data S in two disjoint subsets S1 and S2. Continue division until problem is ‘tractable’. Compute: solve the subproblems associated with S1 and S2 Conquer: combine the solutions for S1 and S2 into a solution for S Mergesort and Quicksort are two examples. Divide and Conquer Sorting

Divide and Conquer Sorting Merge-Sort Merge-sort on an input sequence S with n elements consists of three steps: Divide: partition S into two sequences S1 and S2 of about n/2 elements each, until only 1 element remains in each S1 and S2 Compute: sort S1 and S2 Conquer: merge S1 and S2 into a unique sorted sequence Divide and Conquer Sorting

Merging Two Sorted Sequences The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing all of the elements of A and B Merging two sorted sequences, each with n/2 elements takes O(n) time. Why? Each element must be ‘scanned’ before being merged! Divide and Conquer Sorting

Divide and Conquer Sorting Execution Example Partition 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 7 2 9 4  2 4 7 9 3 8 6 1  1 3 8 6 7 2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Divide and Conquer Sorting

Execution Example (cont.) Continue partition until problem size is 1 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 7 2  9 4  2 4 7 9 3 8 6 1  1 3 8 6 7 2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Divide and Conquer Sorting

Execution Example (cont.) Continue partition until problem size is 1 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 7 2  9 4  2 4 7 9 3 8 6 1  1 3 8 6 7  2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Divide and Conquer Sorting

Execution Example (cont.) ‘Base’ case of 1! 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 7 2  9 4  2 4 7 9 3 8 6 1  1 3 8 6 7  2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Divide and Conquer Sorting

Execution Example (cont.) Here, too, base case of 1! 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 7 2  9 4  2 4 7 9 3 8 6 1  1 3 8 6 7  2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Divide and Conquer Sorting

Execution Example (cont.) Merge 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 7 2  9 4  2 4 7 9 3 8 6 1  1 3 8 6 7  2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Divide and Conquer Sorting

Execution Example (cont.) Recursive call, …, base case, merge 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 7 2  9 4  2 4 7 9 3 8 6 1  1 3 8 6 7  2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Divide and Conquer Sorting

Divide and Conquer Sorting Now you do the right hand side Divide and Conquer Sorting

Execution Example (cont.) Merge 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 7 2  9 4  2 4 7 9 3 8 6 1  1 3 8 6 7  2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Divide and Conquer Sorting

Execution Example (cont.) Recursive call, …, merge, merge 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 7 2  9 4  2 4 7 9 3 8 6 1  1 3 6 8 7  2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Divide and Conquer Sorting

Execution Example (cont.) Merge 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 7 2  9 4  2 4 7 9 3 8 6 1  1 3 6 8 7  2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Divide and Conquer Sorting

Analysis of Merge-Sort The “height” h of the merge-sort tree is O(log n) This is a consequence of the dividing the problem in 2 at each step, The overall amount of work done at each merge i is O(n) Thus, the total running time of merge-sort is O(n log n) size n … Divide and Conquer Sorting

Running times in practice Home PC executes 108 comparisons/sec Supercomputer does 1012 comps/sec. Insertion Sort (n2) Mergesort (n log n) How do you think they got these numbers Good algorithms are better than a supercomputer! Computer thousand million billion home instant 2.8 hours 317 years super 1 second 1.6 weeks thousand million billion instant 1 sec 18 mins Divide and Conquer Sorting

Introducing Quicksort Mergesort Sorts “at the end” Partition is left/right i.e. merge is sorting step Quicksort Sorts “at beginning” Partition is < or > less than or greater than i.e. partition is sorting step! Divide and Conquer Sorting

Divide and Conquer Sorting Quick-Sort Quick-sort is a sorting algorithm based on the divide-and-conquer: Divide: pick a random element x (called pivot) and partition S into L elements less than x E elements equal x G elements greater than x Re-apply: above on L and G Conquer: join L, E and G x x L G E x Divide and Conquer Sorting

Divide and Conquer Sorting Execution Example Pivot selection 7 2 9 4 3 7 6 1  1 2 3 4 6 7 8 9 < 6 > 6 7 2 9 4  2 4 7 9 3 8 6 1  1 3 8 6 2  2 9 4  4 9 3  3 8  8 9  9 4  4 Divide and Conquer Sorting

Execution Example (cont.) Partition, repeat pivot selection. 7 2 9 4 3 7 6 1  1 2 3 4 6 7 8 9 2 4 3 1  2 4 7 9 3 8 6 1  1 3 8 6 < 2 >2 2  2 9 4  4 9 3  3 8  8 9  9 4  4 Divide and Conquer Sorting

Execution Example (cont.) Partition, select pivot, base case 7 2 9 4 3 7 6 1   1 2 3 4 6 7 8 9 2 4 3 1  2 4 7 3 8 6 1  1 3 8 6 1  1 9 4  4 9 3  3 8  8 9  9 4  4 Divide and Conquer Sorting

Execution Example (cont.) partition, select pivot, smallest case, join 7 2 9 4 3 7 6 1  1 2 3 4 6 7 8 9 2 4 3 1  1 2 3 4 3 8 6 1  1 3 8 6 1  1 4 3  3 4 3  3 8  8 < 3 > 3 9  9 4  4 Divide and Conquer Sorting

Divide and Conquer Sorting Now you do the right hand side Divide and Conquer Sorting

Execution Example (cont.) Partition, pivot selection 7 2 9 4 3 7 6 1  1 2 3 4 6 7 8 9 2 4 3 1  1 2 3 4 7 9 7 1  1 3 8 6 1  1 4 3  3 4 8  8 9  9 9  9 4  4 Divide and Conquer Sorting

Execution Example (cont.) Partition, …, select pivot, smallest case 7 2 9 4 3 7 6 1  1 2 3 4 6 7 8 9 2 4 3 1  1 2 3 4 7 9 7 1  1 3 8 6 < 7 > 7 1  1 4 3  3 4 8  8 9  9 9  9 4  4 Divide and Conquer Sorting

Execution Example (cont.) Join, join 7 2 9 4 3 7 6 1  1 2 3 4 6 7 7 9 2 4 3 1  1 2 3 4 7 9 7  17 7 9 1  1 4 3  3 4 8  8 9  9 9  9 4  4 Divide and Conquer Sorting

‘Flat’ ways to view Quicksort http://youtu.be/y_G9BkAm6B8 https://www.youtube.com/watch?v=kPRA0W1kECg (sound) http://people.stern.nyu.edu/panos/java/Quicksort/ Divide and Conquer Sorting

Worst-case Running Time Divide and Conquer Sorting

Worst-case Running Time The worst case for quick-sort occurs when the pivot is the unique minimum or maximum element One of L and G has size n - 1 and the other has size 0 The running time is proportional to the sum n + (n - 1) + … + 2 + 1 Thus, the worst-case running time of quick-sort is O(n2) Divide and Conquer Sorting

Worst-case Running Time Merge Sort Worst-case Running Time The worst case for quick-sort occurs when the pivot is the unique minimum or maximum element One of L and G has size n - 1 and the other has size 0 The running time is proportional to the sum n + (n - 1) + … + 2 + 1 Thus, the worst-case running time of quick-sort is O(n2) depth time n 1 n - 1 … … Divide and Conquer Sorting

Divide and Conquer Sorting Expected Running Time Consider a call of quick-sort on a sequence of size s Good case: the sizes of L and G are roughly the same Bad case: the sizes of L and G differ greatly (not well-divided) A case is good with probability 1/2 1/2 of the possible pivots cause good calls: 7 2 9 4 3 7 6 1 9 7 2 9 4 3 7 6 1 2 4 3 1 7 9 7 1  1 1 7 2 9 4 3 7 6 Good case Bad case 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Bad pivots Good pivots Bad pivots Divide and Conquer Sorting

Running times in practice Home PC executes 108 comparisons/sec Supercomputer does 1012 comps/sec. Insertion Sort (N2) Mergesort (N log N) Quicksort (N log N) Great algorithms better than good ones! Computer thousand million billion home instant 2.8 hours 317 years super 1 second 1.6 weeks thousand million billion instant 1 sec 18 mins thousand million billion instant 0.3 sec 6 mins Divide and Conquer Sorting