Merge Sort. Alexandra Stefan

Similar documents
Homework 1 Solutions MA 522 Fall 2017

Algorithm Efficiency

Lecture 5. Counting Sort / Radix Sort

CSE 2320 Notes 8: Sorting. (Last updated 10/3/18 7:16 PM) Idea: Take an unsorted (sub)array and partition into two subarrays such that.

Sorting 9/15/2009. Sorting Problem. Insertion Sort: Soundness. Insertion Sort. Insertion Sort: Running Time. Insertion Sort: Soundness

Heaps. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015

Chapter 24. Sorting. Objectives. 1. To study and analyze time efficiency of various sorting algorithms

Lower Bounds for Sorting

Sorting in Linear Time. Data Structures and Algorithms Andrei Bulatov

why study sorting? Sorting is a classic subject in computer science. There are three reasons for studying sorting algorithms.

A graphical view of big-o notation. c*g(n) f(n) f(n) = O(g(n))

Data Structures and Algorithms Part 1.4

2. ALGORITHM ANALYSIS

Algorithm Design Techniques. Divide and conquer Problem

Computational Geometry

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Data Structures Week #9. Sorting

CSE 417: Algorithms and Computational Complexity

Priority Queues. Binary Heaps

What are we going to learn? CSC Data Structures Analysis of Algorithms. Overview. Algorithm, and Inputs

CIS 121 Data Structures and Algorithms with Java Spring Stacks, Queues, and Heaps Monday, February 18 / Tuesday, February 19

CIS 121 Data Structures and Algorithms with Java Fall Big-Oh Notation Tuesday, September 5 (Make-up Friday, September 8)

quality/quantity peak time/ratio

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures

Order statistics. Order Statistics. Randomized divide-andconquer. Example. CS Spring 2006

The isoperimetric problem on the hypercube

Array Applications. Sorting. Want to put the contents of an array in order. Selection Sort Bubble Sort Insertion Sort. Quicksort Quickersort

Analysis of Algorithms

Chapter 3 Classification of FFT Processor Algorithms

Lecture Notes 6 Introduction to algorithm analysis CSS 501 Data Structures and Object-Oriented Programming

CIS 121 Data Structures and Algorithms with Java Spring Stacks and Queues Monday, February 12 / Tuesday, February 13

CS473-Algorithms I. Lecture 2. Asymptotic Notation. CS 473 Lecture 2 1

CS200: Hash Tables. Prichard Ch CS200 - Hash Tables 1

Math Section 2.2 Polynomial Functions

1 Graph Sparsfication

BST Sequence of Operations

UNIT 4C Iteration: Scalability & Big O. Efficiency

Outline and Reading. Analysis of Algorithms. Running Time. Experimental Studies. Limitations of Experiments. Theoretical Analysis

Computer Science Foundation Exam. August 12, Computer Science. Section 1A. No Calculators! KEY. Solutions and Grading Criteria.

15-859E: Advanced Algorithms CMU, Spring 2015 Lecture #2: Randomized MST and MST Verification January 14, 2015

Analysis of Algorithms

Pseudocode ( 1.1) Analysis of Algorithms. Primitive Operations. Pseudocode Details. Running Time ( 1.1) Estimating performance

Fundamental Algorithms

1.2 Binomial Coefficients and Subsets

Running Time ( 3.1) Analysis of Algorithms. Experimental Studies. Limitations of Experiments

Analysis of Algorithms

Alpha Individual Solutions MAΘ National Convention 2013

Switching Hardware. Spring 2018 CS 438 Staff, University of Illinois 1

Major CSL Write your name and entry no on every sheet of the answer script. Time 2 Hrs Max Marks 70

Wavelet Transform. CSE 490 G Introduction to Data Compression Winter Wavelet Transformed Barbara (Enhanced) Wavelet Transformed Barbara (Actual)

Intro to Scientific Computing: Solutions

Sorting. Task Description. Selection Sort. Should we worry about speed?

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring, Instructions:

Lecture 6. Lecturer: Ronitt Rubinfeld Scribes: Chen Ziv, Eliav Buchnik, Ophir Arie, Jonathan Gradstein

Big-O Analysis. Asymptotics

Mergesort. CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

Running Time. Analysis of Algorithms. Experimental Studies. Limitations of Experiments

Data Structures and Algorithms. Analysis of Algorithms

Examples and Applications of Binary Search

Chapter 4 The Datapath

The Nature of Light. Chapter 22. Geometric Optics Using a Ray Approximation. Ray Approximation

Chapter 3: The Efficiency of Algorithms. Invitation to Computer Science, C++ Version, Third Edition

CSC165H1 Worksheet: Tutorial 8 Algorithm analysis (SOLUTIONS)

Minimum Spanning Trees

SD vs. SD + One of the most important uses of sample statistics is to estimate the corresponding population parameters.

Chapter 3: The Efficiency of Algorithms

Optimal Mapped Mesh on the Circle

Chapter 3: The Efficiency of Algorithms Invitation to Computer Science,

DATA STRUCTURES. amortized analysis binomial heaps Fibonacci heaps union-find. Data structures. Appetizer. Appetizer

Elementary Data Structures. The Stack ADT ( 2.1.1) Applications of Stacks. Stacks, Queues, Vectors, Lists & Sequences Trees

Module 8-7: Pascal s Triangle and the Binomial Theorem

Algorithm. Counting Sort Analysis of Algorithms

CSC Design and Analysis of Algorithms

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

COSC 311: ALGORITHMS HW1: SORTING

Improving Template Based Spike Detection

Fast Fourier Transform (FFT) Algorithms

CS 171: Introduction to Computer Science II. Quicksort

Package RcppRoll. December 22, 2014

Lecture 6: Divide-and-Conquer

End Semester Examination CSE, III Yr. (I Sem), 30002: Computer Organization

CIS 121. Introduction to Trees

6.854J / J Advanced Algorithms Fall 2008

OCR Statistics 1. Working with data. Section 3: Measures of spread

MapReduce and Hadoop. Debapriyo Majumdar Data Mining Fall 2014 Indian Statistical Institute Kolkata. November 10, 2014

Math 3201 Notes Chapter 4: Rational Expressions & Equations

6.851: Advanced Data Structures Spring Lecture 17 April 24

Project 2.5 Improved Euler Implementation

Arithmetic Sequences

We can use a max-heap to sort data.

CSC 220: Computer Organization Unit 11 Basic Computer Organization and Design

Ones Assignment Method for Solving Traveling Salesman Problem

Balanced Greedy Colorings of Sparse Random Graphs

top() Applications of Stacks

CMPT 125 Assignment 2 Solutions

COMPUTER ORGANIZATION AND DESIGN The Hardware/Software Interface. Chapter 4. The Processor. Single-Cycle Disadvantages & Advantages

ENGI 4421 Probability and Statistics Faculty of Engineering and Applied Science Problem Set 1 Descriptive Statistics

How do we evaluate algorithms?

9.1. Sequences and Series. Sequences. What you should learn. Why you should learn it. Definition of Sequence

Morgan Kaufmann Publishers 26 February, COMPUTER ORGANIZATION AND DESIGN The Hardware/Software Interface. Chapter 5.

Transcription:

Merge Sort Alexadra Stefa

Merge Sort Divide ad Coquer Tehique Divide ad oquer Divide the problem i smaller problems Solve these problems Combie the aswers Merge sort Split the problem i 2 halves. Sort eah half. Merge the sorted halves. Eah of the three steps will brig a otributio to the time omplexity of the method. Resoures: http://iterativepytho.org/ruestoe/stati/pythods/sortsearh/themerg esort.html https://www.s.usfa.edu/~galles/visualizatio/comparisosort.html 2

Merge sort 0 1 2 3 5 6 7 7 1 3 9 1 8 6 p r q 3

Merge sort The atual sortig is doe whe mergig i this order: 7 1 3 9 1 8 6 7 1 3 9 1 2 1 7 3 9 3 1 3 7 9 1 8 6 5 1 6 8 7 6 1 6 8 1 1 3 6 7 8 9

Merge-Sort Exeutio Eah row shows the array after eah all to the Merge futio fiished. Origial Red items were moved by Merge. 0 1 2 3 5 6 7 7 1 3 9 1 8 6 1 7 3 9 1 8 6 1 7 3 9 1 8 6 1 3 7 9 1 8 6 1 3 7 9 1 8 6 1 3 7 9 1 6 8 1 3 7 9 1 6 8 1 1 3 6 7 8 9 MS(0,7) // q = 3 MS(0,3) // q = 1 MS(0,1) // q = 0 MS(0,0) //p==r, basease MS(1,1) //p==r Merge(0,0,1) MS(2,3) // q = 2 MS(2,2) MS(3,3) Merge(2,2,3) Merge(0,1,3) MS(,7) // q = 5 MS(,5) // q = MS(,) MS(5,5) Merge(,,5) MS(6,7) // q = 6 MS(6,6) MS(7,7) Merge(6,6,7) Merge(,5,7) Merge(0,3,7) Notatio: MS(p,r) for Merge-Sort(A,p,r) 5

Merge sort (CLRS) 7 1 3 9 1 6 8 6

Merge sort (CLRS) What part of the algorithm does the atual sortig (moves the data aroud)? 7 1 3 9 1 6 8 7

Merge sort (CLRS) What is the SPACE omplexity for this lie? How would you implemet this lie? (What C ode would you write?) Look at your ode. What is your spae omplexity? (keep the ostat) 7 1 3 9 1 6 8 8

Merge Sort Is it stable? Variatio that would ot be stable? How muh extra memory does it eed? Pay attetio to the implemetatio! Is it adaptive? Best, worst, average ases? 9

Merge Sort Is it stable? - YES Variatio that would ot be stable? How muh extra memory does it eed? Pay attetio to the implemetatio! Liear: Θ() Extra memory eeded for arrays L ad R i the worst ase is. Note that the extra memory used i merge is freed up, therefore we do ot have to repeatedly add it ad we will NOT get Θ(lg) extra memory. There will be at most lg ope reursive alls. Eah oe of those eeds ostat memory (oe stak frame) => extra memory due to reursio: *lg ( i.e. Θ(lg ) ) Total extra memory: + *lg = Θ(). Is it adaptive? - NO Best, worst, average ases? 10

Time omplexity Let T() be the time omplexity to sort (with merge sort) a array of elemets. Assume is a power of 2 (i.e. = 2 k ). What is the time omplexity to: Split the array i 2: Sort eah half (with MERGESORT): T(/2) Merge the aswers together: (or Θ()) We will see other ways to aswer this questio later. 11

Merge sort (CLRS) Reurree formula Here is the umber of items beig proessed Base ase: T(1) = (I the ode, see for what value of there is NO reursive all. Here whe p<r is false => p r => 1 ) Reursive ase: T() = 2T(/2) + also ok: T() = 2T(/2) + Θ() T(/2) T(/2) 12

Reursio Tree CLRS, page 38. 13

2 2 T ()........................ Reursio Tree Assume that is a power of 2: = 2 k. Number of levels: lg + 1 Eah level has the same ost: Total ost of the tree: (lg + 1)() = lg + = Θ(lg ) 2 2 Level Arg/ pb size Nodes per level 1 ode ost 0 1 Level ost 1 /2 2 /2 2/2 = 2 / / / = i /2 i 2 i /2 i 2 i /2 i k=lg 1 (=/2 k ) 2 k (=) =*1= /2 k = 2 k /2 k 1 =

Reursio Tree - brief Assume that is a power of 2: = 2 k. Number of levels: lg + 1 Eah level has the same ost: Total ost of the tree: (lg + 1)() = lg + = Θ(lg ) 2 2 T () 2..................... 2 2 2... 15

Tree of reursive alls to Merge-Sort MergeSort(A,0,6) proesses the 7 elemets betwee idexes 0 ad 6 (ilusive). The tree below shows all the reursive alls made. N=r-p+1 p 7 (0,6) r (0,3) 3 (,6) 2 (0,1) 2 (2,3) 2 (,5) 1 (6,6) 1 (0,0) 1 (1,1) 1 (2,2) 1 (3,3) 1 (,) 1 (5,5) 16

Mergesort Variatios (Sedgewik) Mergesort with isertio sort for small problem sizes (whe N is smaller tha a ut-off size). The base ase will be at say 10 ad it will ru isertio sort. Bottom-up mergesort, Iterative. Mergesort usig lists ad ot arrays. Both top-dow ad bottom-up implemetatios Sedgewik mergesort uses oe auxiliary array (ot two) Alterate betwee regular array ad the auxiliary oe Will opy data oly oe (ot twie) per reursive all. Costat extra spae (istead of liear extra spae). More ompliated, somewhat slower. Bitoi sequee (first ireasig, the dereasig) Elimiates the idex boudary hek for the subarrays. 17

Merge sort bottom-up

Merge sort bottom-up Notie that after eah pass, subarrays of ertai sizes (2,, 8, 16) or less are sorted. Colors show the subarrays of speifi sizes: 1, 2,, 8, 11. 7 1 3 9 1 6 8 5 15 2 Size 1 1 7 3 9 1 6 8 5 15 2 Size 2 1 3 7 9 1 6 8 2 5 15 Size 1 1 3 6 7 8 9 2 5 15 Size 8 1 1 2 3 5 6 7 8 9 15 Size 16 (11) 19