MERGESORT BBM ALGORITHMS DEPT. OF COMPUTER ENGINEERING ERKUT ERDEM. Mergesort. Feb. 27, 2014

Similar documents
MERGESORT BBM ALGORITHMS DEPT. OF COMPUTER ENGINEERING. Mergesort

Algorithms. Algorithms. Algorithms 2.2 M ERGESORT. mergesort bottom-up mergesort. sorting complexity divide-and-conquer

Algorithms. Algorithms 2.2 MERGESORT. mergesort bottom-up mergesort sorting complexity divide-and-conquer ROBERT SEDGEWICK KEVIN WAYNE

ELEMENTARY SORTING ALGORITHMS

SORTING ALGORITHMS BBM ALGORITHMS DEPT. OF COMPUTER ENGINEERING ERKUT ERDEM. Mar. 21, 2013

Outline. 1 Merging. 2 Merge Sort. 3 Complexity of Sorting. 4 Merge Sort and Other Sorts 2 / 10

Sorting Algorithms. !rules of the game!shellsort!mergesort!quicksort!animations. Classic sorting algorithms

Inf 2B: Sorting, MergeSort and Divide-and-Conquer

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

Advanced Algebraic Algorithms on Integers and Polynomials

Lecture 14: Nov. 11 & 13

CS 770G - Parallel Algorithms in Scientific Computing

Lecture 4: November 17, Part 1 Single Buffer Management

Exercises. 18 Algorithms

Outline and Reading. Dynamic Programming. Dynamic Programming revealed. Computing Fibonacci. The General Dynamic Programming Technique

Dynamic Programming! CSE 417: Algorithms and Computational Complexity!

Partition and Select

Learning Theory: Lecture Notes

CS 350 Algorithms and Complexity

Mergesort and Recurrences (CLRS 2.3, 4.4)

Single Variable Optimization

Divide-Conquer-Glue. Divide-Conquer-Glue Algorithm Strategy. Skyline Problem as an Example of Divide-Conquer-Glue

Outline. 1 Introduction. Merging and MergeSort. 3 Analysis. 4 Reference

Homework 9 Solutions. 1. (Exercises from the book, 6 th edition, 6.6, 1-3.) Determine the number of distinct orderings of the letters given:

The Selection Problem - Variable Size Decrease/Conquer (Practice with algorithm analysis)

Divide-and-Conquer. Consequence. Brute force: n 2. Divide-and-conquer: n log n. Divide et impera. Veni, vidi, vici.

Chapter 5. Divide and Conquer CLRS 4.3. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

V. Adamchik 1. Recurrences. Victor Adamchik Fall of 2005

The maximum-subarray problem. Given an array of integers, find a contiguous subarray with the maximum sum. Very naïve algorithm:

For now, let us focus on a specific model of neurons. These are simplified from reality but can achieve remarkable results.

ENTROPIC QUESTIONING

CHAPTER 17 Amortized Analysis

Week 5: Quicksort, Lower bound, Greedy

CPS 616 DIVIDE-AND-CONQUER 6-1

A design paradigm. Divide and conquer: (When) does decomposing a problem into smaller parts help? 09/09/ EECS 3101

Divide&Conquer: MergeSort. Algorithmic Thinking Luay Nakhleh Department of Computer Science Rice University Spring 2014

princeton univ. F 13 cos 521: Advanced Algorithm Design Lecture 3: Large deviations bounds and applications Lecturer: Sanjeev Arora

data structures and algorithms lecture 2

Chapter 5. Divide and Conquer. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Chapter 5. Divide and Conquer CLRS 4.3. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Divide and Conquer. Recurrence Relations

Design and Analysis of Algorithms

Elementary Sorts 1 / 18

Common loop optimizations. Example to improve locality. Why Dependence Analysis. Data Dependence in Loops. Goal is to find best schedule:

Algorithms Test 1. Question 1. (10 points) for (i = 1; i <= n; i++) { j = 1; while (j < n) {

Vapnik-Chervonenkis theory

Algorithms. Algorithms 2.3 QUICKSORT. quicksort selection duplicate keys system sorts ROBERT SEDGEWICK KEVIN WAYNE.

Algorithm Design and Analysis

Stanford University CS359G: Graph Partitioning and Expanders Handout 4 Luca Trevisan January 13, 2011

NON LINEAR ANALYSIS OF STRUCTURES ACCORDING TO NEW EUROPEAN DESIGN CODE

5. DIVIDE AND CONQUER I

DATA STRUCTURES FOR LOGIC OPTIMIZATION

Lecture 22: Multithreaded Algorithms CSCI Algorithms I. Andrew Rosenberg

CSci 6974 and ECSE 6966 Math. Tech. for Vision, Graphics and Robotics Lecture 21, April 17, 2006 Estimating A Plane Homography

Finding Primitive Roots Pseudo-Deterministically

Algorithms And Programming I. Lecture 5 Quicksort

For combinatorial problems we might need to generate all permutations, combinations, or subsets of a set.

= 1.23 m/s 2 [W] Required: t. Solution:!t = = 17 m/s [W]! m/s [W] (two extra digits carried) = 2.1 m/s [W]

A generalization of a trace inequality for positive definite matrices

Effective Power Optimization combining Placement, Sizing, and Multi-Vt techniques

Lecture 4. Quicksort

Introduction to Divide and Conquer

2.3 Nilpotent endomorphisms

A CLASS OF RECURSIVE SETS. Florentin Smarandache University of New Mexico 200 College Road Gallup, NM 87301, USA

On some variants of Jensen s inequality

Errors for Linear Systems

Ensemble Methods: Boosting

Algorithms. Algorithms 2.3 QUICKSORT. quicksort selection duplicate keys system sorts ROBERT SEDGEWICK KEVIN WAYNE.

Divide and Conquer. Arash Rafiey. 27 October, 2016

Calculation of time complexity (3%)

SUBSTRING SEARCH BBM ALGORITHMS TODAY DEPT. OF COMPUTER ENGINEERING ERKUT ERDEM. Apr. 29, Substring search applications.

Appendix B: Resampling Algorithms

Lecture 2: Prelude to the big shrink

RELIABILITY ASSESSMENT

JOURNAL OF OBJECT TECHNOLOGY

CS 331 DESIGN AND ANALYSIS OF ALGORITHMS DYNAMIC PROGRAMMING. Dr. Daisy Tang

Foundations of Arithmetic

Dynamic Programming. Preview. Dynamic Programming. Dynamic Programming. Dynamic Programming (Example: Fibonacci Sequence)

Lecture 21: Numerical methods for pricing American type derivatives

SUBSTRING SEARCH BBM ALGORITHMS TODAY DEPT. OF COMPUTER ENGINEERING ERKUT ERDEM. Apr. 28, Substring search applications.

Feature Selection & Dynamic Tracking F&P Textbook New: Ch 11, Old: Ch 17 Guido Gerig CS 6320, Spring 2013

Structure and Drive Paul A. Jensen Copyright July 20, 2003

E Tail Inequalities. E.1 Markov s Inequality. Non-Lecture E: Tail Inequalities

5. DIVIDE AND CONQUER I

Divide and Conquer Algorithms

Review Of Topics. Review: Induction

Chapter 3 Describing Data Using Numerical Measures

Suppose that there s a measured wndow of data fff k () ; :::; ff k g of a sze w, measured dscretely wth varable dscretzaton step. It s convenent to pl

04 - Treaps. Dr. Alexander Souza

Compiling for Parallelism & Locality. Example. Announcement Need to make up November 14th lecture. Last time Data dependences and loops

Supplement: Proofs and Technical Details for The Solution Path of the Generalized Lasso

The Problem: Mapping programs to architectures

COMP 382: Reasoning about algorithms

Economics 101. Lecture 4 - Equilibrium and Efficiency

Modeling and Simulation NETW 707

Survey says... Announcements. Survey says... Survey says...

Copyright 2000, Kevin Wayne 1

PARTICIPATION FACTOR IN MODAL ANALYSIS OF POWER SYSTEMS STABILITY

This lecture and the next. Why Sorting? Sorting Algorithms so far. Why Sorting? (2) Selection Sort. Heap Sort. Heapsort

Quicksort. ! quicksort! selection! duplicate keys! system sorts. Two classic sorting algorithms

Transcription:

ergesort BB 202 - ALGOITHS Basc plan. Dvde array nto two halves. ecursvely sort each half. erge two halves. DPT. OF COPUT NGINING KUT D GSOT nput G S O T X A P L sort left half G O S T X A P L sort rght half G O S A L P T X merge results A G L O P S T X ergesort overvew Feb. 27, 2014 Acnowledgement: The course sldes are adapted from the sldes prepared by. Sedgewc and K. Wayne of Prnceton Unversty. 2 Abstract n-place merge Abstract n-place merge lo G sorted md md+1 A h C lo T G md md+1 A h C T sorted copy to auxlary array 3 4

Abstract n-place merge Abstract n-place merge compare mnmum n each subarray G A C T 5 6 Abstract n-place merge Abstract n-place merge A A G A C T compare mnmum n each subarray compare mnmum n each subarray 7 8

Abstract n-place merge Abstract n-place merge A C G A C T A C G A C T compare mnmum n each subarray compare mnmum n each subarray 9 10 Abstract n-place merge Abstract n-place merge A C G A C T A C A C T compare mnmum n each subarray compare mnmum n each subarray 11 12

Abstract n-place merge Abstract n-place merge A C A C T A C A C T compare mnmum n each subarray compare mnmum n each subarray G A C T 13 14 Abstract n-place merge Abstract n-place merge A C A C T A C A C T compare mnmum n each subarray compare mnmum n each subarray 15 16

Abstract n-place merge Abstract n-place merge A C AG C T A C G C T compare mnmum n each subarray compare mnmum n each subarray 17 18 Abstract n-place merge Abstract n-place merge A C G C T A C G T compare mnmum n each subarray compare mnmum n each subarray 19 20

Abstract n-place merge Abstract n-place merge A C G T A C G T compare mnmum n each subarray one subarray exhausted, tae from other 21 22 Abstract n-place merge Abstract n-place merge A C G T A C G T one subarray exhausted, tae from other one subarray exhausted, tae from other 23 24

Abstract n-place merge Abstract n-place merge A C G T A C G T one subarray exhausted, tae from other both subarrays exhausted, done 25 26 Abstract n-place merge ergng Q. How to combne two sorted subarrays nto a sorted whole. A. Use an auxlary array. lo h A C G T sorted nput copy merged result 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 - - - - - - - - - - 0 5 0 A 0 6 1 A C 0 7 G C T 2 A C 1 7 G T 3 A C 2 7 G T 4 A C 2 8 G T 5 A C G 3 8 G T 6 A C G 4 8 T 7 A C G 5 8 T 8 A C G 5 9 T 9 A C G T 6 10 T A C G T Abstract n-place merge trace 27 28

ergng: Java mplementaton ergesort: Java mplementaton prvate statc vod merge(comparable[] a, Comparable[] aux, nt lo, nt md, nt h) assert ssorted(a, lo, md); // precondton: a[lo..md] sorted assert ssorted(a, md+1, h); // precondton: a[md+1..h] sorted for (nt = lo; <= h; ++) copy aux[] = a[]; nt = lo, = md+1; for (nt = lo; <= h; ++) f ( > md) a[] = aux[++]; else f ( > h) a[] = aux[++]; else f (less(aux[], aux[])) a[] = aux[++]; else a[] = aux[++]; assert ssorted(a, lo, h); lo // postcondton: a[lo..h] sorted md A G L O H I S T h merge publc class erge prvate statc vod merge(comparable[] a, Comparable[] aux, nt lo, nt md, nt h) /* as before */ prvate statc vod sort(comparable[] a, Comparable[] aux, nt lo, nt h) f (h <= lo) return; nt md = lo + (h - lo) / 2; sort (a, aux, lo, md); sort (a, aux, md+1, h); merge(a, aux, lo, md, h); publc statc vod sort(comparable[] a) aux = new Comparable[a.length]; sort(a, aux, 0, a.length - 1); A G H I L 29 lo md h 10 11 12 13 14 15 16 17 18 19 30 ergesort: trace ergesort: anmaton 50 random tems lo h merge(a, 0, 0, 1) merge(a, 2, 2, 3) merge(a, 0, 1, 3) merge(a, 4, 4, 5) merge(a, 6, 6, 7) merge(a, 4, 5, 7) merge(a, 0, 3, 7) merge(a, 8, 8, 9) merge(a, 10, 10, 11) merge(a, 8, 9, 11) merge(a, 12, 12, 13) merge(a, 14, 14, 15) merge(a, 12, 13, 15) merge(a, 8, 11, 15) merge(a, 0, 7, 15) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 G S O T X A P L G S O T X A P L G S O T X A P L G S O T X A P L G S O T X A P L G S O T X A P L G O S T X A P L G O S T X A P L G O S T X A P L G O S T A X P L G O S A T X P L G O S A T X P L G O S A T X P L G O S A T X L P G O S A L P T X A G L O P S T X Trace of merge results for top-down mergesort result after recursve call http://www.sortng-algorthms.com/merge-sort algorthm poston n order current subarray not n order 31 32

ergesort: anmaton ergesort: emprcal analyss 50 reverse-sorted tems http://www.sortng-algorthms.com/merge-sort algorthm poston n order current subarray not n order unnng tme estmates: Laptop executes 10 8 compares/second. Supercomputer executes 10 12 compares/second. nserton sort (N mergesort (N log N) computer thousand mllon bllon thousand mllon bllon home nstant 2.8 hours 317 years nstant 1 second 18 mn super nstant 1 second 1 wee nstant nstant nstant Bottom lne. Good algorthms are better than supercomputers. 33 34 ergesort: number of compares and array accesses Proposton. ergesort uses at most N lg N compares and 6 N lg N array accesses to sort any array of sze N. Pf setch. The number of compares C (N) and array accesses A (N) to mergesort an array of sze N satsfy the recurrences: Dvde-and-conquer recurrence: proof by pcture Proposton. If D (N) satsfes D (N) = 2 D (N / 2) + N for N > 1, wth D (1) = 0, then D (N) = N lg N. Pf 1. [assumng N s a power of 2] D (N) N = N C (N) C ( N / 2 ) + C ( N / 2 ) + N for N > 1, wth C (1) = 0. D (N / 2) D (N / 2) 2 (N/2) = N left half rght half merge A (N) A ( N / 2 ) + A ( N / 2 ) + 6 N for N > 1, wth A (1) = 0. We solve the recurrence when N s a power of 2. lg N D (N / 4) D (N / 4) D (N / 4) D (N / 2 ) D (N / 4) 4 (N/4) = N... 2 (N/2 ) = N... D (N) = 2 D (N / 2) + N, for N > 1, wth D (1) = 0. D (2) D (2) D (2) D (2) D (2) D (2) D (2) D (2) N/2 (2) = N N lg N 35 36

Dvde-and-conquer recurrence: proof by expanson Proposton. If D (N) satsfes D (N) = 2 D (N / 2) + N for N > 1, wth D (1) = 0, then D (N) = N lg N. Pf 2. [assumng N s a power of 2] gven D (N) = 2 D (N/2) + N D (N) / N = 2 D (N/2) / N + 1 dvde both sdes by N = D (N/2) / (N/2) + 1 algebra = D (N/4) / (N/4) + 1 + 1 apply to frst term = D (N/8) / (N/8) + 1 + 1 + 1 Dvde-and-conquer recurrence: proof by nducton Proposton. If D (N) satsfes D (N) = 2 D (N / 2) + N for N > 1, wth D (1) = 0, then D (N) = N lg N. Pf 3. [assumng N s a power of 2] Base case: N = 1. Inductve hypothess: D (N) = N lg N. Goal: show that D (2N) = (2N) lg (2N). D (2N) = 2 D (N) + 2N = 2 N lg N + 2N gven nductve hypothess... = D (N/N) / (N/N) + 1 + 1 +... + 1 apply to frst term agan = 2 N (lg (2N) 1) + 2N = 2 N lg (2N) algebra = lg N stop applyng, D(1) = 0 QD 37 38 ergesort analyss: memory Proposton. ergesort uses extra space proportonal to N. Pf. The array needs to be of sze N for the last merge. A C D G H I N U V two sorted subarrays A B C D F G H I J N O P Q S T U V merged result Def. A sortng algorthm s n-place f t uses c log N extra memory. x. Inserton sort, selecton sort, shellsort. B F J O P Q S T Challenge for the bored. In-place merge. [Kronrod, 1969] ergesort: practcal mprovements Use nserton sort for small subarrays. ergesort has too much overhead for tny subarrays. Cutoff to nserton sort for 7 tems. prvate statc vod sort(comparable[] a, Comparable[] aux, nt lo, nt h) f (h <= lo + CUTOFF - 1) Inserton.sort(a, lo, h); nt md = lo + (h - lo) / 2; sort (a, aux, lo, md); sort (a, aux, md+1, h); merge(a, aux, lo, md, h); 39 40

ergesort: practcal mprovements Stop f already sorted. Is bggest tem n frst half smallest tem n second half? Helps for partally-ordered arrays. A B C D F G H I J N O P Q S T U V A B C D F G H I J N O P Q S T U V prvate statc vod sort(comparable[] a, Comparable[] aux, nt lo, nt h) f (h <= lo) return; nt md = lo + (h - lo) / 2; sort (a, aux, lo, md); sort (a, aux, md+1, h); f (less(a[md+1], a[md])) return; merge(a, aux, lo, md, h); ergesort: practcal mprovements lmnate the copy to the auxlary array. Save tme (but not space) by swtchng the role of the nput and auxlary array n each recursve call. prvate statc vod merge(comparable[] a, Comparable[] aux, nt lo, nt md, nt h) nt = lo, = md+1; for (nt = lo; <= h; ++) f ( > md) aux[] = a[++]; else f ( > h) aux[] = a[++]; merge from to else f (less(a[], a[])) aux[] = a[++]; else aux[] = a[++]; prvate statc vod sort(comparable[] a, Comparable[] aux, nt lo, nt h) f (h <= lo) return; nt md = lo + (h - lo) / 2; sort (aux, a, lo, md); sort (aux, a, md+1, h); merge(aux, a, lo, md, h); 41 swtch roles of and 42 ergesort: vsualzaton Bottom-up mergesort frst subarray second subarray frst merge frst half sorted second half sorted result 43 Basc plan. Pass through array, mergng subarrays of sze 1. epeat for subarrays of sze 2, 4, 8, 16,... sz = 1 merge(a, 0, 0, 1) merge(a, 2, 2, 3) merge(a, 4, 4, 5) merge(a, 6, 6, 7) merge(a, 8, 8, 9) merge(a, 10, 10, 11) merge(a, 12, 12, 13) merge(a, 14, 14, 15) sz = 2 merge(a, 0, 1, 3) merge(a, 4, 5, 7) merge(a, 8, 9, 11) merge(a, 12, 13, 15) sz = 4 merge(a, 0, 3, 7) merge(a, 8, 11, 15) sz = 8 merge(a, 0, 7, 15) Trace of merge results for bottom-up mergesort Bottom lne. No recurson needed a[] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 G S O T X A P L G S O T X A P L G S O T X A P L G S O T X A P L G S O T X A P L G S O T X A P L G S O T A X P L G S O T A X P L G S O T A X P L G S O T A X P L G O S T A X P L G O S A T X P L G O S A T X L P G O S A T X L P G O S A L P T X A G L O P S T X 44

Bottom-up mergesort: Java mplementaton Bottom-up mergesort: vsual trace publc class ergebu prvate statc Comparable[] aux; prvate statc vod merge(comparable[] a, nt lo, nt md, nt h) /* as before */ publc statc vod sort(comparable[] a) nt N = a.length; aux = new Comparable[N]; for (nt sz = 1; sz < N; sz = sz+sz) for (nt lo = 0; lo < N-sz; lo += sz+sz) merge(a, lo, lo+sz-1, ath.mn(lo+sz+sz-1, N-1)); Bottom lne. Concse ndustral-strength code, f you have the space. 2 4 8 16 32 45 46