Pivot Sampling in Dual-Pivot Quicksort Sebastian Wild Markus E. Nebel [wild, nebel] @cs.uni-kl.de 16 June 2014 25th International Conference on Probabilistic, Combinatorial and Asymptotic Methods for the Analysis of Algorithms Sebastian Wild Dual-Pivot Quicksort 2014-06-16 1 / 16
120
Embed
Pivot Sampling in Dual-Pivot Quicksort - Aofa 2014 · 2014. 6. 21. · Pivot Sampling in Dual-Pivot Quicksort Sebastian Wild Markus E. Nebel [wild,nebel]@cs.uni-kl.de 16 June 2014
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Pivot Sampling in Dual-Pivot Quicksort
Sebastian Wild Markus E. Nebel[wild, nebel] @cs.uni-kl.de
16 June 2014
25th International Conference on Probabilistic,
Combinatorial and Asymptotic Methods for the
Analysis of Algorithms
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 1 / 16
Sorting History
Almost identical Quicksort in all programming libraries!
2009: Java switches to Yaroslavskiy’s algorithm
Dual-Pivot Quicksort with new partitioning methodFaster running timeNo proper analysis at that time
Why not earlier?
Other dual-pivot variants had been studiedCould not beat classic Quicksort
Prehistory Age of classic Quicksort Dual-Pivot Era
Invention of Quicksort Dual-Pivot Quicksort in Java
1969 1975 ’78 1993 19971961’62 ’77
today2009’11
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 2 / 16
Sorting History
Almost identical Quicksort in all programming libraries!
2009: Java switches to Yaroslavskiy’s algorithm
Dual-Pivot Quicksort with new partitioning methodFaster running timeNo proper analysis at that time
Why not earlier?
Other dual-pivot variants had been studiedCould not beat classic Quicksort
Prehistory Age of classic Quicksort Dual-Pivot Era
Invention of Quicksort Dual-Pivot Quicksort in Java
1969 1975 ’78 1993 19971961’62 ’77
today2009’11
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 2 / 16
Sorting History
Almost identical Quicksort in all programming libraries!
2009: Java switches to Yaroslavskiy’s algorithm
Dual-Pivot Quicksort with new partitioning methodFaster running timeNo proper analysis at that time
Why not earlier?
Other dual-pivot variants had been studiedCould not beat classic Quicksort
Prehistory Age of classic Quicksort Dual-Pivot Era
Invention of Quicksort Dual-Pivot Quicksort in Java
1969 1975 ’78 1993 19971961’62 ’77
today2009’11
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 2 / 16
Sorting History
Almost identical Quicksort in all programming libraries!
2009: Java switches to Yaroslavskiy’s algorithm
Dual-Pivot Quicksort with new partitioning methodFaster running timeNo proper analysis at that time
Why not earlier?
Other dual-pivot variants had been studiedCould not beat classic Quicksort
Prehistory Age of classic Quicksort Dual-Pivot Era
Invention of Quicksort Dual-Pivot Quicksort in Java
1969 1975 ’78 1993 19971961’62 ’77
today2009’11
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 2 / 16
Previous Results (Average Case)
Classic Dual-Pivot Relative
Running Time (from various experiments) −10±2%
Comparisons 2n lnn 1.9n lnn −5%
Swaps 0.3n lnn 0.6n lnn +80%
Bytecode Instructions 18n lnn 21.7n lnn +20.6%
MMIX oops υ 11n lnn 13.1n lnn +19.1%
MMIX mems µ 2.6n lnn 2.8n lnn +5%
fresh elements (≈ cache misses) 2n lnn 1.6n lnn −20%
+O(n) , average case results
Results for pivots at fixed positions.
. . . nobody uses that!
Here: Choosing pivots from sample.
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 3 / 16
Previous Results (Average Case)
Classic Dual-Pivot Relative
Running Time (from various experiments) −10±2%
Comparisons 2n lnn 1.9n lnn −5%
Swaps 0.3n lnn 0.6n lnn +80%
Bytecode Instructions 18n lnn 21.7n lnn +20.6%
MMIX oops υ 11n lnn 13.1n lnn +19.1%
MMIX mems µ 2.6n lnn 2.8n lnn +5%
fresh elements (≈ cache misses) 2n lnn 1.6n lnn −20%
+O(n) , average case results
Results for pivots at fixed positions.
. . . nobody uses that!
Here: Choosing pivots from sample.
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 3 / 16
Previous Results (Average Case)
Classic Dual-Pivot Relative
Running Time (from various experiments) −10±2%
Comparisons 2n lnn 1.9n lnn −5%
Swaps 0.3n lnn 0.6n lnn +80%
Bytecode Instructions 18n lnn 21.7n lnn +20.6%
MMIX oops υ 11n lnn 13.1n lnn +19.1%
MMIX mems µ 2.6n lnn 2.8n lnn +5%
fresh elements (≈ cache misses) 2n lnn 1.6n lnn −20%
+O(n) , average case results
Results for pivots at fixed positions.
. . . nobody uses that!
Here: Choosing pivots from sample.
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 3 / 16
Previous Results (Average Case)
Classic Dual-Pivot Relative
Running Time (from various experiments) −10±2%
Comparisons 2n lnn 1.9n lnn −5%
Swaps 0.3n lnn 0.6n lnn +80%
Bytecode Instructions 18n lnn 21.7n lnn +20.6%
MMIX oops υ 11n lnn 13.1n lnn +19.1%
MMIX mems µ 2.6n lnn 2.8n lnn +5%
fresh elements (≈ cache misses) 2n lnn 1.6n lnn −20%
+O(n) , average case results
Results for pivots at fixed positions.
. . . nobody uses that!
Here: Choosing pivots from sample.
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 3 / 16
Previous Results (Average Case)
Classic Dual-Pivot Relative
Running Time (from various experiments) −10±2%
Comparisons 2n lnn 1.9n lnn −5%
Swaps 0.3n lnn 0.6n lnn +80%
Bytecode Instructions 18n lnn 21.7n lnn +20.6%
MMIX oops υ 11n lnn 13.1n lnn +19.1%
MMIX mems µ 2.6n lnn 2.8n lnn +5%
fresh elements (≈ cache misses) 2n lnn 1.6n lnn −20%
+O(n) , average case results
Results for pivots at fixed positions. . . . nobody uses that!
Here: Choosing pivots from sample.
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 3 / 16
Previous Results (Average Case)
Classic Dual-Pivot Relative
Running Time (from various experiments) −10±2%
Comparisons 2n lnn 1.9n lnn −5%
Swaps 0.3n lnn 0.6n lnn +80%
Bytecode Instructions 18n lnn 21.7n lnn +20.6%
MMIX oops υ 11n lnn 13.1n lnn +19.1%
MMIX mems µ 2.6n lnn 2.8n lnn +5%
fresh elements (≈ cache misses) 2n lnn 1.6n lnn −20%
+O(n) , average case results
Results for pivots at fixed positions. . . . nobody uses that!
Here: Choosing pivots from sample.
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 3 / 16
Dual Pivot Quicksort
Algorithm (Conceptual View)1 Choose two pivots P 6 Q2 For each element x, determine its class
small for x < P
medium for P < x < Q
large for Q < x
by comparing x to pivots P and Q
3 Arrange elements according to classes:
P Q
4 Sort subarrays recursively.
How to implement 3 efficiently on arrays?
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 4 / 16
Dual Pivot Quicksort
Algorithm (Conceptual View)1 Choose two pivots P 6 Q2 For each element x, determine its class
small for x < P
medium for P < x < Q
large for Q < x
by comparing x to pivots P and Q
3 Arrange elements according to classes:
P Q
4 Sort subarrays recursively.
How to implement 3 efficiently on arrays?
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 4 / 16
Dual Pivot Quicksort
Algorithm (Conceptual View)1 Choose two pivots P 6 Q2 For each element x, determine its class
small for x < P
medium for P < x < Q
large for Q < x
by comparing x to pivots P and Q
3 Arrange elements according to classes:
P Q
4 Sort subarrays recursively.
How to implement 3 efficiently on arrays?
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 4 / 16
Dual Pivot Quicksort
Algorithm (Conceptual View)1 Choose two pivots P 6 Q2 For each element x, determine its class
small for x < P
medium for P < x < Q
large for Q < x
by comparing x to pivots P and Q
3 Arrange elements according to classes:
P Q
4 Sort subarrays recursively.
How to implement 3 efficiently on arrays?
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 4 / 16
Yaroslavskiy’s Algorithm
< P ?
swap ` < Q ?
skip swap g
3 7
3 7
> Q ?
< P ? skip
swap ` swap k
37
3 7
P Q
3 5 1 7 4 2 8 6
gk̀
P QInvariant:
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 5 / 16
Yaroslavskiy’s Algorithm
< P ?
swap ` < Q ?
skip swap g
3 7
3 7
> Q ?
< P ? skip
swap ` swap k
37
3 7
P Q
3 5 1 7 4 2 8 6
gk̀
P QInvariant:
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 5 / 16
Yaroslavskiy’s Algorithm
< P ?
swap ` < Q ?
skip swap g
3 7
3 7
> Q ?
< P ? skip
swap ` swap k
37
3 7
P Q
3 5 1 7 4 2 8 6
gk̀
P QInvariant:
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 5 / 16
Yaroslavskiy’s Algorithm
< P ?
swap ` < Q ?
skip swap g
3 7
3 7
> Q ?
< P ? skip
swap ` swap k
37
3 7
P Q
3 5 1 7 4 2 8 6
gk̀
P QInvariant:
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 5 / 16
Yaroslavskiy’s Algorithm
< P ?
swap ` < Q ?
skip swap g
3 7
3 7
> Q ?
< P ? skip
swap ` swap k
37
3 7
P Q
3 5 1 7 4 2 8 6
g` k
P≤◦≤QP QInvariant:
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 5 / 16
Yaroslavskiy’s Algorithm
< P ?
swap ` < Q ?
skip swap g
3 7
3 7
> Q ?
< P ? skip
swap ` swap k
37
3 7
P Q
3 5 1 7 4 2 8 6
g` k
P≤◦≤QP QInvariant:
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 5 / 16
Yaroslavskiy’s Algorithm
< P ?
swap ` < Q ?
skip swap g
3 7
3 7
> Q ?
< P ? skip
swap ` swap k
37
3 7
P Q
3 1 5 7 4 2 8 6
g` k
P≤◦≤QP QInvariant:
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 5 / 16
Yaroslavskiy’s Algorithm
< P ?
swap ` < Q ?
skip swap g
3 7
3 7
> Q ?
< P ? skip
swap ` swap k
37
3 7
P Q
3 1 5 7 4 2 8 6
g` k
< P P≤◦≤QP QInvariant:
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 5 / 16
Yaroslavskiy’s Algorithm
< P ?
swap ` < Q ?
skip swap g
3 7
3 7
> Q ?
< P ? skip
swap ` swap k
37
3 7
P Q
3 1 5 7 4 2 8 6
g` k
< P P≤◦≤QP QInvariant:
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 5 / 16
Yaroslavskiy’s Algorithm
< P ?
swap ` < Q ?
skip swap g
3 7
3 7
> Q ?
< P ? skip
swap ` swap k
37
3 7
P Q
3 1 5 7 4 2 8 6
g` k
< P P≤◦≤QP QInvariant:
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 5 / 16
Yaroslavskiy’s Algorithm
< P ?
swap ` < Q ?
skip swap g
3 7
3 7
> Q ?
< P ? skip
swap ` swap k
37
3 7
P Q
3 1 5 7 4 2 8 6
g` k
< P P≤◦≤QP QInvariant:
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 5 / 16
Yaroslavskiy’s Algorithm
< P ?
swap ` < Q ?
skip swap g
3 7
3 7
> Q ?
< P ? skip
swap ` swap k
37
3 7
P Q
3 1 5 7 4 2 8 6
g` k
< P P≤◦≤QP QInvariant:
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 5 / 16
Yaroslavskiy’s Algorithm
< P ?
swap ` < Q ?
skip swap g
3 7
3 7
> Q ?
< P ? skip
swap ` swap k
37
3 7
P Q
3 1 5 7 4 2 8 6
g` k
< P P≤◦≤Q ≥ QP QInvariant:
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 5 / 16
Yaroslavskiy’s Algorithm
< P ?
swap ` < Q ?
skip swap g
3 7
3 7
> Q ?
< P ? skip
swap ` swap k
37
3 7
P Q
3 1 5 7 4 2 8 6
g` k
< P P≤◦≤Q ≥ QP QInvariant:
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 5 / 16
Yaroslavskiy’s Algorithm
< P ?
swap ` < Q ?
skip swap g
3 7
3 7
> Q ?
< P ? skip
swap ` swap k
37
3 7
P Q
3 1 5 7 4 2 8 6
g` k
< P P≤◦≤Q ≥ QP QInvariant:
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 5 / 16
Yaroslavskiy’s Algorithm
< P ?
swap ` < Q ?
skip swap g
3 7
3 7
> Q ?
< P ? skip
swap ` swap k
37
3 7
P Q
3 1 2 5 4 7 8 6
g` k
< P P≤◦≤Q ≥ QP QInvariant:
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 5 / 16
Yaroslavskiy’s Algorithm
< P ?
swap ` < Q ?
skip swap g
3 7
3 7
> Q ?
< P ? skip
swap ` swap k
37
3 7
P Q
3 1 2 5 4 7 8 6
g` k
< P P≤◦≤Q ≥ QP QInvariant:
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 5 / 16
Yaroslavskiy’s Algorithm
< P ?
swap ` < Q ?
skip swap g
3 7
3 7
> Q ?
< P ? skip
swap ` swap k
37
3 7
P Q
3 1 2 5 4 7 8 6
g` k
< P P≤◦≤Q ≥ QP QInvariant:
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 5 / 16
Yaroslavskiy’s Algorithm
< P ?
swap ` < Q ?
skip swap g
3 7
3 7
> Q ?
< P ? skip
swap ` swap k
37
3 7
P Q
3 1 2 5 4 7 8 6
g` k
< P P≤◦≤Q ≥ QP QInvariant:
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 5 / 16
Yaroslavskiy’s Algorithm
< P ?
swap ` < Q ?
skip swap g
3 7
3 7
> Q ?
< P ? skip
swap ` swap k
37
3 7
P Q
3 1 2 5 4 7 8 6
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 5 / 16
Yaroslavskiy’s Algorithm
< P ?
swap ` < Q ?
skip swap g
3 7
3 7
> Q ?
< P ? skip
swap ` swap k
37
3 7
P Q
2 1 3 5 4 6 8 7
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 5 / 16
Yaroslavskiy’s Algorithm
< P ?
swap ` < Q ?
skip swap g
3 7
3 7
> Q ?
< P ? skip
swap ` swap k
37
3 7
P Q
1 2 3 4 5 6 7 8
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 5 / 16
Pivot Sampling
Classic Quicksort: pivot as median of k(in practice: k = 3, pseudomedian of 9)
We need two pivots.
Here: parametric scheme with parameter t = (t1, t2, t3)
1 Sample k = t1 + t2 + t3 + 2 elements2 Sort the sample3 Select pivots s. t. in sorted sample
t1 smaller ,
t2 between and
t3 larger than pivots
Example withk = 8 and t = (3, 2, 1):
P Q
t1 t2 t3
ALENEX 2013:Empirical evidence that asymmetric t = (0, 1, 2) beats t = (1, 1, 1)!
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 6 / 16
Pivot Sampling
Classic Quicksort: pivot as median of k(in practice: k = 3, pseudomedian of 9)
We need two pivots.
Here: parametric scheme with parameter t = (t1, t2, t3)
1 Sample k = t1 + t2 + t3 + 2 elements2 Sort the sample3 Select pivots s. t. in sorted sample
t1 smaller ,
t2 between and
t3 larger than pivots
Example withk = 8 and t = (3, 2, 1):
P Q
t1 t2 t3
ALENEX 2013:Empirical evidence that asymmetric t = (0, 1, 2) beats t = (1, 1, 1)!
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 6 / 16
Pivot Sampling
Classic Quicksort: pivot as median of k(in practice: k = 3, pseudomedian of 9)
We need two pivots.
Here: parametric scheme with parameter t = (t1, t2, t3)
1 Sample k = t1 + t2 + t3 + 2 elements2 Sort the sample3 Select pivots s. t. in sorted sample
t1 smaller ,
t2 between and
t3 larger than pivots
Example withk = 8 and t = (3, 2, 1):
P Q
t1 t2 t3
ALENEX 2013:Empirical evidence that asymmetric t = (0, 1, 2) beats t = (1, 1, 1)!
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 6 / 16
Pivot Sampling
Classic Quicksort: pivot as median of k(in practice: k = 3, pseudomedian of 9)
We need two pivots.
Here: parametric scheme with parameter t = (t1, t2, t3)
1 Sample k = t1 + t2 + t3 + 2 elements2 Sort the sample3 Select pivots s. t. in sorted sample
t1 smaller ,
t2 between and
t3 larger than pivots
Example withk = 8 and t = (3, 2, 1):
P Q
t1 t2 t3
ALENEX 2013:Empirical evidence that asymmetric t = (0, 1, 2) beats t = (1, 1, 1)!
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 6 / 16
Expected Number of Comparisons
Goal: Count comparisons in Dual-Pivot Quicksort with Pivot Sampling
Cn: (random!) #cmps to sort n i. i. d. Uniform(0, 1) elements
Distributional Recurrence:
CnD= Tn + CJ1 + CJ2 + CJ3
J = (J1, J2, J3): (random) subproblem sizes left medium rightP Q
J1 J2 J3Tn: (random) #cmps of first partitioning step
Taking expectations and conditioning on J:
E[Cn] = E[Tn] +n−2∑j=0
wn,j E[Cj]
with wn,j = Pr[J1 = j] + Pr[J2 = j] + Pr[J3 = j]
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 7 / 16
Expected Number of Comparisons
Goal: Count comparisons in Dual-Pivot Quicksort with Pivot Sampling
Cn: (random!) #cmps to sort n i. i. d. Uniform(0, 1) elements
Distributional Recurrence:
CnD= Tn + CJ1 + CJ2 + CJ3
J = (J1, J2, J3): (random) subproblem sizes left medium rightP Q
J1 J2 J3Tn: (random) #cmps of first partitioning step
Taking expectations and conditioning on J:
E[Cn] = E[Tn] +n−2∑j=0
wn,j E[Cj]
with wn,j = Pr[J1 = j] + Pr[J2 = j] + Pr[J3 = j]
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 7 / 16
Expected Number of Comparisons
Goal: Count comparisons in Dual-Pivot Quicksort with Pivot Sampling
Cn: (random!) #cmps to sort n i. i. d. Uniform(0, 1) elements
Distributional Recurrence:
CnD= Tn + CJ1 + CJ2 + CJ3
J = (J1, J2, J3): (random) subproblem sizes left medium rightP Q
J1 J2 J3Tn: (random) #cmps of first partitioning step
Taking expectations and conditioning on J:
E[Cn] = E[Tn] +n−2∑j=0
wn,j E[Cj]
with wn,j = Pr[J1 = j] + Pr[J2 = j] + Pr[J3 = j]
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 7 / 16
Solution of Recurrence
Recurrence: E[Cn] = E[Tn] +n−2∑j=0
wn,j E[Cj] with E[Tn] ∼ γn
Apply Continuous Master Theorem [Roura 2001]Idea: Approximate sum by integral over relative subprobem size α = j/n
n−2∑j=0
wn,j E[Cj] ≈ˆ n0
wn,j E[Cj]dj ≈ˆ 10
n ·wn,αn E[Cαn]dα
Continuous Recurrence: E[Cn] ≈ E[Tn] +ˆ 10
w(α)E[Cαn]dα
Ansatz: E[Cn] = γHn lnn
fulfills recurrence for H =
3∑l=1
tl + 1
k+ 1
(Hk+1 −Htl+1
) E[Cn] ∼
γ
Hn lnn Next: What is γ?
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 8 / 16
Solution of Recurrence
Recurrence: E[Cn] = E[Tn] +n−2∑j=0
wn,j E[Cj] with E[Tn] ∼ γn
Apply Continuous Master Theorem [Roura 2001]Idea: Approximate sum by integral over relative subprobem size α = j/n
n−2∑j=0
wn,j E[Cj] ≈ˆ n0
wn,j E[Cj]dj ≈ˆ 10
n ·wn,αn E[Cαn]dα
Continuous Recurrence: E[Cn] ≈ E[Tn] +ˆ 10
w(α)E[Cαn]dα
Ansatz: E[Cn] = γHn lnn
fulfills recurrence for H =
3∑l=1
tl + 1
k+ 1
(Hk+1 −Htl+1
) E[Cn] ∼
γ
Hn lnn Next: What is γ?
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 8 / 16
Solution of Recurrence
Recurrence: E[Cn] = E[Tn] +n−2∑j=0
wn,j E[Cj] with E[Tn] ∼ γn
Apply Continuous Master Theorem [Roura 2001]Idea: Approximate sum by integral over relative subprobem size α = j/n
n−2∑j=0
wn,j E[Cj] ≈ˆ n0
wn,j E[Cj]dj ≈ˆ 10
n ·wn,αn E[Cαn]dα
Continuous Recurrence: E[Cn] ≈ E[Tn] +ˆ 10
w(α)E[Cαn]dα
Ansatz: E[Cn] = γHn lnn
fulfills recurrence for H =
3∑l=1
tl + 1
k+ 1
(Hk+1 −Htl+1
) E[Cn] ∼
γ
Hn lnn Next: What is γ?
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 8 / 16
Solution of Recurrence
Recurrence: E[Cn] = E[Tn] +n−2∑j=0
wn,j E[Cj] with E[Tn] ∼ γn
Apply Continuous Master Theorem [Roura 2001]Idea: Approximate sum by integral over relative subprobem size α = j/n
n−2∑j=0
wn,j E[Cj] ≈ˆ n0
wn,j E[Cj]dj ≈ˆ 10
n ·wn,αn︸ ︷︷ ︸→ w(α)
E[Cαn]dα
Continuous Recurrence: E[Cn] ≈ E[Tn] +ˆ 10
w(α)E[Cαn]dα
Ansatz: E[Cn] = γHn lnn
fulfills recurrence for H =
3∑l=1
tl + 1
k+ 1
(Hk+1 −Htl+1
) E[Cn] ∼
γ
Hn lnn Next: What is γ?
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 8 / 16
Solution of Recurrence
Recurrence: E[Cn] = E[Tn] +n−2∑j=0
wn,j E[Cj] with E[Tn] ∼ γn
Apply Continuous Master Theorem [Roura 2001]Idea: Approximate sum by integral over relative subprobem size α = j/n
n−2∑j=0
wn,j E[Cj] ≈ˆ n0
wn,j E[Cj]dj ≈ˆ 10
n ·wn,αn︸ ︷︷ ︸→ w(α)
E[Cαn]dα
Continuous Recurrence: E[Cn] ≈ E[Tn] +ˆ 10
w(α)E[Cαn]dα
Ansatz: E[Cn] = γHn lnn
fulfills recurrence for H =
3∑l=1
tl + 1
k+ 1
(Hk+1 −Htl+1
) E[Cn] ∼
γ
Hn lnn Next: What is γ?
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 8 / 16
Solution of Recurrence
Recurrence: E[Cn] = E[Tn] +n−2∑j=0
wn,j E[Cj] with E[Tn] ∼ γn
Apply Continuous Master Theorem [Roura 2001]Idea: Approximate sum by integral over relative subprobem size α = j/n
n−2∑j=0
wn,j E[Cj] ≈ˆ n0
wn,j E[Cj]dj ≈ˆ 10
n ·wn,αn︸ ︷︷ ︸→ w(α)
E[Cαn]dα
Continuous Recurrence: E[Cn] ≈ E[Tn] +ˆ 10
w(α)E[Cαn]dα
Ansatz: E[Cn] = γHn lnn
fulfills recurrence for H =
3∑l=1
tl + 1
k+ 1
(Hk+1 −Htl+1
) E[Cn] ∼
γ
Hn lnn Next: What is γ?
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 8 / 16
Solution of Recurrence
Recurrence: E[Cn] = E[Tn] +n−2∑j=0
wn,j E[Cj] with E[Tn] ∼ γn
Apply Continuous Master Theorem [Roura 2001]Idea: Approximate sum by integral over relative subprobem size α = j/n
n−2∑j=0
wn,j E[Cj] ≈ˆ n0
wn,j E[Cj]dj ≈ˆ 10
n ·wn,αn︸ ︷︷ ︸→ w(α)
E[Cαn]dα
Continuous Recurrence: E[Cn] ≈ E[Tn] +ˆ 10
w(α)E[Cαn]dα
Ansatz: E[Cn] = γHn lnn
fulfills recurrence for H =
3∑l=1
tl + 1
k+ 1
(Hk+1 −Htl+1
) E[Cn] ∼
γ
Hn lnn Next: What is γ?
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 8 / 16
Solution of Recurrence
Recurrence: E[Cn] = E[Tn] +n−2∑j=0
wn,j E[Cj] with E[Tn] ∼ γn
Apply Continuous Master Theorem [Roura 2001]Idea: Approximate sum by integral over relative subprobem size α = j/n
n−2∑j=0
wn,j E[Cj] ≈ˆ n0
wn,j E[Cj]dj ≈ˆ 10
n ·wn,αn︸ ︷︷ ︸→ w(α)
E[Cαn]dα
Continuous Recurrence: E[Cn] ≈ E[Tn] +ˆ 10
w(α)E[Cαn]dα
Ansatz: E[Cn] = γHn lnn
fulfills recurrence for H =
3∑l=1
tl + 1
k+ 1
(Hk+1 −Htl+1
) E[Cn] ∼
γ
Hn lnn Next: What is γ?
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 8 / 16
Solution of Recurrence
Recurrence: E[Cn] = E[Tn] +n−2∑j=0
wn,j E[Cj] with E[Tn] ∼ γn
Apply Continuous Master Theorem [Roura 2001]Idea: Approximate sum by integral over relative subprobem size α = j/n
n−2∑j=0
wn,j E[Cj] ≈ˆ n0
wn,j E[Cj]dj ≈ˆ 10
n ·wn,αn︸ ︷︷ ︸→ w(α)
E[Cαn]dα
Continuous Recurrence: E[Cn] ≈ E[Tn] +ˆ 10
w(α)E[Cαn]dα
Ansatz: E[Cn] = γHn lnn
fulfills recurrence for H =
3∑l=1
tl + 1
k+ 1
(Hk+1 −Htl+1
) E[Cn] ∼
γ
Hn lnn Next: What is γ?
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 8 / 16
#Comparisons per Partitioning
How many comparisons for one element? It depends! Either 2 or 1 .
< P ?
swap ` < Q ?
skip swap g
3 7
3 7
> Q ?
< P ? skip
swap ` swap k
37
3 7
Tn ∼ 2n− ”cheap elements”:
s@K : small in k’s range
l@ G : large in g’s range
need ranges K and G !
Recall Invariant: < P P ≤ ◦ ≤ Q ≥ QP Q
|K| ∼ I1 + I2
|G | ∼ I3with I = ( I1 , I2 , I3 ) sizes of partitions
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 9 / 16
#Comparisons per Partitioning
How many comparisons for one element? It depends! Either 2 or 1 .
< P ?
swap ` < Q ?
skip swap g
3 7
3 7
> Q ?
< P ? skip
swap ` swap k
37
3 7
Tn ∼ 2n− ”cheap elements”:
s@K : small in k’s range
l@ G : large in g’s range
need ranges K and G !
Recall Invariant: < P P ≤ ◦ ≤ Q ≥ QP Q
|K| ∼ I1 + I2
|G | ∼ I3with I = ( I1 , I2 , I3 ) sizes of partitions
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 9 / 16
#Comparisons per Partitioning
How many comparisons for one element? It depends! Either 2 or 1 .
< P ?
swap ` < Q ?
skip swap g
3 7
3 7
> Q ?
< P ? skip
swap ` swap k
37
3 7
Tn ∼ 2n− ”cheap elements”:
s@K : small in k’s range
l@ G : large in g’s range
need ranges K and G !
Recall Invariant: < P P ≤ ◦ ≤ Q ≥ QP Q
|K| ∼ I1 + I2
|G | ∼ I3with I = ( I1 , I2 , I3 ) sizes of partitions
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 9 / 16
#Comparisons per Partitioning
How many comparisons for one element? It depends! Either 2 or 1 .
< P ?
swap ` < Q ?
skip swap g
3 7
3 7
> Q ?
< P ? skip
swap ` swap k
37
3 7
Tn ∼ 2n− ”cheap elements”:
s@K : small in k’s range
l@ G : large in g’s range
need ranges K and G !
Recall Invariant: < P P ≤ ◦ ≤ Q ≥ QP Q
|K| ∼ I1 + I2
|G | ∼ I3with I = ( I1 , I2 , I3 ) sizes of partitions
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 9 / 16
#Comparisons per Partitioning
How many comparisons for one element? It depends! Either 2 or 1 .
< P ?
swap ` < Q ?
skip swap g
3 7
3 7
> Q ?
< P ? skip
swap ` swap k
37
3 7
Tn ∼ 2n− ”cheap elements”:
s@K : small in k’s range
l@ G : large in g’s range
need ranges K and G !
Recall Invariant: < P P ≤ ◦ ≤ Q ≥ QP Q
|K| ∼ I1 + I2
|G | ∼ I3with I = ( I1 , I2 , I3 ) sizes of partitions
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 9 / 16
#Comparisons per Partitioning
How many comparisons for one element? It depends! Either 2 or 1 .
< P ?
swap ` < Q ?
skip swap g
3 7
3 7
> Q ?
< P ? skip
swap ` swap k
37
3 7
Tn ∼ 2n− ”cheap elements”:
s@K : small in k’s range
l@ G : large in g’s range
need ranges K and G !
Recall Invariant: < P P ≤ ◦ ≤ Q ≥ QP Q
|K| ∼ I1 + I2
|G | ∼ I3with I = ( I1 , I2 , I3 ) sizes of partitions
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 9 / 16
#Comparisons per Partitioning
How many comparisons for one element? It depends! Either 2 or 1 .
< P ?
swap ` < Q ?
skip swap g
3 7
3 7
> Q ?
< P ? skip
swap ` swap k
37
3 7
Tn ∼ 2n− ”cheap elements”:
s@K : small in k’s range
l@ G : large in g’s range
need ranges K and G !
Recall Invariant: < P P ≤ ◦ ≤ Q ≥ QP Q
|K| ∼ I1 + I2
|G | ∼ I3with I = ( I1 , I2 , I3 ) sizes of partitions
Sebastian Wild Dual-Pivot Quicksort 2014-06-16 9 / 16
Distribution of s@K and l@G
Consider I fixed.|K| ∼ I1 + I2|G | ∼ I3
#small = I1
#large = I3
s@KD= Hypergeometric( #small , |K| , n− k)
[sample excludedfrom partitioning n− k
]
Draw positions of small elements:1 All array indices in urn2 Draw without replacement3 Count green balls