MergeSort Source: Gibbs & Tamassia. 2 MergeSort MergeSort is a divide and conquer method of sorting.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

IS 2610: Data Structures Sorting Feb 16, Sorting Algorithms: Bubble sort Bubble sort  Move through the elements exchanging adjacent pairs if the.
Back to Sorting – More efficient sorting algorithms.
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
© 2004 Goodrich, Tamassia Merge Sort1 7 2  9 4   2  2 79  4   72  29  94  4.
© 2004 Goodrich, Tamassia Merge Sort1 7 2  9 4   2  2 79  4   72  29  94  4.
CSC 213 – Large Scale Programming or. Today’s Goals  Begin by discussing basic approach of quick sort  Divide-and-conquer used, but how does this help?
Data Structures Lecture 9 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
1 Merge Sort Review of Sorting Merge Sort. 2 Sorting Algorithms Selection Sort uses a priority queue P implemented with an unsorted sequence: –Phase 1:
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
Quicksort Quicksort     29  9.
Merge Sort1 Part-G1 Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
Merge Sort1 7 2  9 4   2  2 79  4   72  29  94  4.
Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.
CSC2100B Quick Sort and Merge Sort Xin 1. Quick Sort Efficient sorting algorithm Example of Divide and Conquer algorithm Two phases ◦ Partition phase.
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
Sorting Algorithms and Average Case Time Complexity
© 2004 Goodrich, Tamassia QuickSort1 Quick-Sort     29  9.
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.
Quick-Sort     29  9.
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
CS 171: Introduction to Computer Science II Quicksort.
1 Sorting/Searching CS308 Data Structures. 2 Sorting means... l Sorting rearranges the elements into either ascending or descending order within the array.
TTIT33 Algorithms and Optimization – Dalg Lecture 2 HT TTIT33 Algorithms and optimization Lecture 2 Algorithms Sorting [GT] 3.1.2, 11 [LD] ,
© 2004 Goodrich, Tamassia Merge Sort1 Quick-Sort     29  9.
Sorting Lower Bound Andreas Klappenecker based on slides by Prof. Welch 1.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
1 Designing algorithms There are many ways to design an algorithm. Insertion sort uses an incremental approach: having sorted the sub-array A[1…j - 1],
Sorting Fun1 Chapter 4: Sorting     29  9.
Lecture10: Sorting II Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Heapsort. Heapsort is a comparison-based sorting algorithm, and is part of the selection sort family. Although somewhat slower in practice on most machines.
Chapter 12 Binary Search and QuickSort Fundamentals of Java.
Sorting CS Sorting means... Sorting rearranges the elements into either ascending or descending order within the array. (we’ll use ascending order.)
Sorting and Searching. Selection Sort  “Search-and-Swap” algorithm 1) Find the smallest element in the array and exchange it with a[0], the first element.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
1 Parallel Sorting Algorithm. 2 Bitonic Sequence A bitonic sequence is defined as a list with no more than one LOCAL MAXIMUM and no more than one LOCAL.
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
1 Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
Towers of Hanoi Move n (4) disks from pole A to pole B such that a larger disk is never put on a smaller disk A BC ABC.
Sorting Quick, Merge & Radix Divide-and-conquer Technique subproblem 2 of size n/2 subproblem 1 of size n/2 a solution to subproblem 1 a solution to.
QuickSort by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data.
Merge Sort Comparison Left Half Data Movement Right Half Sorted.
CSE 250 – Data Structures. Today’s Goals  First review the easy, simple sorting algorithms  Compare while inserting value into place in the vector 
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Merge Sort 1/12/2018 5:48 AM Merge Sort 7 2   7  2  2 7
Advanced Sorting 7 2  9 4   2   4   7
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Merge Sort 1/12/2018 9:44 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
CS 162 Intro to Programming II
Quick Sort and Merge Sort
Quick-Sort 9/12/2018 3:26 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Quick-Sort 9/13/2018 1:15 AM Quick-Sort     2
Divide and Conquer.
MergeSort Source: Gibbs & Tamassia.
More on Merge Sort CS 244 This presentation is not given in class
Objectives Introduce different known sorting algorithms
Merge Sort 2/23/ :15 PM Merge Sort 7 2   7  2   4  4 9
Merge Sort 2/24/2019 6:07 PM Merge Sort 7 2   7  2  2 7
Quick-Sort 2/25/2019 2:22 AM Quick-Sort     2
Copyright © Aiman Hanna All rights reserved
Copyright © Aiman Hanna All rights reserved
Quick-Sort 4/8/ :20 AM Quick-Sort     2 9  9
Merge Sort 4/10/ :25 AM Merge Sort 7 2   7  2   4  4 9
Quick-Sort 4/25/2019 8:10 AM Quick-Sort     2
Searching/Sorting/Searching
Merge Sort 5/30/2019 7:52 AM Merge Sort 7 2   7  2  2 7
CS203 Lecture 15.
Divide-and-Conquer 7 2  9 4   2   4   7
Presentation transcript:

MergeSort Source: Gibbs & Tamassia

2 MergeSort MergeSort is a divide and conquer method of sorting

3 MergeSort Algorithm MergeSort is a recursive sorting procedure that uses at most O(n lg(n)) comparisons. To sort an array of n elements, we perform the following steps in sequence: If n < 2 then the array is already sorted. Otherwise, n > 1, and we perform the following three steps in sequence: 1.Sort the left half of the the array using MergeSort. 2.Sort the right half of the the array using MergeSort. 3.Merge the sorted left and right halves.

4 How to Merge Here are two lists to be merged: First: (12, 16, 17, 20, 21, 27) Second: (9, 10, 11, 12, 19) Checkout 12 and 9 First: (12, 16, 17, 20, 21, 27) Second: (10, 11, 12, 19) New: (9) Checkout 12 and 10 First: (12, 16, 17, 20, 21, 27) Second: (11, 12, 19) New: (9, 10)

5 Merge Example Checkout 12 and 11 First: (12, 16, 17, 20, 21, 27) Second: (12, 19) New: (9, 10, 11) Checkout 12 and 12 First: (16, 17, 20, 21, 27) Second: (12, 19) New: (9, 10, 11, 12)

6 Merge Example Checkout 16 and 12 First: (16, 17, 20, 21, 27) Second: (19) New: (9, 10, 11, 12, 12) Checkout 16 and 19 First: (17, 20, 21, 27) Second: (19) New: (9, 10, 11, 12, 12, 16)

7 Merge Example Checkout 17 and 19 First: (20, 21, 27) Second: (19) New: (9, 10, 11, 12, 12, 16, 17) Checkout 20 and 19 First: (20, 21, 27) Second:( ) New: (9, 10, 11, 12, 12, 16, 17, 19)

8 Merge Example Checkout 20 and empty list First:( ) Second:( ) New: (9, 10, 11, 12, 12, 16, 17, 19, 20, 21, 27)

9 MergeSort

10 Merge-Sort Tree An execution of merge-sort is depicted by a binary tree – each node represents a recursive call of merge-sort and stores unsorted sequence before the execution and its partition sorted sequence at the end of the execution – the root is the initial call – the leaves are calls on subsequences of size 0 or  9 4   2  2 79  4   72  29  94  4

11 Execution Example Partition 

12 Execution Example (cont.) Recursive call, partition 7 2  

13 Execution Example (cont.) Recursive call, partition 7 2   

14 Execution Example (cont.) Recursive call, base case 7 2   2 7  77  

15 Execution Example (cont.) Recursive call, base case 7 2   2 7  77  72  22  

16 Execution Example (cont.) Merge 7 2   2   77  72  22  

17 Execution Example (cont.) Recursive call, …, base case, merge 7 2   2    77  72  22    94  4

18 Execution Example (cont.) Merge 7 2  9 4   2    77  72  22  29  94  

19 Execution Example (cont.) Recursive call, …, merge, merge 7 2  9 4    2      77  72  22  29  94  43  33  38  88  86  66  61  11  

20 Execution Example (cont.) Merge 7 2  9 4    2      77  72  22  29  94  43  33  38  88  86  66  61  11   

21 Complexity of MergeSort Pass Number Number of merges Merge list length # of comps / moves per merge 12 k-1 (n/2)1 (n/2 k )  21 k-2 (n/4)2 (n/2 k-1 )  22 k-3 (n/8)4 (n/2 k-2 )  k – 12 1 (n/2 k-1 )2 k-2 (n/4)  2 k-1 k2 0 (n/2 k )2 k-1 (n/2)  2k 2k k = log n

22 Multiplying the number of merges by the maximum number of comparisons per merge, we get: (2 k-1 )2 1 = 2 k (2 k-2 )2 2 = 2 k  (2 1 )2 k-1 = 2 k (2 0 )2 k = 2 k Complexity of MergeSort k passes each require 2 k comparisons (and moves). But k = lg n and hence, we get lg(n)  n comparisons or O(n lgn)