Alexandra Stefan Last updated: 4/16/2019

Slides:



Advertisements
Similar presentations
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Advertisements

ADA: 5. Quicksort1 Objective o describe the quicksort algorithm, it's partition function, and analyse its running time under different data conditions.
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search.
CMPS1371 Introduction to Computing for Engineers SORTING.
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
CS 171: Introduction to Computer Science II Quicksort.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Sorting Chapter 6 Chapter 6 –Insertion Sort 6.1 –Quicksort 6.2 Chapter 5 Chapter 5 –Mergesort 5.2 –Stable Sorts Divide & Conquer.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
10/14/ Algorithms1 Algorithms - Ch2 - Sorting.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
Getting Started Introduction to Algorithms Jeff Chastine.
Divide and Conquer Sorting Algorithms COMP s1 Sedgewick Chapters 7 and 8.
CSC317 1 So far so good, but can we do better? Yes, cheaper by halves... orkbook/cheaperbyhalf.html.
Nothing is particularly hard if you divide it into small jobs. Henry Ford Nothing is particularly hard if you divide it into small jobs. Henry Ford.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Example Algorithms CSE 2320 – Algorithms and Data Structures
Fundamentals of Algorithms MCS - 2 Lecture # 11
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Lecture No.45 Data Structures Dr. Sohail Aslam.
CS 3343: Analysis of Algorithms
Algorithm Design & Analysis
Mergesort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
Divide-And-Conquer-And-Combine
Teach A level Computing: Algorithms and Data Structures
Divide and Conquer.
MergeSort Source: Gibbs & Tamassia.
Algorithm Design Methods
CS 3343: Analysis of Algorithms
Chapter 4: Divide and Conquer
CSC212 Data Structure - Section RS
Ch 7: Quicksort Ming-Te Chi
Hassan Khosravi / Geoffrey Tien
Mergesort Based on divide-and-conquer strategy
Richard Anderson Lecture 13 Divide and Conquer
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
Divide-And-Conquer-And-Combine
CS200: Algorithms Analysis
Recurrences (Method 4) Alexandra Stefan.
CSE 326: Data Structures Sorting
Divide-and-Conquer The most-well known algorithm design strategy:
CSE 332: Data Abstractions Sorting I
CSE 373 Data Structures and Algorithms
Algorithms: Design and Analysis
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Divide & Conquer Algorithms
Merge Sort (11.1) CSE 2011 Winter April 2019.
Topic: Divide and Conquer
Sorting and Asymptotic Complexity
CSC 380: Design and Analysis of Algorithms
David Kauchak cs161 Summer 2009
Algorithms Recurrences.
Recurrences.
The Selection Problem.
nalysis of lgorithms A A Universidad Nacional de Colombia
Divide and Conquer Merge sort and quick sort Binary search
Advanced Sorting Methods: Shellsort
CMPT 225 Lecture 10 – Merge Sort.
Presentation transcript:

Alexandra Stefan Last updated: 4/16/2019 Merge Sort Alexandra Stefan Last updated: 4/16/2019

Mergesort Idea Code/pseudocode Properties: Time complexity: O(NlgN) Stable - Yes Adaptive – No Better cache usage than Quicksort (local data moves) Time complexity: O(NlgN) Recurrence formula Space complexity: O(N) O(N) for merge + O(lgN) for recursion stack Variations with O(1) space for merge exist Implementation tricks Use ‘infinity’ – CLRS Use Bitonic sequence Variations Use insertion sort for small N Bottom-up (iterative) Works for linked lists External sorting – see wikipedia section “Use with tape drives”

Merge Sort – Divide and Conquer Technique Divide the problem in smaller problems Split the problem in 2 halves. Solve these problems Sort each half. Combine the answers Merge the sorted halves. Each of the three steps will bring a contribution to the time complexity of the method. Resources: http://interactivepython.org/runestone/static/pythonds/SortSearch/TheMergeSort.html https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html

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

Tree of recursive calls to Merge-Sort MergeSort(A,0,6) processes the 7 elements between indexes 0 and 6 (inclusive). The tree below shows all the recursive calls made. N=r-p+1 p 7 (0,6) r 4 (0,3) 3 (4,6) 2 (0,1) 2 (2,3) 2 (4,5) 1 (6,6) 1 (0,0) 1 (1,1) 1 (2,2) 1 (3,3) 1 (4,4) 1 (5,5) 1 2 3 4 5 6 7 9 8

Merge sort The actual sorting is done when merging in this order: 7 1 3 9 4 8 6 7 1 3 9 1 2 1 7 3 9 3 1 3 7 9 4 1 8 6 4 5 1 4 6 8 6 1 4 6 8 7 1 3 4 6 7 8 9

Merge-Sort Execution MS(0,7) // q = 3 |MS(0,3) // q = 1 | |MS(0,1) // q = 0 | | MS(0,0) //p==r, basecase | | 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(4,7) // q = 5 | |MS(4,5) // q = 4 | | MS(4,4) | | MS(5,5) | | Merge(4,4,5) | |MS(6,7) // q = 6 | | MS(6,6) | | MS(7,7) | | Merge(6,6,7) | |Merge(4,5,7) |Merge(0,3,7) Each row shows the array after each call to the Merge function finished. Red items were moved by Merge. 1 2 3 4 5 6 7 9 8 Original Notation: MS(p,r) for Merge-Sort(A,p,r)

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

Merge sort (CLRS) What part of the algorithm does the actual sorting (moves the data around)? 7 1 3 9 4 6 8

Merge sort (CLRS) What is the SPACE complexity for this line? How would you implement this line? (What C code would you write?) Look at your code. What is your space complexity? (keep the constant) 7 1 3 9 4 6 8

Merge Sort Is it stable? How much extra memory does it need? Variation that would not be stable? How much extra memory does it need? Is it adaptive? Best, worst, average cases?

Merge Sort Is it stable? - YES Is it adaptive? - NO Variation that would not be stable? Is it adaptive? - NO Best, worst, average cases? How much extra memory does it need? Pay attention to the implementation! Linear: Θ(n) Extra memory needed for arrays L and R in the worst case is n. Note that the extra memory used in merge is freed up, therefore we do not have to repeatedly add it and we will NOT get Θ(nlgn) extra memory. There will be at most lg n open recursive calls. Each one of those needs constant memory (one stack frame) => extra memory due to recursion: c*lg n ( i.e. Θ(lg n) ) Total extra memory: n + c*lg n = Θ(n).

Time complexity Let T(n) be the time complexity to sort (with merge sort) an array of n elements. Assume n is a power of 2 (i.e. n = 2k). What is the time complexity to: Split the array in 2: c Sort each half (with MERGESORT): T(n/2) Merge the answers together: cn (or Θ(n))

Merge sort (CLRS) Recurrence formula Base case: Recursive case: Here n is the number of items being processed Base case: T(1) = c (In the code, see for what value of n there is NO recursive call. Here when p<r is false => p≤r => n ≤ 1 ) Recursive case: T(n) = 2T(n/2) + cn also ok: T(n) = 2T(n/2) + Θ(n) T(n/2) T(n/2)

Recursion Tree Assume that n is a power of 2: n = 2k. Number of levels: lg n + 1 Each level has the same cost: cn Total cost of the tree: (lg n + 1)(cn) = cn lg n + cn = Θ(nlg n) CLRS book: see page 38 Level Arg/ pb size Nodes per level 1 node cost Level cost n 1 cn n/2 2 cn/2 2cn/2 =cn n/4 4 cn/4 4cn/4 … i n/2i 2i cn/2i 2icn/2i k=lgn (=n/2k) 2k (=n) c=c*1= cn/2k 2kcn/2k . . . . . . . . . . . . . . . . . . . . . . . .

Recursion Tree - brief Assume that n is a power of 2: n = 2k. Number of levels: lg n + 1 Each level has the same cost: cn Total cost of the tree: (lg n + 1)(cn) = cn lg n + cn = Θ(nlg n) . . . . . . . . . . . . . . . . . . . . . . . .

Mergesort Variations (Sedgewick, wiki) Mergesort with insertion sort for small problem sizes (when N is smaller than a cut-off size). The base case will be at say n≤10 and it will run insertion sort. Bottom-up mergesort, Iterative. Mergesort using lists and not arrays. Both top-down and bottom-up implementations Constant extra space (instead of linear) – see Sedgewick More complicated, somewhat slower (high constant factors). Still needs the O(lnN) for the stack (for recursion). A version with purely O(1) space exists, but is not stable - see wikipedia, Katajainen et al. Bitonic sequence (first increasing, then decreasing) – see Sedgewick Eliminates the index boundary check for the subarrays. External sorting – see wikipedia page, section “Use with tape drives” Uses the bottom-up version With one auxiliary array (not two) – see Sedgewick Alternate between regular array and the auxiliary one Will copy data only once (not twice) per recursive call.

Merge sort bottom-up Notice that after each pass, subarrays of certain sizes (2, 4, 8, 16) or less are sorted. Colors show the subarrays of specific sizes: 1, 2, 4, 8, 11. 7 1 3 9 4 6 8 5 15 2 Size 1 Size 2 1 7 3 9 4 6 8 5 15 2 1 3 7 9 4 6 8 2 5 15 Size 4 Size 8 1 3 4 6 7 8 9 2 5 15 Size 16 (11) 1 2 3 4 5 6 7 8 9 15