Merge sort M ERGE -S ORT A [1 . . n ] 1. If n = 1, done. 2. - - PowerPoint PPT Presentation

merge sort
SMART_READER_LITE
LIVE PREVIEW

Merge sort M ERGE -S ORT A [1 . . n ] 1. If n = 1, done. 2. - - PowerPoint PPT Presentation

CS 3343 -- Spring 2009 Merge sort M ERGE -S ORT A [1 . . n ] 1. If n = 1, done. 2. Recursively sort A [ 1 . . n /2 ] and A [ n /2 +1 . . n ] . 3. Merge the 2 sorted lists. Merge Sort Key subroutine: M ERGE Carola Wenk


slide-1
SLIDE 1

1

1/27/09 CS 3343 Analysis of Algorithms 1

CS 3343 -- Spring 2009

Merge Sort

Carola Wenk Slides courtesy of Charles Leiserson with small changes by Carola Wenk

1/27/09 CS 3343 Analysis of Algorithms 2

Merge sort

MERGE-SORT A[1 . . n]

  • 1. If n = 1, done.
  • 2. Recursively sort A[ 1 . . n/2 ]

and A[ n/2+1 . . n ] .

  • 3. “Merge” the 2 sorted lists.

Key subroutine: MERGE

1/27/09 CS 3343 Analysis of Algorithms 3

Merging two sorted arrays

20 13 7 2 12 11 9 1

1/27/09 CS 3343 Analysis of Algorithms 4

Merging two sorted arrays

20 13 7 2 12 11 9 1 1

slide-2
SLIDE 2

2

1/27/09 CS 3343 Analysis of Algorithms 5

Merging two sorted arrays

20 13 7 2 12 11 9 1 1 20 13 7 2 12 11 9

1/27/09 CS 3343 Analysis of Algorithms 6

Merging two sorted arrays

20 13 7 2 12 11 9 1 1 20 13 7 2 12 11 9 2

1/27/09 CS 3343 Analysis of Algorithms 7

Merging two sorted arrays

20 13 7 2 12 11 9 1 1 20 13 7 2 12 11 9 2 20 13 7 12 11 9

1/27/09 CS 3343 Analysis of Algorithms 8

Merging two sorted arrays

20 13 7 2 12 11 9 1 1 20 13 7 2 12 11 9 2 20 13 7 12 11 9 7

slide-3
SLIDE 3

3

1/27/09 CS 3343 Analysis of Algorithms 9

Merging two sorted arrays

20 13 7 2 12 11 9 1 1 20 13 7 2 12 11 9 2 20 13 7 12 11 9 7 20 13 12 11 9

1/27/09 CS 3343 Analysis of Algorithms 10

Merging two sorted arrays

20 13 7 2 12 11 9 1 1 20 13 7 2 12 11 9 2 20 13 7 12 11 9 7 20 13 12 11 9 9

1/27/09 CS 3343 Analysis of Algorithms 11

Merging two sorted arrays

20 13 7 2 12 11 9 1 1 20 13 7 2 12 11 9 2 20 13 7 12 11 9 7 20 13 12 11 9 9 20 13 12 11

1/27/09 CS 3343 Analysis of Algorithms 12

Merging two sorted arrays

20 13 7 2 12 11 9 1 1 20 13 7 2 12 11 9 2 20 13 7 12 11 9 7 20 13 12 11 9 9 20 13 12 11 11

slide-4
SLIDE 4

4

1/27/09 CS 3343 Analysis of Algorithms 13

Merging two sorted arrays

20 13 7 2 12 11 9 1 1 20 13 7 2 12 11 9 2 20 13 7 12 11 9 7 20 13 12 11 9 9 20 13 12 11 11 20 13 12

1/27/09 CS 3343 Analysis of Algorithms 14

Merging two sorted arrays

20 13 7 2 12 11 9 1 1 20 13 7 2 12 11 9 2 20 13 7 12 11 9 7 20 13 12 11 9 9 20 13 12 11 11 20 13 12 12

1/27/09 CS 3343 Analysis of Algorithms 15

Merging two sorted arrays

20 13 7 2 12 11 9 1 1 20 13 7 2 12 11 9 2 20 13 7 12 11 9 7 20 13 12 11 9 9 20 13 12 11 11 20 13 12 12

Time dn ∈ Θ(n) to merge a total

  • f n elements (linear time).

1/27/09 CS 3343 Analysis of Algorithms 16

Analyzing merge sort

MERGE-SORT A[1 . . n]

  • 1. If n = 1, done.
  • 2. Recursively sort A[ 1 . . n/2 ]

and A[ n/2+1 . . n ] .

  • 3. “Merge” the 2 sorted lists

T(n) d0 2T(n/2) dn Sloppiness: Should be T( n/2 ) + T( n/2 ) , but it turns out not to matter asymptotically.

slide-5
SLIDE 5

5

1/27/09 CS 3343 Analysis of Algorithms 17

Recurrence for merge sort

T(n) = d0 if n = 1; 2T(n/2) + dn if n > 1.

  • But what does T(n) solve to? I.e., is it

O(n) or O(n2) or O(n3) or …?

1/27/09 CS 3343 Analysis of Algorithms 18

Recursion tree

Solve T(n) = 2T(n/2) + dn, where d > 0 is constant.

1/27/09 CS 3343 Analysis of Algorithms 19

Recursion tree

Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. T(n)

1/27/09 CS 3343 Analysis of Algorithms 20

Recursion tree

Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. T(n/2) T(n/2) dn

slide-6
SLIDE 6

6

1/27/09 CS 3343 Analysis of Algorithms 21

Recursion tree

Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn T(n/4) T(n/4) T(n/4) T(n/4) dn/2 dn/2

1/27/09 CS 3343 Analysis of Algorithms 22

Recursion tree

Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn dn/4 dn/4 dn/4 dn/4 dn/2 dn/2 d0 …

1/27/09 CS 3343 Analysis of Algorithms 23

Recursion tree

Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn dn/4 dn/4 dn/4 dn/4 dn/2 dn/2 d0 … h = log n

1/27/09 CS 3343 Analysis of Algorithms 24

Recursion tree

Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn dn/4 dn/4 dn/4 dn/4 dn/2 dn/2 d0 … h = log n dn

slide-7
SLIDE 7

7

1/27/09 CS 3343 Analysis of Algorithms 25

Recursion tree

Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn dn/4 dn/4 dn/4 dn/4 dn/2 dn/2 d0 … h = log n dn dn

1/27/09 CS 3343 Analysis of Algorithms 26

Recursion tree

Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn dn/4 dn/4 dn/4 dn/4 dn/2 dn/2 d0 … h = log n dn dn dn …

1/27/09 CS 3343 Analysis of Algorithms 27

Recursion tree

Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn dn/4 dn/4 dn/4 dn/4 dn/2 dn/2 d0 … h = log n dn dn dn #leaves = n d0n …

1/27/09 CS 3343 Analysis of Algorithms 28

Recursion tree

Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn dn/4 dn/4 dn/4 dn/4 dn/2 dn/2 d0 … h = log n dn dn dn #leaves = n d0n Total n log n + d0n …

slide-8
SLIDE 8

8

1/27/09 CS 3343 Analysis of Algorithms 29

Conclusions

  • Merge sort runs in Θ(n log n) time.
  • Θ(n log n) grows more slowly than Θ(n2).
  • Therefore, merge sort asymptotically beats

insertion sort in the worst case.

  • In practice, merge sort beats insertion sort

for n > 30 or so. (Why not earlier?)

1/27/09 CS 3343 Analysis of Algorithms 30

Recursion-tree method

  • A recursion tree models the costs (time) of a

recursive execution of an algorithm.

  • The recursion-tree method can be unreliable,

just like any method that uses ellipses (…).

  • It is good for generating guesses of what the

runtime could be. But: Need to verify that the guess is right. → Induction (substitution method)

1/27/09 CS 3343 Analysis of Algorithms 31

Substitution method

  • 1. Guess the form of the solution:

(e.g. using recursion trees, or expansion)

  • 2. Verify by induction (inductive step).
  • 3. Solve for O-constants n0 and c (base case of

induction) The most general method to solve a recurrence (prove O and Ω separately):