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

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

Elementary Sorts 1 / 18

Lecture 14: Nov. 11 & 13

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

Week 5: Quicksort, Lower bound, Greedy

Data Structures and Algorithms

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

Data selection. Lower complexity bound for sorting

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

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

Mergesort and Recurrences (CLRS 2.3, 4.4)

Quicksort. Recurrence analysis Quicksort introduction. November 17, 2017 Hassan Khosravi / Geoffrey Tien 1

SORTING ALGORITHMS BBM ALGORITHMS DEPT. OF COMPUTER ENGINEERING ERKUT ERDEM. Mar. 21, 2013

Find an Element x in an Unsorted Array

Algorithms. Jordi Planes. Escola Politècnica Superior Universitat de Lleida

Problem. Problem Given a dictionary and a word. Which page (if any) contains the given word? 3 / 26

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

Introduction to Divide and Conquer

Sorting Algorithms. We have already seen: Selection-sort Insertion-sort Heap-sort. We will see: Bubble-sort Merge-sort Quick-sort

Fast Sorting and Selection. A Lower Bound for Worst Case

Randomized Sorting Algorithms Quick sort can be converted to a randomized algorithm by picking the pivot element randomly. In this case we can show th

Sorting Algorithms. !rules of the game!shellsort!mergesort!quicksort!animations. Classic sorting algorithms

MERGESORT BBM ALGORITHMS DEPT. OF COMPUTER ENGINEERING ERKUT ERDEM. Mergesort. Feb. 27, 2014

Sorting and Searching. Tony Wong

Binary Search Trees. Motivation

data structures and algorithms lecture 2

CSCE 750, Spring 2001 Notes 2 Page 1 4 Chapter 4 Sorting ffl Reasons for studying sorting is a big deal pedagogically useful Λ the application itself

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

CS361 Homework #3 Solutions

Design and Analysis of Algorithms

Space Complexity of Algorithms

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

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

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018

Review Of Topics. Review: Induction

Fall 2017 November 10, Written Homework 5

Data Structures and Algorithms " Search Trees!!

CS325: Analysis of Algorithms, Fall Midterm

CMSC 132, Object-Oriented Programming II Summer Lecture 6:

Partition and Select

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

Tricks of the Trade in Combinatorics and Arithmetic

Data Structures and Algorithms

Algorithms and Data Structures 2016 Week 5 solutions (Tues 9th - Fri 12th February)

Advanced Implementations of Tables: Balanced Search Trees and Hashing

Computational Complexity

Topic 17. Analysis of Algorithms

CS 161 Summer 2009 Homework #2 Sample Solutions

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

Searching (within) Data. Programming and Problem Solving Searching (within) Data, Advanced Complexity Theory. Searching (within) Data.

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

Advanced Analysis of Algorithms - Homework I (Solutions)

Analysis of Algorithms

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Algorithms Test 1. Question 1. (10 points) for (i = 1; i <= n; i++) { j = 1; while (j < n) {

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

5. DIVIDE AND CONQUER I

Algorithm Design and Analysis

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

Data Structures and Algorithms Chapter 3

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

Analysis of Algorithms CMPSC 565

Lecture 5: Jun. 10, 2015

Linear Time Selection

Asymptotic Running Time of Algorithms

Fundamental Algorithms

Fundamental Algorithms

CS360 Homework 12 Solution

Advanced Analysis of Algorithms - Midterm (Solutions)

Algorithms. Quicksort. Slide credit: David Luebke (Virginia)

Lecture 4. Quicksort

CMPT 307 : Divide-and-Conqer (Study Guide) Should be read in conjunction with the text June 2, 2015

Data Structures and Algorithms

Data Structures and Algorithms CSE 465

COMP 382: Reasoning about algorithms

Algorithms And Programming I. Lecture 5 Quicksort

csci 210: Data Structures Program Analysis

Sorting algorithms. Sorting algorithms

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

1 Divide and Conquer (September 3)

ITEC2620 Introduction to Data Structures

Algorithm Analysis Divide and Conquer. Chung-Ang University, Jaesung Lee

COMP Analysis of Algorithms & Data Structures

Linear Selection and Linear Sorting

CS 61B Asymptotic Analysis Fall 2017

COMP Analysis of Algorithms & Data Structures

MIDTERM Fundamental Algorithms, Spring 2008, Professor Yap March 10, 2008

shelat divide&conquer closest points matrixmult median

Divide-and-Conquer Algorithms Part Two

Central Algorithmic Techniques. Iterative Algorithms

AVL Trees. Properties Insertion. October 17, 2017 Cinda Heeren / Geoffrey Tien 1

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

Motivation. Dictionaries. Direct Addressing. CSE 680 Prof. Roger Crawfis

csci 210: Data Structures Program Analysis

4.1, 4.2: Analysis of Algorithms

Lecture 20: Analysis of Algorithms

Introduction to Algorithms

Divide-and-conquer. Curs 2015

Quiz 1 Solutions. Problem 2. Asymptotics & Recurrences [20 points] (3 parts)

10: Analysis of Algorithms

Transcription:

Merge Sort 1 / 10

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

Merging Merge sort is based on a simple operation known as merging: combining two ordered arrays to make one larger ordered array To sort an array, divide it into two halves, sort the two halves (recursively), and then merge the results Merge sort overview input sort left half sort right half merge results M E R G E S O R T E X A M P L E E E G M O R R S T E X A M P L E E E G M O R R S A E E L M P T X A E E E E G L M M O P R R S T X 3 / 10

Merging The merge algorithm package edu. princeton.cs. algs4 ; import java. util. Comparator ; public class Merge { private static void merge ( Comparable [] a, Comparable [] aux, int lo, int mid, int hi) { int i = lo, j = mid + 1; for ( int k = lo; k <= hi; k ++) { aux [k] = a[k]; for ( int k = lo; k <= hi; k ++) { if (i > mid ) { a[k] = aux [j ++]; else if (j > hi) { a[k] = aux [i ++]; else if ( less ( aux [j], aux [i ])) { a[k] = aux [j ++]; else { a[k] = aux [i ++]; 4 / 10

Merging Trace of merge operation input copy k a[] aux[] 0 1 2 3 4 5 6 7 8 9 i j 0 1 2 3 4 5 6 7 8 9 merged result 0 1 2 3 4 5 6 7 8 9 A A C A C E A C E E A C E E E A C E E E G A C E E E G M A C E E E G M R A C E E E G M R R A C E E E G M R R T A C E E E G M R R T 0 5 0 6 0 7 1 7 2 7 2 8 3 8 4 8 5 8 5 9 6 10 5 / 10

Merge Sort Top-down merge sort package edu. princeton.cs. algs4 ; import java. util. Comparator ; public class Merge { public static void sort ( Comparable [] a) { Comparable [] aux = new Comparable [ a. length ]; sort (a, aux, 0, a. length - 1); private static void sort ( Comparable [] a, Comparable [] aux, int lo, int hi) { if (hi <= lo) { return ; int mid = lo + ( hi - lo) / 2; sort (a, aux, lo, mid ); sort (a, aux, mid + 1, hi ); merge (a, aux, lo, mid, hi ); 6 / 10

Merge Sort Trace of merge sort a[] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 merge(a, aux, 0, 0, 1) merge(a, aux, 2, 2, 3) merge(a, aux, 0, 1, 3) merge(a, aux, 4, 4, 5) merge(a, aux, 6, 6, 7) merge(a, aux, 4, 5, 7) merge(a, aux, 0, 3, 7) merge(a, aux, 8, 8, 9) merge(a, aux, 10, 10, 11) merge(a, aux, 8, 9, 11) merge(a, aux, 12, 12, 13) merge(a, aux, 14, 14, 15) merge(a, aux, 12, 13, 15) merge(a, aux, 8, 11, 15) merge(a, aux, 0, 7, 15) M E R G E S O R T E X A M P L E E M R G E S O R T E X A M P L E E M G R E S O R T E X A M P L E E G M R E S O R T E X A M P L E E G M R E S O R T E X A M P L E E G M R E S O R T E X A M P L E E G M R E O R S T E X A M P L E E E G M O R R S T E X A M P L E E E G M O R R S E T X A M P L E E E G M O R R S E T A X M P L E E E G M O R R S A E T X M P L E E E G M O R R S A E T X M P L E E E G M O R R S A E T X M P E L E E G M O R R S A E T X E L M P E E G M O R R S A E E L M P T X A E E E E G L M M O P R R S T X 7 / 10

Merge Sort Top-down merge sort uses N lg N comparisons and N lg N array accesses The prime disadvantage of merge sort is that it uses extra space proportional to N The MergeX variant from the text implements the following improvements Handles tiny subarrays using insertion sort Reduces the running time to be linear for arrays that are already in order by adding a test to skip the call to merge() if a[mid] is less than or equal to a[mid + 1] Eliminates the time taken to copy to the auxiliary array by switching the role of the input and auxiliary array in each recursive call 8 / 10

Complexity of Sorting No comparison-based sorting algorithm can guarantee to sort N items with fewer than lg N! N lg N comparisons Proof: Assume array consists of N distinct values a 1 through a N Worst-case number of comparisons is dictated by height h of decision tree Binary tree of height h has at most 2 h leaves N! different orderings = at least N! leaves N! number of leaves 2 h lg N! h, or using Stirling s approximation, N lg N h a b c yes decision tree (for distinct keys a, b, and c) b < c yes a c b yes no a < c a < b no c a b yes b a c no a < c yes b c a height of tree = worst-case number of compares no b < c each leaf corresponds to one (and only one) ordering: (at least) one leaf for each possible ordering no c b a Merge sort is an asymptotically optimal comparison-based sorting algorithm, since the number of comparisons used by merge sort is N lg N 9 / 10

Merge Sort and Other Sorts A comparison of merge sort and selection sort $ java SortCompare Merge Selection 10000 100 For 10000 random Doubles Merge is 44.8 times faster than Selection A comparison of merge sort and insertion sort $ java SortCompare Merge Insertion 10000 100 For 10000 random Doubles Merge is 39.1 times faster than Insertion A comparison of merge sort and shell sort $ java SortCompare Merge Shell 10000 100 For 10000 random Doubles Merge is 1.1 times faster than Shell A comparison of merge sort and system sort $ java SortCompare Merge System 10000 100 For 10000 random Doubles Merge is 2.0 times faster than System 10 / 10