Merge Sort Algorithm Analysis - Prof. Carola Wenk, Study notes of Algorithms and Programming

An analysis of the merge sort algorithm, including its recurrence relation, divide-and-conquer design paradigm, and recursion tree. It also covers the master method for solving recurrences and provides examples of its application.

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-daz-1
koofers-user-daz-1 🇺🇸

9 documents

1 / 48

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1/21/04 CS 5633 Analysis of Algorithms 1
CS 5633 -- Spring 2004
Recurrences and Divide & Conquer
Carola Wenk
Slides courtesy of Charles Leiserson with small
changes by Carola Wenk
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30

Partial preview of the text

Download Merge Sort Algorithm Analysis - Prof. Carola Wenk and more Study notes Algorithms and Programming in PDF only on Docsity!

1/21/

CS 5633 Analysis of Algorithms

CS 5633 -- Spring 2004

Recurrences and Divide & Conquer

Carola Wenk

Slides courtesy of Charles Leiserson with small

changes by Carola Wenk

1/21/

CS 5633 Analysis of Algorithms

Merge sortM

ERGE

-S

ORT

A

[..

n

]

1. If

n

= 1, done.

2. Recursively sort

A

[ 1..

n

^

]

and

A

[^

 n

n

].

Merge

” the 2 sorted lists.

Key subroutine:

M

ERGE

1/21/

CS 5633 Analysis of Algorithms

Merging two sorted arrays

1/21/

CS 5633 Analysis of Algorithms

Merging two sorted arrays

1/21/

CS 5633 Analysis of Algorithms

Merging two sorted arrays

1/21/

CS 5633 Analysis of Algorithms

Merging two sorted arrays

1/21/

CS 5633 Analysis of Algorithms

Merging two sorted arrays

1/21/

CS 5633 Analysis of Algorithms

Merging two sorted arrays

1/21/

CS 5633 Analysis of Algorithms

13

Merging two sorted arrays

1/21/

CS 5633 Analysis of Algorithms

14

Merging two sorted arrays

1/21/

CS 5633 Analysis of Algorithms

16

Analyzing merge sort

M

ERGE

-S

ORT

A

[..

n

]

1. If

n

= 1, done.

2. Recursively sort

A

[ 1..

n

^

]

and

A

[^

 n

n

].

3. “Merge”

the 2 sorted lists

T

( n

d

0

T

( n

dn

Sloppiness:

Should be

T

(^

 n

^

T

(^

 n

^

but it turns out not to matter asymptotically.

1/21/

CS 5633 Analysis of Algorithms

Recurrence for merge sort

T

( n

d

0

if

n

T

( n

dn

if

n

• We shall often omit stating the base case

when

T

( n

(1) for sufficiently small

n

but only when it has no effect on theasymptotic solution to the recurrence.

• But what does

T

( n

) solve to? I.e., is it

O(n) or O(n

2 ) or O(n

3 ) or …?

1/21/

CS 5633 Analysis of Algorithms

Example: merge sort

1. Divide:

Trivial.

2. Conquer:

Recursively sort 2 subarrays.

3. Combine:

Linear-time merge.

T

( n

T

( n

( n

# subproblems

subproblem size

work dividingand combining

1/21/

CS 5633 Analysis of Algorithms

Binary search

Example:

Find 9

Find an element in a sorted array: 1. Divide:

Check middle element.

2. Conquer:

Recursively search 1 subarray.

3. Combine:

Trivial.