Merge Sort Goodrich, Tamassia Merge Sort 1

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

Merge Sort fi fi fi 4 9. Merge Sort Goodrich, Tamassia

Sorting Goodrich, Tamassia Sorting 1

Merge Sort Goodrich, Tamassia. Merge Sort 1

Lecture 19 Sorting Goodrich, Tamassia

Sorting. Data structures and Algorithms

Divide-and-Conquer. Divide-and conquer is a general algorithm design paradigm:

CPE702 Algorithm Analysis and Design Week 7 Algorithm Design Patterns

Recursive Sorts. Recursive Sorts. Divide-and-Conquer. Divide-and-Conquer. Divide-and-conquer paradigm:

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

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

Quick-Sort. Quick-sort is a randomized sorting algorithm based on the divide-and-conquer paradigm:

Merge Sort

Sorting. Divide-and-Conquer 1

SORTING, SETS, AND SELECTION

SORTING AND SELECTION

Sorting. Outline. Sorting with a priority queue Selection-sort Insertion-sort Heap Sort Quick Sort

1. The Sets ADT. 1. The Sets ADT. 1. The Sets ADT 11/22/2011. Class Assignment Set in STL

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Merge Sort & Quick Sort

Quick-Sort. Quick-Sort 1

Data Structures and Algorithms " Sorting!

Quick-Sort fi fi fi 7 9. Quick-Sort Goodrich, Tamassia

QuickSort

Comparison Sorts. Chapter 9.4, 12.1, 12.2

We can use a max-heap to sort data.

Introduction to Computers and Programming. Today

Sorting. Lecture10: Sorting II. Sorting Algorithms. Performance of Sorting Algorithms

Sorting and Selection

Algorithm Analysis Advanced Data Structure. Chung-Ang University, Jaesung Lee

Heaps 2. Recall Priority Queue ADT. Heaps 3/19/14

SORTING LOWER BOUND & BUCKET-SORT AND RADIX-SORT

COMP 352 FALL Tutorial 10

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

IS 709/809: Computational Methods in IS Research. Algorithm Analysis (Sorting)

Chapter 4: Sorting. Spring 2014 Sorting Fun 1

COMP2012H Spring 2014 Dekai Wu. Sorting. (more on sorting algorithms: mergesort, quicksort, heapsort)

Programming II (CS300)

CSE Data Structures and Introduction to Algorithms... In Java! Instructor: Fei Wang. Mid-Term Exam. CSE2100 DS & Algorithms 1

Plan of the lecture. Quick-Sort. Partition of lists (or using extra workspace) Quick-Sort ( 10.2) Quick-Sort Tree. Partitioning arrays

Outline and Reading. Quick-Sort. Partition. Quick-Sort. Quick-Sort Tree. Execution Example

Priority queues. Priority queues. Priority queue operations

Stores a collection of elements each with an associated key value

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Sorting and Searching

Programming II (CS300)

Algorithms and Data Structures (INF1) Lecture 7/15 Hua Lu

CH 8. HEAPS AND PRIORITY QUEUES

Sorting and Searching

CH. 8 PRIORITY QUEUES AND HEAPS

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

Sorting Algorithms. + Analysis of the Sorting Algorithms

COMP251: Greedy algorithms

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

Heaps Goodrich, Tamassia. Heaps 1

Sorting a File of Records

Describe how to implement deque ADT using two stacks as the only instance variables. What are the running times of the methods

Basic Techniques. Recursion. Today: Later on: Recursive methods who call themselves directly or indirectly.

COMP250: Priority queue ADT, Heaps. Lecture 23 Jérôme Waldispühl School of Computer Science McGill University

Review implementation of Stable Matching Survey of common running times. Turn in completed problem sets. Jan 18, 2019 Sprenkle - CSCI211

Algorithm classification

Cpt S 122 Data Structures. Sorting

Priority Queues. 04/10/03 Lecture 22 1

CHAPTER 11 SETS, AND SELECTION

Table ADT and Sorting. Algorithm topics continuing (or reviewing?) CS 24 curriculum

Chapter 7 Sorting. Terminology. Selection Sort

Heaps Outline and Required Reading: Heaps ( 7.3) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

Priority queue ADT Heaps. Lecture 21

9. Heap : Priority Queue

EECS 2011M: Fundamentals of Data Structures

Cpt S 223 Course Overview. Cpt S 223, Fall 2007 Copyright: Washington State University

Basic C Programming: Examples. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Dictionaries. Priority Queues

Lecture 5: Sorting Part A

Sorting Algorithms. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Sorting is a problem for which we can prove a non-trivial lower bound.

Implementations. Priority Queues. Heaps and Heap Order. The Insert Operation CS206 CS206

Topics Recursive Sorting Algorithms Divide and Conquer technique An O(NlogN) Sorting Alg. using a Heap making use of the heap properties STL Sorting F

COMP Data Structures

algorithm evaluation Performance

CSL 860: Modern Parallel

CSE 373 Final Exam 3/14/06 Sample Solution

CSE 5311 Notes 4a: Priority Queues

CSE 373: Data Structures and Algorithms

Priority Queues. Reading: 7.1, 7.2

SORTING. Comparison of Quadratic Sorts

Parallel Algorithms for (PRAM) Computers & Some Parallel Algorithms. Reference : Horowitz, Sahni and Rajasekaran, Computer Algorithms

Algorithms Interview Questions

CSCI 262 Data Structures. Recursive Function Analysis. Analyzing Power. Analyzing Power. Analyzing Power 3/31/2018

Union Find. Data Structures and Algorithms Andrei Bulatov

Binary heaps (chapters ) Leftist heaps

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

Heaps. 2/13/2006 Heaps 1

Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

CS106X Programming Abstractions in C++ Dr. Cynthia Bailey Lee

Algorithms, Spring 2014, CSE, OSU Lecture 2: Sorting

CS301 - Data Structures Glossary By

CSE373: Data Structure & Algorithms Lecture 18: Comparison Sorting. Dan Grossman Fall 2013

Data Structures and Algorithms Week 8

Transcription:

Merge Sort 7 2 9 4 2 4 7 9 7 2 2 7 9 4 4 9 7 7 2 2 9 9 4 4 2004 Goodrich, Tamassia Merge Sort 1

Review of Sorting Selection-sort: Search: search through remaining unsorted elements for min Remove: remove element from remainder list Append: append next min to end of sorted list Insertion-sort: Remove: fetch and remove next unsorted item Search: find correct position in sorted list Insert: insert next element into sorted list Heap-sort Uses the fact that a sequence of RemoveMin ops will return items in sorted order Construct: build heap by inserting each element Ordered remove: remove each min element sequentially 2004 Goodrich, Tamassia Merge Sort 2

Divide-and-Conquer ( 10.1.1) Divide-and conquer is a general algorithm design paradigm: Divide: divide the input data S in two disjoint subsets S 1 and S 2 Recur: solve the subproblems associated with S 1 and S 2 Conquer: combine the solutions for S 1 and S 2 into a solution for S The base case for the recursion are subproblems of size 0 or 1 Merge-sort is a sorting algorithm based on the divide-and-conquer paradigm Like heap-sort It uses a comparator It has O(n log n) running time Unlike heap-sort It does not use an auxiliary priority queue It accesses data in a sequential manner (suitable to sort data on a disk) 2004 Goodrich, Tamassia Merge Sort 3

Merge-Sort ( 10.1) Merge-sort on an input sequence S with n elements consists of three steps: Divide: partition S into two sequences S 1 and S 2 of about n/2 elements each Recur: recursively sort S 1 and S 2 Conquer: merge S 1 and S 2 into a unique sorted sequence Algorithm mergesort(s, C) Input sequence S with n elements, comparator C Output sequence S sorted according to C if S.size() > 1 (S 1, S 2 ) partition(s, n/2) mergesort(s 1, C) mergesort(s 2, C) S merge(s 1, S 2 ) 2004 Goodrich, Tamassia Merge Sort 4

Merging Two Sorted Sequences The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time Algorithm merge(a, B) Input sequences A and B with n/2 elements each Output sorted sequence of A B S empty sequence while A.empty() B.empty() if A.front() < B.front() S.addBack(A.front()); A.eraseFront(); else S.addBack(B.front()); B.eraseFront(); while A.empty() S.addBack(A.front()); A.eraseFront(); while B.empty() S.addBack(B.front()); B.eraseFront(); return S 2004 Goodrich, Tamassia Merge Sort 5

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 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 2004 Goodrich, Tamassia Merge Sort 6

Execution Example Partition 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6 2004 Goodrich, Tamassia Merge Sort 7

Execution Example (cont.) Recursive call, partition 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6 2004 Goodrich, Tamassia Merge Sort 8

Execution Example (cont.) Recursive call, partition 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6 2004 Goodrich, Tamassia Merge Sort 9

Execution Example (cont.) Recursive call, base case 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6 2004 Goodrich, Tamassia Merge Sort 10

Execution Example (cont.) Recursive call, base case 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6 2004 Goodrich, Tamassia Merge Sort 11

Execution Example (cont.) Merge 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6 2004 Goodrich, Tamassia Merge Sort 12

Execution Example (cont.) Recursive call,, base case, merge 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6 2004 Goodrich, Tamassia Merge Sort 13

Execution Example (cont.) Merge 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6 2004 Goodrich, Tamassia Merge Sort 14

Execution Example (cont.) Recursive call,, merge, merge 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 2004 Goodrich, Tamassia Merge Sort 15

Execution Example (cont.) Merge 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 2004 Goodrich, Tamassia Merge Sort 16

Analysis of Merge-Sort The height h of the merge-sort tree is O(log n) at each recursive call we divide in half the sequence, The overall amount or work done at the nodes of depth i is O(n) we partition and merge 2 i sequences of size n/2 i we make 2 i+1 recursive calls Thus, the total running time of merge-sort is O(n log n) depth #seqs size 0 1 n 1 2 n/2 i 2 i n/2 i 2004 Goodrich, Tamassia Merge Sort 17

Summary of Sorting Algorithms Algorithm Time Notes selection-sort O(n 2 ) insertion-sort O(n 2 ) heap-sort O(n log n) merge-sort O(n log n) slow in-place for small data sets (< 1K) slow in-place for small data sets (< 1K) fast in-place for large data sets (1K 1M) fast sequential data access for huge data sets (> 1M) 2004 Goodrich, Tamassia Merge Sort 18