quicksort analysis of algorithms1. quicksort – two partioning algorithms analysis of algorithms2

47
Quicksort Analysis of Algorithms 1

Upload: donna-oconnor

Post on 18-Dec-2015

235 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Quicksort

Analysis of Algorithms 1

Page 2: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Quicksort – Two Partioning Algorithms

Analysis of Algorithms 2

Page 3: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Hoares’ Partitioning Algorithm

Analysis of Algorithms 3

Page 4: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Quicksort

Analysis of Algorithms 4

Page 5: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Hoare’s Partitioning Algorithm

Analysis of Algorithms 5

Page 6: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Hoare’s Partitioning Algorithm

Analysis of Algorithms 6

Page 7: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Hoare’s Partitioning Algorithm

Analysis of Algorithms 7

Page 8: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Hoare’s Partitioning Algorithm - Ex1 (pivot=5)

Analysis of Algorithms 8

Page 9: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Hoare’s Partitioning Algorithm – Ex2 (pivot=5)

Analysis of Algorithms 9

Page 10: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Correctness of Hoare’s Partitioning Algorithm

Analysis of Algorithms 10

Page 11: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Correctness of Hoare’s Partitioning Algorithm

Analysis of Algorithms 11

Page 12: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Correctness of Hoare’s Partitioning Algorithm

Analysis of Algorithms 12

Page 13: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Correctness of Hoare’s Partitioning Algorithm

Analysis of Algorithms 13

Page 14: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Correctness of Hoare’s Partitioning Algorithm

Analysis of Algorithms 14

Page 15: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Correctness of Hoare’s Partitioning Algorithm

Analysis of Algorithms 15

Page 16: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Correctness of Hoare’s Partitioning Algorithm

Analysis of Algorithms 16

Page 17: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Lomuto’s Partitioning Algorithm

Analysis of Algorithms 17

Page 18: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Quiksort – Lumuto’s Algorithm

Analysis of Algorithms 18

Page 19: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Lomuto’s Algorithm: Example (pivot = 4)

Analysis of Algorithms 19

Page 20: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Lomuto’s Algorithm: Example (pivot = 4)

Analysis of Algorithms 20

Page 21: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Lomuto’s Algorithm: Example (pivot = 4)

Analysis of Algorithms 21

Page 22: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Comparison of Hoare’s & Lomuto’s Algorithms

Analysis of Algorithms 22

Page 23: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Comparison of Hoare’s & Lomuto’s Algorithms

Analysis of Algorithms 23

Page 24: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Comparison of Hoare’s & Lomuto’s Algorithms

Analysis of Algorithms 24

Page 25: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Analysis of Quicksort

Analysis of Algorithms 25

Page 26: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Worst-case of quicksort

Analysis of Algorithms 26

Page 27: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Worst-case recursion tree

Analysis of Algorithms 27

Page 28: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Best-case analysis

Analysis of Algorithms 28

Page 29: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Analysis of “almost-best” case

Analysis of Algorithms 29

Page 30: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Balanced Partitionings:Splits of constant proportionality

Analysis of Algorithms 30

Page 31: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Analysis of Algorithms 31

Page 32: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Balanced Partitionings

Analysis of Algorithms 32

Page 33: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Intuition for the average case

Analysis of Algorithms 33

Page 34: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Intuition for the average case

Analysis of Algorithms 34

Page 35: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Intuition for the average case

Analysis of Algorithms 35

Page 36: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Randomized Quicksort

Analysis of Algorithms 36

• Average-case assumption:– all permutations are equally likely– cannot always expect to hold

• Alternative to assuming a distribution: Impose a distribution–Partition around a random pivot

• Typically useful when– there are many ways that an algorithm can proceed– but, it is difficult to determine a way that is guaranteed to be good.– Many good alternatives; simply choose one randomly

• Running time is independent of input ordering• No specific input causes worst-case behavior• Worst case determined only by output of random number generator

Page 37: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Randomized Quicksort

Analysis of Algorithms 37

Page 38: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Formal Average - Case Analysis

Analysis of Algorithms 38

Page 39: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Likelihood of Various Outcomes ofHoare’s Partitioning Algorithm

Analysis of Algorithms 39

Page 40: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Various Outcomes

Analysis of Algorithms 40

Page 41: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Average - Case Analysis: Recurrence

Analysis of Algorithms 41

Page 42: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Average - Case Analysis: Recurrence

Analysis of Algorithms 42

Page 43: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Solving Recurrence: Substitution

Analysis of Algorithms 43

Page 44: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Tight bound for Σ klgk

Analysis of Algorithms 44

Page 45: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Tight bound for Σ klgk

Analysis of Algorithms 45

Page 46: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Analysis of Algorithms 46

Page 47: Quicksort Analysis of Algorithms1. Quicksort – Two Partioning Algorithms Analysis of Algorithms2

Analysis of Algorithms 47