Merge Sort. Yufei Tao. Department of Computer Science and Engineering Chinese University of Hong Kong

Similar documents
Recursion: The Beginning

CS4311 Design and Analysis of Algorithms. Lecture 1: Getting Started

CS2351 Data Structures. Lecture 1: Getting Started

Recursion: The Beginning

RAM with Randomization and Quick Sort

Binary Search and Worst-Case Analysis

k-selection Yufei Tao Department of Computer Science and Engineering Chinese University of Hong Kong

Dynamic Arrays and Amortized Analysis

We can use a max-heap to sort data.

(Refer Slide Time: 01.26)

Algorithms. Lecture Notes 5

Multidimensional Divide and Conquer 2 Spatial Joins

CSC 273 Data Structures

Programming II (CS300)

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

Binary Search and Worst-Case Analysis

COMP Analysis of Algorithms & Data Structures

Week - 04 Lecture - 01 Merge Sort. (Refer Slide Time: 00:02)

Lecture 15 : Review DRAFT

Faster Sorting Methods

Divide and Conquer Algorithms

Divide and Conquer Algorithms: Advanced Sorting

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015

CSE 373: Data Structures and Algorithms

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

Binary Search to find item in sorted array

Goal of the course: The goal is to learn to design and analyze an algorithm. More specifically, you will learn:

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1

Introduction to Computers and Programming. Today

Reading for this lecture (Goodrich and Tamassia):

The divide and conquer strategy has three basic parts. For a given problem of size n,

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri

Merge Sort Roberto Hibbler Dept. of Computer Science Florida Institute of Technology Melbourne, FL

Divide-and-Conquer Algorithms

Association Rule Mining: FP-Growth

Scientific Computing. Algorithm Analysis

Test 1 Review Questions with Solutions

Copyright 2009, Artur Czumaj 1

CSC Design and Analysis of Algorithms

CSE Winter 2015 Quiz 2 Solutions

1 (15 points) LexicoSort

Divide and Conquer Algorithms

5. DIVIDE AND CONQUER I

Sorting and Searching

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

Scribe: Sam Keller (2015), Seth Hildick-Smith (2016), G. Valiant (2017) Date: January 25, 2017

COMP Data Structures

CSE 373: Data Structures and Algorithms

Chapter 5. Quicksort. Copyright Oliver Serang, 2018 University of Montana Department of Computer Science

Data Structures and Algorithms Running time, Divide and Conquer January 25, 2018

EE/CSCI 451 Spring 2018 Homework 8 Total Points: [10 points] Explain the following terms: EREW PRAM CRCW PRAM. Brent s Theorem.

CSE548, AMS542: Analysis of Algorithms, Fall 2012 Date: October 16. In-Class Midterm. ( 11:35 AM 12:50 PM : 75 Minutes )

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Week 02 Module 06 Lecture - 14 Merge Sort: Analysis

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

Merge Sort Algorithm

CS2351 Data Structures. Lecture 5: Sorting in Linear Time

PRAM Divide and Conquer Algorithms

Hashing. Yufei Tao. Department of Computer Science and Engineering Chinese University of Hong Kong

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 5. The Towers of Hanoi. Divide and Conquer

Lecture 9: Sorting Algorithms

Lecture 2: Divide and Conquer

Subset sum problem and dynamic programming

ESc101 : Fundamental of Computing

Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

Lower bound for comparison-based sorting

15 150: Principles of Functional Programming Sorting Integer Lists

Linked Lists, Stacks, and Queues

Programming II (CS300)

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

(Refer Slide Time: 00:50)

Computer Programming

CS 6463: AT Computational Geometry Spring 2006 Convex Hulls Carola Wenk

Solutions for Algorithm Design Exercises and Tests

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms

MERGE SORT SYSTEM IJIRT Volume 1 Issue 7 ISSN:

UNIT 5C Merge Sort. Course Announcements

Reasoning and writing about algorithms: some tips

Design and Analysis of Algorithms

Sorting. There exist sorting algorithms which have shown to be more efficient in practice.

CS 171: Introduction to Computer Science II. Quicksort

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING

Divide-and-Conquer. Divide-and conquer is a general algorithm design paradigm:

Multiple-choice (35 pt.)

Programming in Haskell Aug-Nov 2015

Cpt S 122 Data Structures. Sorting

CIS 121 Data Structures and Algorithms with Java Spring Code Snippets and Recurrences Monday, January 29/Tuesday, January 30

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

Practical Session #11 - Sort properties, QuickSort algorithm, Selection

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Dynamic Programming I Date: 10/6/16

Lecture Notes: Range Searching with Linear Space

COMP 352 FALL Tutorial 10

EECS 2011M: Fundamentals of Data Structures

Algorithms Lab 3. (a) What is the minimum number of elements in the heap, as a function of h?

4. Sorting and Order-Statistics

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

cs1120: Exam 1 Due: Wednesday, 06 October at 3:30pm (in class)

Dynamic Arrays and Amortized Analysis

Transcription:

Department of Computer Science and Engineering Chinese University of Hong Kong

In this lecture, we will design the merge sort which sorts n elements in O(n log n) time. The algorithm illustrates a technique called divide and conquer, which is the most common and useful form of recursion in computer science.

Recall: The Sorting Problem Problem Input: A set S of n integers is given in an array of length n. The value of n is inside the CPU (i.e., in a register). Goal: Design an algorithm to store S in an array where the elements have been arranged in ascending order.

Example Input: 38 28 88 17 26 41 72 83 69 47 12 68 5 52 35 9 Output: 5 9 12 17 26 28 35 38 41 47 52 68 69 72 83 88

Recall: The idea of recursion is to carry out two steps: 1 [Base] Solve the problem trivially where the input size n is a constant. 2 [Reduce] Argue that if we can solve the same problem with a size smaller than n, we can solve the original problem (with size n).

Base. If n = 1 (i.e., S has a single element), there is nothing to sort. Return directly. Reduce. Otherwise: 1 Recursively sort the first half of the array S (i.e., same problem but with size n/2). 2 Recursively sort the second half of the array. 3 Merge the two halves of the array into the final sorted sequence (details later).

Example Input: 38 28 88 17 26 41 72 83 69 47 12 68 5 52 35 9 First step, sort the first half of the array by recursion. 17 26 28 38 41 72 83 88 69 47 12 68 5 52 35 9 sort recursively

Example Second step, sort the second half of the array by recursion: 17 26 28 38 41 72 83 88 5 9 12 35 47 52 68 69 sort recursively Third step, merge the two halves. 5 9 12 17 26 28 35 38 41 47 52 68 69 72 83 88

Merging We are looking at the following (sub-)problem. There are two arrays denoted as A 1 and A 2 of integers. Each array has (at most) n/2 integers, which have been sorted in ascending order. The goal is to produce an array A with all the integers in A 1 and A 2, sorted in ascending order. The following shows an example of the input: 17 26 28 38 41 72 83 88 5 9 12 35 47 52 68 69 A 1 A 2

Merging At the beginning, set i and j to 1. Repeat the following until i > n/2 or j > n/2: 1 If A 1 [i] (i.e., the i-th integer of A 1 ) is smaller than A 2 [j], append A 1 [i] to A, and increase i by 1. 2 Otherwise, append A 2 [j] to A, and increase j by 1.

Example At the beginning of merging: i j 17 26 28 38 41 72 83 88 5 9 12 35 47 52 68 69 A 1 A 2 A Appending 5 to A: i j 17 26 28 38 41 72 83 88 5 9 12 35 47 52 68 69 A 1 A 2 5 A

Example Appending 9 to A: i j 17 26 28 38 41 72 83 88 5 9 12 35 47 52 68 69 A 1 A 2 5 9 A Appending 12 to A: i j 17 26 28 38 41 72 83 88 5 9 12 35 47 52 68 69 A 1 A 2 5 9 12 A

Example Appending 17 to A: i j 17 26 28 38 41 72 83 88 5 9 12 35 47 52 68 69 A 1 A 2 5 9 12 17 A And so on.

Running Time of Let f (n) denote the worst-case running time of merge sort when executed on an array of size n. From the base of recursion, we have: From the reduce part, we know: f (n) = O(1) f (n) 2f (n/2) + O(n) where the term 2f (n/2) is because the recursion sorts two arrays each of size n/2, and the term O(n) is the time of merging (convince yourself this is true).

Running Time of So it remains to solve the following recurrence: f (n) c 1 f (n) 2f (n/2) + c 2 n where c 1, c 2 are constants (whose values we do not care). Using the expansion method, we have: f (n) 2f (n/2) + c 2 n 2(2f (n/4) + c 2 n/2) + c 2 n = 4f (n/4) + 2c 2 n 4(2f (n/8) + c 2 n/4) + 2c 2 n = 8f (n/8) + 3c 2 n 2 i f (n/2 i ) + i c 2 n (h = log 2 n) 2 h f (1) + h c 2 n n c 1 + c 2 n log 2 n = O(n log n).

Running Time of The previous discussion assumed n to be a power of 2. How do we remove the assumption? Hint: The rounding approach discussed in a previous lecture.

The form of recursion we used in merge sort is also called divide and conquer. The name is fairly intuitive: we divided the input array into two halves, conquered them separately (i.e., sorting them), and derived the overall result. This form of recursion is frequently applied in computer science it can be utilized to solve numerous problems elegantly.

Recall that selection sort performs sorting in O(n 2 ) time. Today, we have significantly improved the running time to O(n log n). Interestingly, this can no longer been improved asymptotically using the so-called comparison-based approach we will prove later in this course that any comparison-based algorithm must incur Ω(n log n) time!