Abstract In-place Merge Demo - DePaul...

37
Algorithms, 4 th Edition · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2011 · February 23, 2012 4:05:53 PM Algorithms F O U R T H E D I T I O N R O B E R T S E D G E W I C K K E V I N W AY N E 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo

Transcript of Abstract In-place Merge Demo - DePaul...

Page 1: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Algorithms, 4th Edition · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2011 · February 23, 2012 4:05:53 PM

AlgorithmsF O U R T H E D I T I O N

R O B E R T S E D G E W I C K K E V I N W A Y N E

2.2 ABSTRACT IN-PLACE MERGE DEMO

click to begin demo

Page 2: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

2

Abstract in-place merge

E E G M R A C E R T

lo mid mid+1 hi

a[]

sorted sorted

Page 3: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

3

Abstract in-place merge

E E G M R A C E R T

lo mid mid+1 hi

a[]

copy to auxiliary array

aux[]

E E G M R A C E R T

Page 4: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

3

Abstract in-place merge

E E G M R A C E R T

lo mid mid+1 hi

a[]

copy to auxiliary array

aux[] E E G M R A C E R T

Page 5: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

4

Abstract in-place merge

E E G M R A C E R Taux[]

a[] E E G M R A C E R T

Page 6: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

5

Abstract in-place merge

E E G M R A C E R Taux[]

i j

a[] E E G M R A C E R T

k

Page 7: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

5

Abstract in-place merge

compare minimum in each subarray

E E G M R A C E R Taux[]

i j

a[] E E G M R A C E R T

k

A

Page 8: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

6

Abstract in-place merge

compare minimum in each subarray

E E G M R A C E R Taux[]

i j

a[] E E G M R A C E R T

k

A

Page 9: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

7

Abstract in-place merge

E E G M R A C E R Taux[]

i j

A E G M R A C E R Ta[]

k

Page 10: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

7

Abstract in-place merge

E E G M R A C E R Taux[]

i j

A E G M R A C E R Ta[]

k

compare minimum in each subarray

C

Page 11: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

8

Abstract in-place merge

E E G M R A C E R Taux[]

i j

A E G M R A C E R Ta[]

k

compare minimum in each subarray

C

Page 12: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

9

Abstract in-place merge

E E G M R A C E R Taux[]

i j

A C G M R A C E R Ta[]

k

Page 13: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

9

Abstract in-place merge

E E G M R A C E R Taux[]

i j

A C G M R A C E R Ta[]

k

compare minimum in each subarray

E

Page 14: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

10

Abstract in-place merge

E E G M R A C E R Taux[]

i j

A C G M R A C E R Ta[]

k

compare minimum in each subarray

E

Page 15: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

11

Abstract in-place merge

E E G M R A C E R Taux[]

i j

A C E M R A C E R Ta[]

k

Page 16: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

11

Abstract in-place merge

E E G M R A C E R Taux[]

i j

A C E M R A C E R Ta[]

k

compare minimum in each subarray

E

Page 17: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

12

Abstract in-place merge

E E G M R A C E R Taux[]

i j

A C E M R A C E R Ta[]

k

compare minimum in each subarray

E

Page 18: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

13

Abstract in-place merge

E E G M R A C E R Taux[]

i j

A C E E R A C E R Ta[]

k

Page 19: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

13

Abstract in-place merge

E E G M R A C E R Taux[]

i j

A C E E R A C E R Ta[]

k

compare minimum in each subarray

E

Page 20: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

14

Abstract in-place merge

E E G M R A C E R Taux[]

i j

A C E E R A C E R Ta[]

k

compare minimum in each subarray

E

Page 21: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

15

Abstract in-place merge

E E G M R A C E R Taux[]

i j

A C E E E A C E R Ta[]

k

Page 22: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

15

Abstract in-place merge

E E G M R A C E R Taux[]

i j

A C E E E A C E R Ta[]

k

compare minimum in each subarray

G

Page 23: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

16

Abstract in-place merge

E E G M R A C E R Taux[]

i j

A C E E E A C E R Ta[]

k

compare minimum in each subarray

G

Page 24: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

17

Abstract in-place merge

E E G M R A C E R Taux[]

i j

A C E E E G C E R Ta[]

k

Page 25: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

17

Abstract in-place merge

E E G M R A C E R Taux[]

i j

A C E E E G C E R Ta[]

k

compare minimum in each subarray

M

Page 26: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

18

Abstract in-place merge

E E G M R A C E R Taux[]

i j

A C E E E G C E R Ta[]

k

compare minimum in each subarray

M

Page 27: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

19

Abstract in-place merge

E E G M R A C E R Taux[]

i j

A C E E E G M E R Ta[]

k

Page 28: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

19

Abstract in-place merge

E E G M R A C E R Taux[]

i j

A C E E E G M E R Ta[]

k

compare minimum in each subarray

R

Page 29: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

20

Abstract in-place merge

E E G M R A C E R Taux[]

i j

A C E E E G M E R Ta[]

k

compare minimum in each subarray

R

Page 30: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

21

Abstract in-place merge

E E G M R A C E R Taux[]

i j

A C E E E G M R R Ta[]

k

Page 31: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

21

Abstract in-place merge

E E G M R A C E R Taux[]

i j

A C E E E G M R R Ta[]

k

one subarray exhausted, take from other

R

Page 32: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

22

Abstract in-place merge

E E G M R A C E R Taux[]

i j

A C E E E G M R R Ta[]

k

one subarray exhausted, take from other

R

Page 33: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

23

Abstract in-place merge

E E G M R A C E R Taux[]

i j

A C E E E G M R R Ta[]

k

Page 34: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

23

Abstract in-place merge

E E G M R A C E R Taux[]

i j

A C E E E G M R R Ta[]

k

one subarray exhausted, take from other

T

Page 35: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

24

Abstract in-place merge

E E G M R A C E R Taux[]

i j

A C E E E G M R R Ta[]

k

one subarray exhausted, take from other

T

Page 36: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

25

Abstract in-place merge

E E G M R A C E R T

both subarrays exhausted, done

A C E E E G M R R Ta[]

aux[]

i j

k

Page 37: Abstract In-place Merge Demo - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/22DemoMerge.pdf · 2.2 ABSTRACT IN-PLACE MERGE DEMO click to begin demo. Goal. Given two

Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].

26

Abstract in-place merge

A C E E E G M R R Ta[]

sorted

lo hi