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

Similar documents
Merge Sort Goodrich, Tamassia Merge Sort 1

Merge Sort Goodrich, Tamassia. Merge Sort 1

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

Sorting. Data structures and Algorithms

Lecture 19 Sorting Goodrich, Tamassia

CPE702 Algorithm Analysis and Design Week 7 Algorithm Design Patterns

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

Sorting. Divide-and-Conquer 1

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

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

Data Structures and Algorithms " Sorting!

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

Merge Sort

Sorting Goodrich, Tamassia Sorting 1

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

SORTING AND SELECTION

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

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

SORTING, SETS, AND SELECTION

Quick-Sort. Quick-Sort 1

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

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:

Comparison Sorts. Chapter 9.4, 12.1, 12.2

QuickSort

Introduction to Computers and Programming. Today

Sorting and Selection

We can use a max-heap to sort data.

Programming II (CS300)

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

Chapter 4: Sorting. Spring 2014 Sorting Fun 1

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

Programming II (CS300)

COMP 352 FALL Tutorial 10

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

Sorting Algorithms. + Analysis of the Sorting Algorithms

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

Sorting and Searching

Sorting and Searching

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.

Parallelism; Parallel Collections

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

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

7. Sorting I. 7.1 Simple Sorting. Problem. Algorithm: IsSorted(A) 1 i j n. Simple Sorting

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

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

Hiroki Yasuga, Elisabeth Kolp, Andreas Lang. 25th September 2014, Scientific Programming

Priority queues. Priority queues. Priority queue operations

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

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

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

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

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

COMP Data Structures

Chapter 7 Sorting. Terminology. Selection Sort

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg

ECE 2574: Data Structures and Algorithms - Basic Sorting Algorithms. C. L. Wyatt

Algorithms Interview Questions

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

Divide and Conquer Algorithms

Visualizing Data Structures. Dan Petrisko

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

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

SORTING LOWER BOUND & BUCKET-SORT AND RADIX-SORT

Priority queue ADT Heaps. Lecture 21

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

About this exam review

EECS 2011M: Fundamentals of Data Structures

Motivation of Sorting

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

Sorting L7.2 Recall linear search for an element in an array, which is O(n). The divide-and-conquer technique of binary search divides the array in ha

Parallel Sorting Algorithms

Cpt S 122 Data Structures. Sorting

Quick Sort. CSE Data Structures May 15, 2002

CSE 373: Data Structures and Algorithms

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

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

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

Algorithm classification

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

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

Sorting. Bubble Sort. Pseudo Code for Bubble Sorting: Sorting is ordering a list of elements.

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

Sorting a File of Records

CS301 - Data Structures Glossary By

CSE 373 NOVEMBER 8 TH COMPARISON SORTS

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

Course Review for Finals. Cpt S 223 Fall 2008

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

Chapter 3:- Divide and Conquer. Compiled By:- Sanjay Patel Assistant Professor, SVBIT.

A loose end: binary search

Lecture 5: Sorting Part A

CSL 860: Modern Parallel

Data Structures. Alice E. Fischer. Lecture 4, Fall Alice E. Fischer Data Structures L4... 1/19 Lecture 4, Fall / 19

CS 310 Advanced Data Structures and Algorithms

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

CSCI2100B Data Structures Heaps

Course Review. Cpt S 223 Fall 2009

DATA STRUCTURES AND ALGORITHMS

Transcription:

Merge Sort 7 9 4 fi 4 7 9 7 fi 7 9 4 fi 4 9 7 fi 7 fi 9 fi 9 4 fi 4 Merge Sort 1

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 Recur: solve the subproblems associated with S 1 and S Conquer: combine the solutions for S 1 and S 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 where not all elements are in main memory ) Merge Sort

Merge-Sort Merge-sort on an input sequence S with n elements consists of three steps: Divide: partition S into two sequences S 1 and S of about n/ elements each Recur: recursively sort S 1 and S Conquer: merge S 1 and S into a unique sorted sequence 7 13 9 17 4 7 13 fi 7 13 9 17 4 fi 4 9 17 7 9 4 13 17 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 ) partition(s, n/) mergesort(s 1, C) mergesort(s, C) S merge(s 1, S ) Merge Sort 3

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/ 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/ elements each Output sorted sequence of A B S empty sequence while A.isEmpty() B.isEmpty() if A.first().element() < B.first().element() S.insertLast(A.remove(A.first())) else S.insertLast(B.remove(B.first())) while A.isEmpty() S.insertLast(A.remove(A.first())) while B.isEmpty() S.insertLast(B.remove(B.first())) return S Merge Sort 4

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 9 4 fi 4 7 9 7 fi 7 9 4 fi 4 9 7 fi 7 fi 9 fi 9 4 fi 4 Merge Sort 5

Execution Example Partition 7 9 4 3 8 6 1 fi 1 3 4 6 7 8 9 7 9 4 fi 4 7 9 3 8 6 1 fi 1 3 8 6 7 fi 7 9 4 fi 4 9 3 8 fi 3 8 6 1 fi 1 6 Merge Sort 6

Execution Example (cont.) Recursive call, partition 7 9 4 3 8 6 1 fi 1 3 4 6 7 8 9 7 9 4 fi 4 7 9 3 8 6 1 fi 1 3 8 6 7 fi 7 9 4 fi 4 9 3 8 fi 3 8 6 1 fi 1 6 Merge Sort 7

Execution Example (cont.) Recursive call, partition 7 9 4 3 8 6 1 fi 1 3 4 6 7 8 9 7 9 4 fi 4 7 9 3 8 6 1 fi 1 3 8 6 7 fi 7 9 4 fi 4 9 3 8 fi 3 8 6 1 fi 1 6 Merge Sort 8

Execution Example (cont.) Recursive call, base case 7 9 4 3 8 6 1 fi 1 3 4 6 7 8 9 7 9 4 fi 4 7 9 3 8 6 1 fi 1 3 8 6 7 fi 7 9 4 fi 4 9 3 8 fi 3 8 6 1 fi 1 6 Merge Sort 9

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

Execution Example (cont.) Merge 7 9 4 3 8 6 1 fi 1 3 4 6 7 8 9 7 9 4 fi 4 7 9 3 8 6 1 fi 1 3 8 6 7 fi 7 9 4 fi 4 9 3 8 fi 3 8 6 1 fi 1 6 Merge Sort 11

Execution Example (cont.) Recursive call,, base case, merge 7 9 4 3 8 6 1 fi 1 3 4 6 7 8 9 7 9 4 fi 4 7 9 3 8 6 1 fi 1 3 8 6 7 fi 7 9 4 fi 4 9 3 8 fi 3 8 6 1 fi 1 6 Merge Sort 1

Execution Example (cont.) Merge 7 9 4 3 8 6 1 fi 1 3 4 6 7 8 9 7 9 4 fi 4 7 9 3 8 6 1 fi 1 3 8 6 7 fi 7 9 4 fi 4 9 3 8 fi 3 8 6 1 fi 1 6 Merge Sort 13

Execution Example (cont.) Recursive call,, merge, merge 7 9 4 3 8 6 1 fi 1 3 4 6 7 8 9 7 9 4 fi 4 7 9 3 8 6 1 fi 1 3 6 8 7 fi 7 9 4 fi 4 9 3 8 fi 3 8 6 1 fi 1 6 Merge Sort 14

Execution Example (cont.) Merge 7 9 4 3 8 6 1 fi 1 3 4 6 7 8 9 7 9 4 fi 4 7 9 3 8 6 1 fi 1 3 6 8 7 fi 7 9 4 fi 4 9 3 8 fi 3 8 6 1 fi 1 6 Merge Sort 15

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 i sequences of size n/ i we make i+1 recursive calls Thus, the total running time of merge-sort is O(n log n) depth 0 1 i #seqs 1 i size n n/ n/ i Merge Sort 16

Analysis of Merge-Sort -supplementary T ( n) = T ( n / ) + kn; T (0) = T (1) = T ( n) = kn + T ( n / ); T (0) = T (1) = c So n n n n = kn + ( k + T ( )) = kn + ( k + T ( )) 4 n n n n n n = kn + ( k + ( k + T ( ))) = kn + ( k + ( k + T ( 4 8 n n n n n = kn + ( k + ( k +... + ( k + T ( )...))) log n 1. log n n n 3 n log ( n) n n = kn + k + k +. k +... + k 3 logn ( n) log = k n T ( n) is O ( n log n) c 3 i= 0 ( n) = kn(log n + 1) = n log n + n 7 13 9 17 4 7 13 fi 7 13 9 17 4 fi 4 9 17 4 7 9 13 17 n log n ))) for n>= Merge Sort 17

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