MergeSort Source: Gibbs & Tamassia.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

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.
© 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.
© 2004 Goodrich, Tamassia Merge Sort1 Quick-Sort     29  9.
Analysis of Algorithms CS 477/677
MergeSort Source: Gibbs & Tamassia. 2 MergeSort MergeSort is a divide and conquer method of sorting.
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],
Lecture10: Sorting II Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
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.
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.
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,
Sorting Chapter 14.
Fundamentals of Algorithms MCS - 2 Lecture # 11
Algorithm Efficiency and Sorting
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.
More on Merge Sort CS 244 This presentation is not given in class
Radish-Sort 11/11/ :01 AM Quick-Sort     2 9  9
Objectives Introduce different known sorting algorithms
MSIS 655 Advanced Business Applications Programming
CS Two Basic Sorting Algorithms Review Exchange Sorting Merge Sorting
1 Lecture 13 CS2013.
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
Algorithm Efficiency and Sorting
Searching/Sorting/Searching
Merge Sort 5/30/2019 7:52 AM Merge Sort 7 2   7  2  2 7
CS203 Lecture 15.
Algorithm Efficiency and Sorting
Divide-and-Conquer 7 2  9 4   2   4   7
Presentation transcript:

MergeSort Source: Gibbs & Tamassia

MergeSort MergeSort is a divide and conquer method of sorting 2

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: Sort the left half of the the array using MergeSort. Sort the right half of the the array using MergeSort. Merge the sorted left and right halves. 3

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

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

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

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

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

MergeSort 9 9

Merge-Sort Tree 10 7 2  9 4  2 4 7 9 7  2  2 7 9  4  4 9 7  7 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 1 7 2  9 4  2 4 7 9 7  2  2 7 9  4  4 9 7  7 2  2 9  9 4  4 10 10

Execution Example Partition 7 2 9 4  3 8 6 1 11 11

Execution Example (cont.) Recursive call, partition 7 2 9 4  3 8 6 1 7 2  9 4 12 12

Execution Example (cont.) Recursive call, partition 7 2 9 4  3 8 6 1 7 2  9 4 7  2 13

Execution Example (cont.) Recursive call, base case 7 2 9 4  3 8 6 1 7 2  9 4 7  2 7  7 14

Execution Example (cont.) Recursive call, base case 7 2 9 4  3 8 6 1 7 2  9 4 7  2 7  7 2  2 15

Execution Example (cont.) Merge 7 2 9 4  3 8 6 1 7 2  9 4 7  2  2 7 7  7 2  2 16

Execution Example (cont.) Recursive call, …, base case, merge 7 2 9 4  3 8 6 1 7 2  9 4 7  2  2 7 9 4  4 9 7  7 2  2 9  9 4  4 17

Execution Example (cont.) Merge 7 2 9 4  3 8 6 1 7 2  9 4  2 4 7 9 7  2  2 7 9 4  4 9 7  7 2  2 9  9 4  4 18

Execution Example (cont.) Recursive call, …, merge, merge 7 2 9 4  3 8 6 1 7 2  9 4  2 4 7 9 3 8 6 1  1 3 6 8 7  2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 19

Execution Example (cont.) Merge 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 7 2  9 4  2 4 7 9 3 8 6 1  1 3 6 8 7  2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 20

# of comps / moves per merge Complexity of MergeSort Pass Number Number of merges Merge list length # of comps / moves per merge 1 2k-1 or n/2 1 or n/2k  21 2 2k-2 or n/4 2 or n/2k-1  22 3 2k-3 or n/8 4 or n/2k-2  23 . k – 1 21 or n/2k-1  2k-1 k 20 or n/2k  2k k = log n 21

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