Outline. 1 Introduction. Merging and MergeSort. 3 Analysis. 4 Reference

Similar documents
Outline. 1 Introduction. 3 Quicksort. 4 Analysis. 5 References. Idea. 1 Choose an element x and reorder the array as follows:

Inf 2B: Sorting, MergeSort and Divide-and-Conquer

CS483 Design and Analysis of Algorithms

Introduction to Divide and Conquer

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

b + O(n d ) where a 1, b > 1, then O(n d log n) if a = b d d ) if a < b d O(n log b a ) if a > b d

Analysis of Algorithms - Using Asymptotic Bounds -

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

Mergesort and Recurrences (CLRS 2.3, 4.4)

Lecture 4. Quicksort

CIS 121. Analysis of Algorithms & Computational Complexity. Slides based on materials provided by Mary Wootters (Stanford University)

CS173 Running Time and Big-O. Tandy Warnow

COMP Analysis of Algorithms & Data Structures

Divide and Conquer CPE 349. Theresa Migler-VonDollen

data structures and algorithms lecture 2

Class Note #14. In this class, we studied an algorithm for integer multiplication, which. 2 ) to θ(n

COMP Analysis of Algorithms & Data Structures

Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort

Divide and Conquer. CSE21 Winter 2017, Day 9 (B00), Day 6 (A00) January 30,

CS483 Design and Analysis of Algorithms

Week 5: Quicksort, Lower bound, Greedy

Divide and Conquer Algorithms

Divide and Conquer Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 14

Divide and Conquer Algorithms

Asymptotic Analysis and Recurrences

Lecture 5: Loop Invariants and Insertion-sort

1 Divide and Conquer (September 3)

Data Structures and Algorithms CSE 465

V. Adamchik 1. Recurrences. Victor Adamchik Fall of 2005

COMP 382: Reasoning about algorithms

Divide and Conquer Algorithms

Data selection. Lower complexity bound for sorting

CMPS 2200 Fall Divide-and-Conquer. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk

Divide and Conquer. Andreas Klappenecker

CPS 616 DIVIDE-AND-CONQUER 6-1

Algorithms And Programming I. Lecture 5 Quicksort

Outline. 1 Merging. 2 Merge Sort. 3 Complexity of Sorting. 4 Merge Sort and Other Sorts 2 / 10

Divide&Conquer: MergeSort. Algorithmic Thinking Luay Nakhleh Department of Computer Science Rice University Spring 2014

Lecture 14: Nov. 11 & 13

Divide and conquer. Philip II of Macedon

CS161: Algorithm Design and Analysis Recitation Section 3 Stanford University Week of 29 January, Problem 3-1.

Notes for Recitation 14

Sorting. Chapter 11. CSE 2011 Prof. J. Elder Last Updated: :11 AM

CPSC 320 (Intermediate Algorithm Design and Analysis). Summer Instructor: Dr. Lior Malka Final Examination, July 24th, 2009

ITEC2620 Introduction to Data Structures

Quicksort. Where Average and Worst Case Differ. S.V. N. (vishy) Vishwanathan. University of California, Santa Cruz

The maximum-subarray problem. Given an array of integers, find a contiguous subarray with the maximum sum. Very naïve algorithm:

COL106: Data Structures and Algorithms (IIT Delhi, Semester-II )

Divide and Conquer: Polynomial Multiplication Version of October 1 / 7, 24201

CSC236 Intro. to the Theory of Computation Lecture 7: Master Theorem; more D&C; correctness

CS 4407 Algorithms Lecture 3: Iterative and Divide and Conquer Algorithms

Midterm Exam. CS 3110: Design and Analysis of Algorithms. June 20, Group 1 Group 2 Group 3

Algorithms. Algorithms 2.2 MERGESORT. mergesort bottom-up mergesort sorting complexity divide-and-conquer ROBERT SEDGEWICK KEVIN WAYNE

CMPSCI611: Three Divide-and-Conquer Examples Lecture 2

Divide-and-Conquer and Recurrence Relations: Notes on Chapter 2, Dasgupta et.al. Howard A. Blair

In-Class Soln 1. CS 361, Lecture 4. Today s Outline. In-Class Soln 2

5. DIVIDE AND CONQUER I

Divide and Conquer. Andreas Klappenecker. [based on slides by Prof. Welch]

Chapter 2. Recurrence Relations. Divide and Conquer. Divide and Conquer Strategy. Another Example: Merge Sort. Merge Sort Example. Merge Sort Example

Algorithm Design and Analysis

Data Structures and Algorithms Chapter 3

Review Of Topics. Review: Induction

Analysis of Algorithms

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Divide-and-conquer: Order Statistics. Curs: Fall 2017

CS 4104 Data and Algorithm Analysis. Recurrence Relations. Modeling Recursive Function Cost. Solving Recurrences. Clifford A. Shaffer.

5. DIVIDE AND CONQUER I

A design paradigm. Divide and conquer: (When) does decomposing a problem into smaller parts help? 09/09/ EECS 3101

CS 5321: Advanced Algorithms - Recurrence. Acknowledgement. Outline. Ali Ebnenasir Department of Computer Science Michigan Technological University

Algorithms, Design and Analysis. Order of growth. Table 2.1. Big-oh. Asymptotic growth rate. Types of formulas for basic operation count

Problem Set 2 Solutions

Data Structures and Algorithms

CS 5321: Advanced Algorithms Analysis Using Recurrence. Acknowledgement. Outline

Fundamental Algorithms

Fundamental Algorithms

Central Algorithmic Techniques. Iterative Algorithms

Data Structures and Algorithms Chapter 3

Grade 11/12 Math Circles Fall Nov. 5 Recurrences, Part 2

Data Structures and Algorithms CSE 465

COMP 355 Advanced Algorithms

CSE 613: Parallel Programming. Lecture 8 ( Analyzing Divide-and-Conquer Algorithms )

Introduction to Algorithms 6.046J/18.401J/SMA5503

Divide-Conquer-Glue. Divide-Conquer-Glue Algorithm Strategy. Skyline Problem as an Example of Divide-Conquer-Glue

Recurrence Relations

Divide and Conquer. Arash Rafiey. 27 October, 2016

Partition and Select

CS 577 Introduction to Algorithms: Strassen s Algorithm and the Master Theorem

Divide and Conquer. Recurrence Relations

COMP 355 Advanced Algorithms Algorithm Design Review: Mathematical Background

COMP 250 Fall Midterm examination

Computational Complexity

Analysis of Algorithms CMPSC 565

Divide-Conquer-Glue Algorithms

Solutions. Problem 1: Suppose a polynomial in n of degree d has the form

COMP 250: Quicksort. Carlos G. Oliver. February 13, Carlos G. Oliver COMP 250: Quicksort February 13, / 21

Multidimensional Divide and Conquer 1 Skylines

Reductions, Recursion and Divide and Conquer

Divide and Conquer Algorithms

Divide and Conquer Strategy

Transcription:

Outline Computer Science 331 Sort Mike Jacobson Department of Computer Science University of Calgary Lecture #25 1 Introduction 2 Merging and 3 4 Reference Mike Jacobson (University of Calgary) Computer Science 331 Lecture #25 1 / 20 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #25 2 / 20 Introduction Merging and Introduction The Merging Problem Sort is is an asymptotically faster algorithm than the sorting algorithms we have seen so far. It can be used to sort an array of size n using Θ(n log 2 n) operations in the worst case. Presented here: A version that takes an input array A and produces another sorted array B (containing the entries of A, rearranged) A solution to the Merging Problem (presented next) is a subroutine that is used to do much of the work. Calling Sequence: void merge(int [] A 1, int [] A 2, int [] B) Precondition: A 1 is a sorted array of length n 1 (positive integer) such that A 1 [h] A 1 [h + 1] for 0 h n 1 2 A 2 is a sorted array of length n 2 (positive integer) such that A 2 [h] A 2 [h + 1] for 0 h n 2 2 Entries of A 1 and A 2 are integers (more generally, objects from the same ordered class) Mike Jacobson (University of Calgary) Computer Science 331 Lecture #25 3 / 20 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #25 4 / 20

Merging and Merging and The Merging Problem (cont.) Idea for an Algorithm Postcondition: B is a sorted array of length n 1 + n 2, so that B[h] B[h + 1] for 0 h n 1 + n 2 2 Entries of B are the entries of A 1 together with the entries of A 2, reordered but otherwise unchanged A 1 and A 2 have not been modified Maintain indices into each array (each initially pointing to the leftmost element) repeat Compare the current elements of each array Append the smaller entry onto the end of B, advancing the index for the array from which this entry was taken until one of the input arrays has been exhausted Append the rest of the other input array onto the end of B Mike Jacobson (University of Calgary) Computer Science 331 Lecture #25 5 / 20 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #25 6 / 20 Merging and Merging and Pseudocode Pseudocode, Continued void merge(int [] A 1, int [] A 2, int [] B) n 1 = length(a 1 ); n 2 = length(a 2 ) Declare B to be an array of length n 1 + n 2 i 1 = 0; i 2 = 0; j = 0 while (i 1 < n 1 ) and (i 2 < n 2 ) do if A 1 [i 1 ] A 2 [i 2 ] then B[j] = A 1 [i 1 ]; i 1 = i 1 + 1 else B[j] = A 2 [i 2 ]; i 2 = i 2 + 1 end if j = j + 1 {Copy remainder of A 1 (if any)} while i 1 < n 1 do B[j] = A 1 [i 1 ]; i 1 = i 1 + 1; j = j + 1 {Otherwise copy remainder of A 2 } while i 2 < n 2 do B[j] = A 2 [i 2 ]; i 2 = i 2 + 1; j = j + 1 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #25 7 / 20 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #25 8 / 20

Merging and Merging and Example Sort: Idea for an Algorithm A 1 A 2 0 1 2 3 4 0 1 2 3 4 1 3 5 6 8 4 5 7 9 10 Suppose we: 1 Split an input array into two roughly equally-sized pieces. 2 Recursively sort each piece. 3 the two sorted pieces. This sorts the originally given array. 1 3 4 5 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 Note: Running time is Θ(n 1 + n 2 ), where the input arrays have size n 1 and n 2 B Mike Jacobson (University of Calgary) Computer Science 331 Lecture #25 9 / 20 Note: this algorithm design strategy is known as divide-and-conquer: divide the original problem (sorting an array) into smaller subproblems (sorting smaller arrays) solve the smaller subproblems recursively combine the solutions to the smaller subproblems (the sorted subarrays) to obtain a solution to the original problem (merging the sorted arrays) Mike Jacobson (University of Calgary) Computer Science 331 Lecture #25 10 / 20 Merging and Merging and Pseudocode Example void mergesort(int [] A, int [] B) n = A.length if n == 1 then B[0] = A[0] else n 1 = n/2 n 2 = n n 1 {so that n 2 = n/2 } Set A 1 to be A[0],..., A[n 1 1] {length n 1 } Set A 2 to be A[n 1 ],..., A[n 1] {length n 2 } mergesort(a 1, B 1 ) mergesort(a 2, B 2 ) merge(b 1, B 2, B) end if 0 1 2 3 4 5 6 7 A : 7 3 9 6 5 2 1 8 1 Sort A[0,..., 3] = [7, 3, 9, 6] recursively: Sort A[0, 1] = [7, 3] recursively Sort A[0] = [7] recursively base case Sort A[1] = [3] recursively base case : result is [3, 7] Sort A[2, 3] = [9, 6] recursively. Result is [6, 9] : result is [3, 6, 7, 9] 2 Sort A[4,..., 7] = [5, 2, 1, 8] recursively. Result is [1, 2, 5, 8] 3 : result is [1, 2, 3, 5, 6, 7, 8, 9] Mike Jacobson (University of Calgary) Computer Science 331 Lecture #25 11 / 20 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #25 12 / 20

Correctness of Correctness, continued Theorem 1 If mergesort is run on an input array A of size n 1, then the algorithm eventually halts, producing the desired sorted array as output. Prove by (strong) induction on n (assuming that merge is correct!): Base Case: n = 1 if n = 1, array consists of one element (array is sorted trivially) algorithm returns B containing a copy of the single element in the array (terminates with correct output) Inductive hypothesis: assume the algorithm is correct for input arrays of size k < n Let A be an array of length n 2. Prove that B is sorted copy of A. A 1 contains first n 1 elements of A A 2 contains remaining n 2 elements of A n 1 = n/2 < n and n 2 = n/2 < n, so inductive hypothesis implies that B 1 is A 1 sorted and B 2 is A 2 sorted merge computes B containing all elements of A sorted (assuming that merge is correct) hence, algorithm is partially correct by induction. Mike Jacobson (University of Calgary) Computer Science 331 Lecture #25 13 / 20 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #25 14 / 20 Termination and Efficiency Termination and Efficiency, continued Let T (n) be the number of steps used by this algorithm when given an input array of length n, in the worst case. We can see the following by inspection of the code: { c 0 if n = 1 T (n) T ( n/2 ) + T ( n/2 ) + c 1 n if n 2 for some constants c 0 > 0 and c 1 > 0. Useful Relation: For any integer n: n/2 + n/2 = n. Recall That: Operators and do not change the asymptotic behavior. T (n) can be rewritten as follows: { c 0 if n = 1 T (n) 2T (n/2) + c 1 n if n 2 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #25 15 / 20 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #25 16 / 20

Termination and Efficiency, continued Termination and Efficiency, concluded Recurrence Substitution for n 2: T (n) 2T (n/2) + c 1 n 2 ( 2T (n/2 2 ) + c 1 n/2 ) + c 1 n = 2 2 T (n/2 2 ) + 2c 1 n 2 k T ( n 2 k ) + kc 1n Termination: n 2 k = 1 = k = log 2 n Theorem 2 T (n) nc 0 + (n log 2 n)c 1. Prove by induction on n Base case (n = 1) Inductive Step (n 2) Conclusion: Worst-case is in: O(n log 2 n) Mike Jacobson (University of Calgary) Computer Science 331 Lecture #25 17 / 20 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #25 18 / 20 Reference Further Observations It can be shown (by consideration of particular inputs) that the worst-case running time of this algorithm is also in Ω(n log 2 n). It is therefore in Θ(n log 2 n). This is preferable to the classical sorting algorithms, for sufficiently large inputs, if worst-case running time is critical. The classical algorithms are faster on sufficiently small inputs because they are simpler. References Further Reading: Introduction to Algorithms, Chapter 2.3 Alternative Approach: A hybrid algorithm: Use the recursive strategy given above when the input size is greater than or equal to some (carefully chosen) threshold value. Switch to a simpler, nonrecursive algorithm (that is faster on small inputs) as soon as the input size drops to below this threshold value. Mike Jacobson (University of Calgary) Computer Science 331 Lecture #25 19 / 20 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #25 20 / 20