Merge Sort Carola Wenk Slides courtesy of Charles Leiserson with - - PowerPoint PPT Presentation

merge sort
SMART_READER_LITE
LIVE PREVIEW

Merge Sort Carola Wenk Slides courtesy of Charles Leiserson with - - PowerPoint PPT Presentation

CS 3343 Fall 2010 Merge Sort Carola Wenk Slides courtesy of Charles Leiserson with small Slides courtesy of Charles Leiserson with small changes by Carola Wenk 9/9/10 CS 3343 Analysis of Algorithms 1 Merge sort Merge sort M ERGE -S ORT


slide-1
SLIDE 1

CS 3343 – Fall 2010

Merge Sort

Carola Wenk Slides courtesy of Charles Leiserson with small

9/9/10 CS 3343 Analysis of Algorithms 1

Slides courtesy of Charles Leiserson with small changes by Carola Wenk

slide-2
SLIDE 2

Merge sort Merge sort

MERGE-SORT (A[1 . . n])

  • 1. If n = 1, done.
  • 2. MERGE-SORT (A[ 1 . . n/2 ])
  • 3. MERGE-SORT (A[ n/2+1 . . n ])

( [ ])

  • 4. “Merge” the 2 sorted lists.

Key subroutine: MERGE

9/9/10 CS 3343 Analysis of Algorithms 2

slide-3
SLIDE 3

Merging two sorted arrays Merging two sorted arrays

20 12 20 13 12 11 7 2 9 1

9/9/10 CS 3343 Analysis of Algorithms 3

slide-4
SLIDE 4

Merging two sorted arrays Merging two sorted arrays

20 12 20 13 12 11 7 2 9 1 1

9/9/10 CS 3343 Analysis of Algorithms 4

slide-5
SLIDE 5

Merging two sorted arrays Merging two sorted arrays

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

9/9/10 CS 3343 Analysis of Algorithms 5

slide-6
SLIDE 6

Merging two sorted arrays Merging two sorted arrays

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

9/9/10 CS 3343 Analysis of Algorithms 6

slide-7
SLIDE 7

Merging two sorted arrays Merging two sorted arrays

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

9/9/10 CS 3343 Analysis of Algorithms 7

slide-8
SLIDE 8

Merging two sorted arrays Merging two sorted arrays

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

9/9/10 CS 3343 Analysis of Algorithms 8

slide-9
SLIDE 9

Merging two sorted arrays Merging two sorted arrays

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

9/9/10 CS 3343 Analysis of Algorithms 9

slide-10
SLIDE 10

Merging two sorted arrays Merging two sorted arrays

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

9/9/10 CS 3343 Analysis of Algorithms 10

slide-11
SLIDE 11

Merging two sorted arrays Merging two sorted arrays

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

9/9/10 CS 3343 Analysis of Algorithms 11

slide-12
SLIDE 12

Merging two sorted arrays Merging two sorted arrays

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

9/9/10 CS 3343 Analysis of Algorithms 12

slide-13
SLIDE 13

Merging two sorted arrays Merging two sorted arrays

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

9/9/10 CS 3343 Analysis of Algorithms 13

slide-14
SLIDE 14

Merging two sorted arrays Merging two sorted arrays

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

9/9/10 CS 3343 Analysis of Algorithms 14

slide-15
SLIDE 15

Merging two sorted arrays Merging two sorted arrays

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

Time dn ∈ Θ(n) to merge a total

  • f n elements (linear time)

9/9/10 CS 3343 Analysis of Algorithms 15

  • f n elements (linear time).
slide-16
SLIDE 16

Analyzing merge sort Analyzing merge sort

MERGE-SORT (A[1 . . n])

  • 1. If n = 1, done.

T(n) d0

  • 2. MERGE-SORT (A[ 1 . . n/2 ])
  • 3. MERGE-SORT (A[ n/2+1 . . n ])

T(n/2) T(n/2)

  • 4. “Merge” the 2 sorted lists.

dn Sloppiness: Should be T( n/2 ) + T( n/2 ) ,

9/9/10 CS 3343 Analysis of Algorithms 16

but it turns out not to matter asymptotically.

slide-17
SLIDE 17

Recurrence for merge sort Recurrence for merge sort

d if n = 1; 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 …?

9/9/10 CS 3343 Analysis of Algorithms 17

( ) ( ) ( )

slide-18
SLIDE 18

Recursion tree Recursion tree

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

9/9/10 CS 3343 Analysis of Algorithms 18

slide-19
SLIDE 19

Recursion tree Recursion tree

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

9/9/10 CS 3343 Analysis of Algorithms 19

slide-20
SLIDE 20

Recursion tree Recursion tree

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

9/9/10 CS 3343 Analysis of Algorithms 20

slide-21
SLIDE 21

Recursion tree Recursion tree

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

9/9/10 CS 3343 Analysis of Algorithms 21

slide-22
SLIDE 22

Recursion tree Recursion tree

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

9/9/10 CS 3343 Analysis of Algorithms 22

slide-23
SLIDE 23

Recursion tree Recursion tree

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

9/9/10 CS 3343 Analysis of Algorithms 23

slide-24
SLIDE 24

Recursion tree Recursion tree

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

9/9/10 CS 3343 Analysis of Algorithms 24

slide-25
SLIDE 25

Recursion tree Recursion tree

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

9/9/10 CS 3343 Analysis of Algorithms 25

slide-26
SLIDE 26

Recursion tree Recursion tree

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

9/9/10 CS 3343 Analysis of Algorithms 26

slide-27
SLIDE 27

Recursion tree Recursion tree

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

9/9/10 CS 3343 Analysis of Algorithms 27

slide-28
SLIDE 28

Recursion tree Recursion tree

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

9/9/10 CS 3343 Analysis of Algorithms 28

Total dn log n + d0n

slide-29
SLIDE 29

Conclusions Conclusions

M i Θ( l ) i

  • 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?) ( y )

9/9/10 CS 3343 Analysis of Algorithms 29

slide-30
SLIDE 30

Recursion-tree method Recursion-tree method

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

recursive execution of an algorithm.

  • The recursion tree method can be unreliable
  • The recursion-tree method can be unreliable,

just like any method that uses ellipses (…).

  • It is good for generating guesses of what the
  • It is good for generating guesses of what the

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

9/9/10 CS 3343 Analysis of Algorithms 30

( )

slide-31
SLIDE 31

Substitution method Substitution method

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

  • 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)

9/9/10 CS 3343 Analysis of Algorithms 31