SlideShare a Scribd company logo
Quicksort and Randomized
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).
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
L1.2
September 21, 2005
Divide and conquer
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.3
Quicksort an n-element array:
1. Divide: Partition the array into two subarrays around a
pivot x such that elements in lower subarray ≤ x ≤
elements in upper subarray.
≤ x x ≥ x
September 21, 2005
2. Conquer: Recursively sort the two subarrays.
3. Combine: Trivial.
Key: Linear-time partitioning subroutine.
Running time
= O(n) for n
elements.
Partitioning subroutine
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.4
PARTITION(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 ?
September 21, 2005
p i j q
Invariant: x ≤ x
Example of partitioning
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.5
6 10 13 5 8 3 2 11
i j
September 21, 2005
https://visualgo.net/en/sorting
Example of partitioning
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.6
6 10 13 5 8 3 2 11
i j
September 21, 2005
Example of partitioning
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.7
6 10 13 5 8 3 2 11
i j
September 21, 2005
Example of partitioning
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.8
6 10 13 5 8 3 2 11
6 5 13 10 8 3 2 11
i j
September 21, 2005
Example of partitioning
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.9
6 10 13 5 8 3 2 11
6 5 13 10 8 3 2 11
i j
September 21, 2005
Example of partitioning
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.10
6 10 13 5 8 3 2 11
6 5 13 10 8 3 2 11
i j
September 21, 2005
Example of partitioning
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.11
6 10 13 5 8 3 2 11
6 5 13 10 8 3 2 11
6 5 3 10 8 13 2 11
i j
September 21, 2005
Example of partitioning
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.12
6 10 13 5 8 3 2 11
6 5 13 10 8 3 2 11
6 5 3 10 8 13 2 11
i j
September 21, 2005
Example of partitioning
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.13
6 10 13 5 8 3 2 11
6 5 13 10 8 3 2 11
6 5 3 10 8 13 2 11
6 5 3 2 8 13 10 11
i j
September 21, 2005
Example of partitioning
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.14
6 10 13 5 8 3 2 11
6 5 13 10 8 3 2 11
6 5 3 10 8 13 2 11
6 5 3 2 8 13 10 11
i j
September 21, 2005
Example of partitioning
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.15
6 10 13 5 8 3 2 11
6 5 13 10 8 3 2 11
6 5 3 10 8 13 2 11
6 5 3 2 8 13 10 11
i j
September 21, 2005
Example of partitioning
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.16
6 10 13 5 8 3 2 11
6 5 13 10 8 3 2 11
6 5 3 10 8 13 2 11
6 5 3 2 8 13 10 11
2 5 3 6 8 13 10 11
i
September 21, 2005
Pseudocode for quicksort
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.17
September 21, 2005
QUICKSORT(A, p, r)
if p < r
then q ← PARTITION(A, p, r)
QUICKSORT(A, p, q–1)
QUICKSORT(A, q+1, r)
Initial call: QUICKSORT(A, 1, n)
Is Quicksort correct?
Assuming Partition is correct
Proof by induction
⚫ Base case: Quicksort works on a list of 1 element
⚫ Inductive case:
⚫ Assume Quicksort sorts arrays for arrays of smaller < n elements, show that it
works to sort n elements
⚫ If partition works correctly then we have:
⚫ and, by our inductive assumption, we have:
A
pivot
≤ pivot > pivot
sorted sorted
Analysis of quicksort
• Assume all input elements are distinct.
• In practice, there are better partitioning algorithms for
when duplicate input elements may exist.
• Let T(n) = worst-case running time on an array of n
elements.
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.19
September 21, 2005
Worst-case of quicksort
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.20
September 21, 2005
= Θ(n2)
• Input sorted or reverse sorted.
• Partition around min or max element.
• One side of partition always has no elements.
T(n) =T(0) +T(n −1) +Θ(n)
= Θ(1)+T(n −1) +Θ(n)
=T(n −1) + Θ(n)
(arithmetic series)
Best-case analysis
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.21
September 21, 2005
If we’re lucky, PARTITION splits the arrayevenly:
T(n) = 2T(n/2) + Θ(n)
= Θ(n lg n) (same as merge sort)
What if the split is always
1
10
:
9
10
?
T(n) = T(
1
10
n)+ T(
9
10
n)+ Θ(n)
What is the solution to this recurrence?
September 21, 2005
Analysis of “almost-best” case
Copyright © 2001-5 by Erik D. Demaine and Charles
E. Leiserson
L4.28
T(n)
Analysis of “almost-best” case
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.23
cn
T( n)
10
1
T( n)
10
9
September 21, 2005
Analysis of “almost-best”
case
cn
10
1 cn
10
9 cn
100
1
T( n)T( n)
100
9
T(100
9
n)T( n)
100
81
September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.24
Analysis of “almost-best”
case
cn
10
1 cn
10
9 cn
1 cn 9 cn
100 100 100
9 cn 100
81 cn
Θ(1)
Θ(1)
10/9
log n
cn
cn
cn
…
O(n) leaves
September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.25
log10
Analysis of “almost-best”
case
cn
10
1 cn
10
9 cn
n 1 cn 9 cn
100 100 100
9 cn 100
81 cn
10/9
log n
cn
cn
cn
…
Θ(1)
cnlog10n ≤ T(n) ≤ cnlog10/9n + Ο(n)
O(n) leaves
Θ(1)
Θ(n lgn)
Lucky!
September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.26
More intuition
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.27
Suppose we alternate lucky, unlucky,
lucky, unlucky, lucky, ….
L(n) = 2U(n/2) + Θ(n)
U(n) = L(n – 1) + Θ(n)
lucky
unlucky
Solving:
L(n) = 2(L(n/2 – 1) + Θ(n/2)) + Θ(n)
= 2L(n/2 – 1) + Θ(n)
= Θ(n lg n)
How can we make sure we are usually lucky?
Lucky!
September 21, 2005
Randomized quicksort
IDEA: Partition around a random element.
• Running time is independent of the input order.
• No assumptions need to be made about the input
distribution.
• No specific input elicits the worst-case behavior.
• The worst case is determined only by the output of a
random-number generator.
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.28
September 21, 2005
Randomized quicksort
analysis
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.29
Let T(n) = the random variable for the running time of
randomized quicksort on an input of size n, assuming
random numbers are independent.
For k = 0, 1, …, n–1, define the indicator
random variable
k
X =
1 if PARTITION generates a k : n–k–1 split,
0 otherwise.
E[Xk] = Pr{Xk = 1} = 1/n, since all splits are equally
likely, assuming elements are distinct.
September 21, 2005
Analysis (continued)
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.30
T(n) =
T(0) + T(n–1) + Θ(n) if 0 : n–1split,
T(1) + T(n–2) + Θ(n) if 1 : n–2split,
…
T(n–1) + T(0) + Θ(n) if n–1 : 0split,
n−1
September 21, 2005
= ∑Xk (T(k) +T(n −k −1) +Θ(n))
k=0
Calculating expectation
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.31
September 21, 2005
Take expectations of both sides.
𝐸 𝑇(𝑛) = 𝐸 ෍
𝑘=0
𝑛−1
𝑋𝑘(𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ(𝑛)
Calculating expectation
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.32
September 21, 2005
Linearity of expectation.
𝐸 𝑇(𝑛) = 𝐸 ෍
𝑘=0
𝑛−1
𝑋𝑘(𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ(𝑛)
= ෍
𝑘=0
𝑛−1
𝐸 𝑋𝑘(𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ(𝑛)
Calculating expectation
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.33
September 21, 2005
Independence of Xk from other random choices.
𝐸 𝑇(𝑛) = 𝐸 ෍
𝑘=0
𝑛−1
𝑋𝑘(𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ(𝑛)
= ෍
𝑘=0
𝑛−1
𝐸 𝑋𝑘(𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ(𝑛)
= ෍
𝑘=0
𝑛−1
𝐸 𝑋𝑘 𝐸 (𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ(𝑛)
Calculating expectation
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.34
September 21, 2005
Linearity of expectation; E[Xk] = 1/n.
𝐸 𝑇(𝑛) = 𝐸 ෍
𝑘=0
𝑛−1
𝑋𝑘(𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ(𝑛)
= ෍
𝑘=0
𝑛−1
𝐸 𝑋𝑘(𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ(𝑛)
= ෍
𝑘=0
𝑛−1
𝐸 𝑋𝑘 𝐸 (𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ(𝑛)
=
1
𝑛
෍
𝑘=0
𝑛−1
𝐸 𝑇 𝑘 +
1
𝑛
෍
𝑘=0
𝑛−1
𝐸 𝑇 𝑛 − 𝑘 − 1 +
1
𝑛
෍
𝑘=0
𝑛−1
𝐸 Θ(𝑛)
Calculating expectation
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.35
September 21, 2005
Summations have
identical terms.
𝐸 𝑇(𝑛) = 𝐸 ෍
𝑘=0
𝑛−1
𝑋𝑘(𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ(𝑛)
= ෍
𝑘=0
𝑛−1
𝐸 𝑋𝑘(𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ(𝑛)
= ෍
𝑘=0
𝑛−1
𝐸 𝑋𝑘 𝐸 (𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ(𝑛)
=
2
𝑛
෍
𝑘=0
𝑛−1
𝐸 𝑇 𝑘 +
1
𝑛
෍
𝑘=0
𝑛−1
𝐸 Θ(𝑛)
=
1
𝑛
෍
𝑘=0
𝑛−1
𝐸 𝑇 𝑘 +
1
𝑛
෍
𝑘=0
𝑛−1
𝐸 𝑇 𝑛 − 𝑘 − 1 +
1
𝑛
෍
𝑘=0
𝑛−1
𝐸 Θ(𝑛)
Hairy recurrence
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.36
September 21, 2005
Use fact: 8
2 2
1 1
2
n−1
∑
k=2
n lg n − n
k lg k≤ (exercise).
(The k = 0, 1 terms can be absorbed in the Θ(n).)
Prove: E[T(n)] ≤ an lgn for constant a > 0.
• Choose a large enough so that anlg n dominates E[T(n)] for
sufficiently large n ≥ 2.
𝐸 𝑇(𝑛) =
2
𝑛
෍
𝑘=2
𝑛−1
𝐸 𝑇 𝑘 +
1
𝑛
෍
𝑘=0
𝑛−1
𝐸 Θ(𝑛)
Substitution method
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.37
September 21, 2005
Substitute inductive hypothesis.
𝐸 𝑇(𝑛) ≤
2
𝑛
෍
𝑘=2
𝑛−1
𝑎∙𝑘∙log 𝑘 +Θ(𝑛)
Substitution method
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.38
September 21, 2005
Use fact.
𝐸 𝑇(𝑛) ≤
2
𝑛
෍
𝑘=2
𝑛−1
𝑎∙𝑘∙log 𝑘 +Θ(𝑛)
≤
2𝑎
𝑛
1
2
𝑛2
log𝑛−
1
8
𝑛2
+Θ(𝑛)
Substitution method
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.39
September 21, 2005
Express as desired – residual.
𝐸 𝑇(𝑛) ≤
2
𝑛
෍
𝑘=2
𝑛−1
𝑎∙𝑘∙log 𝑘 +Θ(𝑛)
≤
2𝑎
𝑛
1
2
𝑛2
log𝑛−
1
8
𝑛2
+Θ(𝑛)
≤𝑎𝑛log𝑛−
𝑎𝑛
4
+Θ(𝑛)
Substitution method
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.40
September 21, 2005
if a is chosen large enough so that an/4 dominates
the Θ(n).
𝐸 𝑇(𝑛) ≤
2
𝑛
෍
𝑘=2
𝑛−1
𝑎∙𝑘∙log 𝑘 +Θ(𝑛)
≤
2𝑎
𝑛
1
2
𝑛2 log𝑛−
1
8
𝑛2 +Θ(𝑛)
≤𝑎𝑛log𝑛−
𝑎𝑛
4
+Θ(𝑛)
≤𝑎𝑛log𝑛
Quicksort in practice
• Quicksort is a great general-purpose sorting
algorithm.
• Quicksort is typically over twice as fast as merge sort.
• Quicksort can benefit substantially from code tuning.
• Quicksort behaves well even with caching and
virtual memory.
Copyright © 2001-5 by Erik D.
Demaine and Charles E. Leiserson
L1.41
September 21, 2005

More Related Content

PDF
Skiena algorithm 2007 lecture09 linear sorting
PPT
lecture 8
PPT
lecture 7
PDF
Skiena algorithm 2007 lecture08 quicksort
PPT
Medians and order statistics
PPT
lecture 10
PPT
lecture7.ppt
Skiena algorithm 2007 lecture09 linear sorting
lecture 8
lecture 7
Skiena algorithm 2007 lecture08 quicksort
Medians and order statistics
lecture 10
lecture7.ppt

Similar to Quicksort in practice___________________ (20)

PDF
Daa notes 2
PPT
Average case Analysis of Quicksort
PDF
Sienna 4 divideandconquer
PPTX
Divide and Conquer - Part 1
PPT
Quick sort Algorithm Discussion And Analysis
PPT
Top school in noida
PPT
Introduction to basic algorithm knowledge.ppt
PDF
Algorithm Homework2
PPT
Randomizing quicksort algorith with example
PPT
lecture8.ppt
PPT
lecture4.ppt
PDF
PDF
Lec3 Algott
PDF
07 Analysis of Algorithms: Order Statistics
PPT
Cis435 week03
PPT
Randomized algorithms ver 1.0
PPTX
CSE680-07QuickSort.pptx
PPTX
Divide and Conquer in DAA concept. For B Tech CSE
PPT
Quick Sort
DOC
pradeepbishtLecture13 div conq
Daa notes 2
Average case Analysis of Quicksort
Sienna 4 divideandconquer
Divide and Conquer - Part 1
Quick sort Algorithm Discussion And Analysis
Top school in noida
Introduction to basic algorithm knowledge.ppt
Algorithm Homework2
Randomizing quicksort algorith with example
lecture8.ppt
lecture4.ppt
Lec3 Algott
07 Analysis of Algorithms: Order Statistics
Cis435 week03
Randomized algorithms ver 1.0
CSE680-07QuickSort.pptx
Divide and Conquer in DAA concept. For B Tech CSE
Quick Sort
pradeepbishtLecture13 div conq
Ad

Recently uploaded (20)

PDF
Sciences of Europe No 170 (2025)
PDF
Placing the Near-Earth Object Impact Probability in Context
PDF
Mastering Bioreactors and Media Sterilization: A Complete Guide to Sterile Fe...
PDF
CAPERS-LRD-z9:AGas-enshroudedLittleRedDotHostingaBroad-lineActive GalacticNuc...
PPTX
EPIDURAL ANESTHESIA ANATOMY AND PHYSIOLOGY.pptx
PDF
Formation of Supersonic Turbulence in the Primordial Star-forming Cloud
PDF
Warm, water-depleted rocky exoplanets with surfaceionic liquids: A proposed c...
PPTX
ognitive-behavioral therapy, mindfulness-based approaches, coping skills trai...
PPTX
ECG_Course_Presentation د.محمد صقران ppt
PPTX
Introduction to Cardiovascular system_structure and functions-1
PDF
An interstellar mission to test astrophysical black holes
PPTX
TOTAL hIP ARTHROPLASTY Presentation.pptx
PDF
Phytochemical Investigation of Miliusa longipes.pdf
PDF
Assessment of environmental effects of quarrying in Kitengela subcountyof Kaj...
PDF
Cosmic Outliers: Low-spin Halos Explain the Abundance, Compactness, and Redsh...
PDF
HPLC-PPT.docx high performance liquid chromatography
PDF
lecture 2026 of Sjogren's syndrome l .pdf
PPTX
Vitamins & Minerals: Complete Guide to Functions, Food Sources, Deficiency Si...
PPTX
7. General Toxicologyfor clinical phrmacy.pptx
PPTX
ANEMIA WITH LEUKOPENIA MDS 07_25.pptx htggtftgt fredrctvg
Sciences of Europe No 170 (2025)
Placing the Near-Earth Object Impact Probability in Context
Mastering Bioreactors and Media Sterilization: A Complete Guide to Sterile Fe...
CAPERS-LRD-z9:AGas-enshroudedLittleRedDotHostingaBroad-lineActive GalacticNuc...
EPIDURAL ANESTHESIA ANATOMY AND PHYSIOLOGY.pptx
Formation of Supersonic Turbulence in the Primordial Star-forming Cloud
Warm, water-depleted rocky exoplanets with surfaceionic liquids: A proposed c...
ognitive-behavioral therapy, mindfulness-based approaches, coping skills trai...
ECG_Course_Presentation د.محمد صقران ppt
Introduction to Cardiovascular system_structure and functions-1
An interstellar mission to test astrophysical black holes
TOTAL hIP ARTHROPLASTY Presentation.pptx
Phytochemical Investigation of Miliusa longipes.pdf
Assessment of environmental effects of quarrying in Kitengela subcountyof Kaj...
Cosmic Outliers: Low-spin Halos Explain the Abundance, Compactness, and Redsh...
HPLC-PPT.docx high performance liquid chromatography
lecture 2026 of Sjogren's syndrome l .pdf
Vitamins & Minerals: Complete Guide to Functions, Food Sources, Deficiency Si...
7. General Toxicologyfor clinical phrmacy.pptx
ANEMIA WITH LEUKOPENIA MDS 07_25.pptx htggtftgt fredrctvg
Ad

Quicksort in practice___________________

  • 2. 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). Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.2 September 21, 2005
  • 3. Divide and conquer Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.3 Quicksort an n-element array: 1. Divide: Partition the array into two subarrays around a pivot x such that elements in lower subarray ≤ x ≤ elements in upper subarray. ≤ x x ≥ x September 21, 2005 2. Conquer: Recursively sort the two subarrays. 3. Combine: Trivial. Key: Linear-time partitioning subroutine.
  • 4. Running time = O(n) for n elements. Partitioning subroutine Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.4 PARTITION(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 ? September 21, 2005 p i j q Invariant: x ≤ x
  • 5. Example of partitioning Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.5 6 10 13 5 8 3 2 11 i j September 21, 2005 https://visualgo.net/en/sorting
  • 6. Example of partitioning Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.6 6 10 13 5 8 3 2 11 i j September 21, 2005
  • 7. Example of partitioning Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.7 6 10 13 5 8 3 2 11 i j September 21, 2005
  • 8. Example of partitioning Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.8 6 10 13 5 8 3 2 11 6 5 13 10 8 3 2 11 i j September 21, 2005
  • 9. Example of partitioning Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.9 6 10 13 5 8 3 2 11 6 5 13 10 8 3 2 11 i j September 21, 2005
  • 10. Example of partitioning Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.10 6 10 13 5 8 3 2 11 6 5 13 10 8 3 2 11 i j September 21, 2005
  • 11. Example of partitioning Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.11 6 10 13 5 8 3 2 11 6 5 13 10 8 3 2 11 6 5 3 10 8 13 2 11 i j September 21, 2005
  • 12. Example of partitioning Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.12 6 10 13 5 8 3 2 11 6 5 13 10 8 3 2 11 6 5 3 10 8 13 2 11 i j September 21, 2005
  • 13. Example of partitioning Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.13 6 10 13 5 8 3 2 11 6 5 13 10 8 3 2 11 6 5 3 10 8 13 2 11 6 5 3 2 8 13 10 11 i j September 21, 2005
  • 14. Example of partitioning Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.14 6 10 13 5 8 3 2 11 6 5 13 10 8 3 2 11 6 5 3 10 8 13 2 11 6 5 3 2 8 13 10 11 i j September 21, 2005
  • 15. Example of partitioning Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.15 6 10 13 5 8 3 2 11 6 5 13 10 8 3 2 11 6 5 3 10 8 13 2 11 6 5 3 2 8 13 10 11 i j September 21, 2005
  • 16. Example of partitioning Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.16 6 10 13 5 8 3 2 11 6 5 13 10 8 3 2 11 6 5 3 10 8 13 2 11 6 5 3 2 8 13 10 11 2 5 3 6 8 13 10 11 i September 21, 2005
  • 17. Pseudocode for quicksort Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.17 September 21, 2005 QUICKSORT(A, p, r) if p < r then q ← PARTITION(A, p, r) QUICKSORT(A, p, q–1) QUICKSORT(A, q+1, r) Initial call: QUICKSORT(A, 1, n)
  • 18. Is Quicksort correct? Assuming Partition is correct Proof by induction ⚫ Base case: Quicksort works on a list of 1 element ⚫ Inductive case: ⚫ Assume Quicksort sorts arrays for arrays of smaller < n elements, show that it works to sort n elements ⚫ If partition works correctly then we have: ⚫ and, by our inductive assumption, we have: A pivot ≤ pivot > pivot sorted sorted
  • 19. Analysis of quicksort • Assume all input elements are distinct. • In practice, there are better partitioning algorithms for when duplicate input elements may exist. • Let T(n) = worst-case running time on an array of n elements. Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.19 September 21, 2005
  • 20. Worst-case of quicksort Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.20 September 21, 2005 = Θ(n2) • Input sorted or reverse sorted. • Partition around min or max element. • One side of partition always has no elements. T(n) =T(0) +T(n −1) +Θ(n) = Θ(1)+T(n −1) +Θ(n) =T(n −1) + Θ(n) (arithmetic series)
  • 21. Best-case analysis Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.21 September 21, 2005 If we’re lucky, PARTITION splits the arrayevenly: T(n) = 2T(n/2) + Θ(n) = Θ(n lg n) (same as merge sort) What if the split is always 1 10 : 9 10 ? T(n) = T( 1 10 n)+ T( 9 10 n)+ Θ(n) What is the solution to this recurrence?
  • 22. September 21, 2005 Analysis of “almost-best” case Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.28 T(n)
  • 23. Analysis of “almost-best” case Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.23 cn T( n) 10 1 T( n) 10 9 September 21, 2005
  • 24. Analysis of “almost-best” case cn 10 1 cn 10 9 cn 100 1 T( n)T( n) 100 9 T(100 9 n)T( n) 100 81 September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.24
  • 25. Analysis of “almost-best” case cn 10 1 cn 10 9 cn 1 cn 9 cn 100 100 100 9 cn 100 81 cn Θ(1) Θ(1) 10/9 log n cn cn cn … O(n) leaves September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.25
  • 26. log10 Analysis of “almost-best” case cn 10 1 cn 10 9 cn n 1 cn 9 cn 100 100 100 9 cn 100 81 cn 10/9 log n cn cn cn … Θ(1) cnlog10n ≤ T(n) ≤ cnlog10/9n + Ο(n) O(n) leaves Θ(1) Θ(n lgn) Lucky! September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.26
  • 27. More intuition Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.27 Suppose we alternate lucky, unlucky, lucky, unlucky, lucky, …. L(n) = 2U(n/2) + Θ(n) U(n) = L(n – 1) + Θ(n) lucky unlucky Solving: L(n) = 2(L(n/2 – 1) + Θ(n/2)) + Θ(n) = 2L(n/2 – 1) + Θ(n) = Θ(n lg n) How can we make sure we are usually lucky? Lucky! September 21, 2005
  • 28. Randomized quicksort IDEA: Partition around a random element. • Running time is independent of the input order. • No assumptions need to be made about the input distribution. • No specific input elicits the worst-case behavior. • The worst case is determined only by the output of a random-number generator. Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.28 September 21, 2005
  • 29. Randomized quicksort analysis Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.29 Let T(n) = the random variable for the running time of randomized quicksort on an input of size n, assuming random numbers are independent. For k = 0, 1, …, n–1, define the indicator random variable k X = 1 if PARTITION generates a k : n–k–1 split, 0 otherwise. E[Xk] = Pr{Xk = 1} = 1/n, since all splits are equally likely, assuming elements are distinct. September 21, 2005
  • 30. Analysis (continued) Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.30 T(n) = T(0) + T(n–1) + Θ(n) if 0 : n–1split, T(1) + T(n–2) + Θ(n) if 1 : n–2split, … T(n–1) + T(0) + Θ(n) if n–1 : 0split, n−1 September 21, 2005 = ∑Xk (T(k) +T(n −k −1) +Θ(n)) k=0
  • 31. Calculating expectation Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.31 September 21, 2005 Take expectations of both sides. 𝐸 𝑇(𝑛) = 𝐸 ෍ 𝑘=0 𝑛−1 𝑋𝑘(𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ(𝑛)
  • 32. Calculating expectation Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.32 September 21, 2005 Linearity of expectation. 𝐸 𝑇(𝑛) = 𝐸 ෍ 𝑘=0 𝑛−1 𝑋𝑘(𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ(𝑛) = ෍ 𝑘=0 𝑛−1 𝐸 𝑋𝑘(𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ(𝑛)
  • 33. Calculating expectation Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.33 September 21, 2005 Independence of Xk from other random choices. 𝐸 𝑇(𝑛) = 𝐸 ෍ 𝑘=0 𝑛−1 𝑋𝑘(𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ(𝑛) = ෍ 𝑘=0 𝑛−1 𝐸 𝑋𝑘(𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ(𝑛) = ෍ 𝑘=0 𝑛−1 𝐸 𝑋𝑘 𝐸 (𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ(𝑛)
  • 34. Calculating expectation Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.34 September 21, 2005 Linearity of expectation; E[Xk] = 1/n. 𝐸 𝑇(𝑛) = 𝐸 ෍ 𝑘=0 𝑛−1 𝑋𝑘(𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ(𝑛) = ෍ 𝑘=0 𝑛−1 𝐸 𝑋𝑘(𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ(𝑛) = ෍ 𝑘=0 𝑛−1 𝐸 𝑋𝑘 𝐸 (𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ(𝑛) = 1 𝑛 ෍ 𝑘=0 𝑛−1 𝐸 𝑇 𝑘 + 1 𝑛 ෍ 𝑘=0 𝑛−1 𝐸 𝑇 𝑛 − 𝑘 − 1 + 1 𝑛 ෍ 𝑘=0 𝑛−1 𝐸 Θ(𝑛)
  • 35. Calculating expectation Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.35 September 21, 2005 Summations have identical terms. 𝐸 𝑇(𝑛) = 𝐸 ෍ 𝑘=0 𝑛−1 𝑋𝑘(𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ(𝑛) = ෍ 𝑘=0 𝑛−1 𝐸 𝑋𝑘(𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ(𝑛) = ෍ 𝑘=0 𝑛−1 𝐸 𝑋𝑘 𝐸 (𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ(𝑛) = 2 𝑛 ෍ 𝑘=0 𝑛−1 𝐸 𝑇 𝑘 + 1 𝑛 ෍ 𝑘=0 𝑛−1 𝐸 Θ(𝑛) = 1 𝑛 ෍ 𝑘=0 𝑛−1 𝐸 𝑇 𝑘 + 1 𝑛 ෍ 𝑘=0 𝑛−1 𝐸 𝑇 𝑛 − 𝑘 − 1 + 1 𝑛 ෍ 𝑘=0 𝑛−1 𝐸 Θ(𝑛)
  • 36. Hairy recurrence Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.36 September 21, 2005 Use fact: 8 2 2 1 1 2 n−1 ∑ k=2 n lg n − n k lg k≤ (exercise). (The k = 0, 1 terms can be absorbed in the Θ(n).) Prove: E[T(n)] ≤ an lgn for constant a > 0. • Choose a large enough so that anlg n dominates E[T(n)] for sufficiently large n ≥ 2. 𝐸 𝑇(𝑛) = 2 𝑛 ෍ 𝑘=2 𝑛−1 𝐸 𝑇 𝑘 + 1 𝑛 ෍ 𝑘=0 𝑛−1 𝐸 Θ(𝑛)
  • 37. Substitution method Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.37 September 21, 2005 Substitute inductive hypothesis. 𝐸 𝑇(𝑛) ≤ 2 𝑛 ෍ 𝑘=2 𝑛−1 𝑎∙𝑘∙log 𝑘 +Θ(𝑛)
  • 38. Substitution method Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.38 September 21, 2005 Use fact. 𝐸 𝑇(𝑛) ≤ 2 𝑛 ෍ 𝑘=2 𝑛−1 𝑎∙𝑘∙log 𝑘 +Θ(𝑛) ≤ 2𝑎 𝑛 1 2 𝑛2 log𝑛− 1 8 𝑛2 +Θ(𝑛)
  • 39. Substitution method Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.39 September 21, 2005 Express as desired – residual. 𝐸 𝑇(𝑛) ≤ 2 𝑛 ෍ 𝑘=2 𝑛−1 𝑎∙𝑘∙log 𝑘 +Θ(𝑛) ≤ 2𝑎 𝑛 1 2 𝑛2 log𝑛− 1 8 𝑛2 +Θ(𝑛) ≤𝑎𝑛log𝑛− 𝑎𝑛 4 +Θ(𝑛)
  • 40. Substitution method Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.40 September 21, 2005 if a is chosen large enough so that an/4 dominates the Θ(n). 𝐸 𝑇(𝑛) ≤ 2 𝑛 ෍ 𝑘=2 𝑛−1 𝑎∙𝑘∙log 𝑘 +Θ(𝑛) ≤ 2𝑎 𝑛 1 2 𝑛2 log𝑛− 1 8 𝑛2 +Θ(𝑛) ≤𝑎𝑛log𝑛− 𝑎𝑛 4 +Θ(𝑛) ≤𝑎𝑛log𝑛
  • 41. Quicksort in practice • Quicksort is a great general-purpose sorting algorithm. • Quicksort is typically over twice as fast as merge sort. • Quicksort can benefit substantially from code tuning. • Quicksort behaves well even with caching and virtual memory. Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L1.41 September 21, 2005