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

An analysis of the quicksort algorithm, including its history, divide and conquer approach, worst-case and best-case running times, and average runtime. It also discusses the concept of randomized quicksort and its advantages.

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-cxr
koofers-user-cxr 🇺🇸

9 documents

1 / 51

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
2/5/08 CS 5633 Analysis of Algorithms 1
CS 5633 -- Spring 2008
Quicksort
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
pf31
pf32
pf33

Partial preview of the text

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

2/5/

CS 5633 Analysis of Algorithms

CS 5633 -- Spring 2008

Quicksort

Carola Wenk

Slides courtesy of Charles Leiserson with small

changes by Carola Wenk

2/5/

CS 5633 Analysis of Algorithms

Quicksort

  • Proposed by C.A.R. Hoare in 1962.• Divide-and-conquer algorithm.• Sorts “in place” (like insertion sort, but not

like merge sort).

  • Very practical (with tuning).

2/5/

CS 5633 Analysis of Algorithms

Running time=

O

n

) for

n

elements. Running time=

O

n

) for

n

elements.

Partitioning subroutine

P

ARTITION

(

A

,

p

,

q

)

A

[

p

..

q

]

x

A

[

p

]

pivot =

A

[

p

]

i

p

for

j

p

  • 1

to

q

do if

A

[

j

]

x

then

i

i

  • 1

exchange

A

[

i

]

A

[

j

]

exchange

A

[

p

]

A

[

i

]

return

i

x x

x

x

x

x

p

i

q

j

Invariant:

CS 5633 Analysis of Algorithms

Example of partitioning

i

j

CS 5633 Analysis of Algorithms

Example of partitioning

i

j

CS 5633 Analysis of Algorithms

Example of partitioning

i

j

CS 5633 Analysis of Algorithms

Example of partitioning

i

j

CS 5633 Analysis of Algorithms

Example of partitioning

i

j

CS 5633 Analysis of Algorithms

Example of partitioning

i

j

CS 5633 Analysis of Algorithms

Example of partitioning

i

j

CS 5633 Analysis of Algorithms

Example of partitioning

i

CS 5633 Analysis of Algorithms

Pseudocode for quicksort

Q

UICKSORT

A

p, r

if

p

r

then

q

P

ARTITION

A

p, r

Q

UICKSORT

A

p, q

Q

UICKSORT

A

q+

, r

Initial call:

Q

UICKSORT

A

, n

2/5/

CS 5633 Analysis of Algorithms

Worst-case ofquicksort

  • Input sorted or reverse sorted.• Partition around min or max element.• One side of partition always has no elements.

2

n

n

n

T

n

n

T

n n T T n T

(arithmetic series)

CS 5633 Analysis of Algorithms

Worst-case recursion tree

T

n

T

T

n

cn