Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
Multi-Pivot QuicksortComparison-Optimal Algorithms and Beyond
Martin Aumuller
IT University of Copenhagen
April 4, 2016
ARCO Workshop
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 1/23
Dual-Pivot Quicksort
p
3 2 8 5 1 4 7 6
Input: Permutation of {1, . . . , n}.
1. Choose two pivots p, q with p < q.
2. Partition, i.e., re-arrange elements.
Partition: < p p < · · · < q > qp q
3. Use recursion to sort the three subarrays.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 2/23
Dual-Pivot Quicksort
p
3 2 8 5 1 4 7 6
Input: Permutation of {1, . . . , n}.1. Choose two pivots p, q with p < q.
2. Partition, i.e., re-arrange elements.
Partition: < p p < · · · < q > qp q
3. Use recursion to sort the three subarrays.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 2/23
Dual-Pivot Quicksort
p q
= =
3 2 8 5 1 4 7 6
Input: Permutation of {1, . . . , n}.1. Choose two pivots p, q with p < q.
2. Partition, i.e., re-arrange elements.
Partition: < p p < · · · < q > qp q
3. Use recursion to sort the three subarrays.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 2/23
Dual-Pivot Quicksort
p q
= =
3 2 8 5 1 4 7 6
Input: Permutation of {1, . . . , n}.1. Choose two pivots p, q with p < q.
2. Partition, i.e., re-arrange elements.
Partition: < p p < · · · < q > qp q
3. Use recursion to sort the three subarrays.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 2/23
Dual-Pivot Quicksort
p q
= =
5 7 84 6312
Input: Permutation of {1, . . . , n}.1. Choose two pivots p, q with p < q.
2. Partition, i.e., re-arrange elements.
Partition: < p p < · · · < q > qp q
3. Use recursion to sort the three subarrays.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 2/23
Dual-Pivot Quicksort
p q
= =
5 7 84 6312
Input: Permutation of {1, . . . , n}.1. Choose two pivots p, q with p < q.
2. Partition, i.e., re-arrange elements.
Partition: < p p < · · · < q > qp q
3. Use recursion to sort the three subarrays.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 2/23
Dual-Pivot Quicksort
p q
= =
7 8633 51 421
Input: Permutation of {1, . . . , n}.1. Choose two pivots p, q with p < q.
2. Partition, i.e., re-arrange elements.
Partition: < p p < · · · < q > qp q
3. Use recursion to sort the three subarrays.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 2/23
Dual-Pivot Quicksort
p
7 8633 51 421
Input: Permutation of {1, . . . , n}.1. Choose two pivots p, q with p < q.
2. Partition, i.e., re-arrange elements.
Partition: < p p < · · · < q > qp q
3. Use recursion to sort the three subarrays.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 2/23
Why Consider It?
(Talk “Sorting in JDK”, V. Yaroslavskiy, 2015.)
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 3/23
Part I
The Exact Average Comparison Count of
Comparison-Optimal Dual-Pivot Quicksort
http://arxiv.org/abs/1602.04031
Joint work with Martin Dietzfelbinger (TU Ilmenau), Clemens Heuberger,Daniel Krenn (AAU Klagenfurt), and Helmut Prodinger (StellenboschUniversity)
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 4/23
Goal
Analyze comparison-optimal dual-pivot quicksort algorithms exactly.
Pn: cost for partitioning the input.
Cn: cost for sorting the input.
Recurrence:
E(Cn) = E(Pn) +3(n2
) n−2∑k=1
(n − 1− k)E(Ck) (1)
Need: exact analysis of partitioning cost E(Pn).
Then use known techniques (Generating functions, [Wild’13]) to solve (1).
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 5/23
Goal
Analyze comparison-optimal dual-pivot quicksort algorithms exactly.
Pn: cost for partitioning the input.
Cn: cost for sorting the input.
Recurrence:
E(Cn) = E(Pn) +3(n2
) n−2∑k=1
(n − 1− k)E(Ck) (1)
Need: exact analysis of partitioning cost E(Pn).
Then use known techniques (Generating functions, [Wild’13]) to solve (1).
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 5/23
How is the partitioning cost determined?
Must classify n − 2 entries x into three parts:
x < p p < x < q x > qp q
small , medium , or large
1 or 2 comparisons.Unavoidable: 1 comparison for small/large x , 2 comparisons for medium x .
Extra: small x compared with q first and large x compared with p first.
Partitioning strategy determines for next element x whether to comparex with p first or with q first.
An implementation (Y./S./. . . ) implicitly defines such a strategy.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 6/23
How is the partitioning cost determined?
Must classify n − 2 entries x into three parts:
x < p p < x < q x > qp q
small , medium , or large
1 or 2 comparisons.Unavoidable: 1 comparison for small/large x , 2 comparisons for medium x .
Extra: small x compared with q first and large x compared with p first.
Partitioning strategy determines for next element x whether to comparex with p first or with q first.
An implementation (Y./S./. . . ) implicitly defines such a strategy.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 6/23
How is the partitioning cost determined?
Must classify n − 2 entries x into three parts:
x < p p < x < q x > qp q
small , medium , or large
1 or 2 comparisons.Unavoidable: 1 comparison for small/large x , 2 comparisons for medium x .
Extra: small x compared with q first and large x compared with p first.
Partitioning strategy determines for next element x whether to comparex with p first or with q first.
An implementation (Y./S./. . . ) implicitly defines such a strategy.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 6/23
How is the partitioning cost determined?
Must classify n − 2 entries x into three parts:
x < p p < x < q x > qp q
small , medium , or large
1 or 2 comparisons.Unavoidable: 1 comparison for small/large x , 2 comparisons for medium x .
Extra: small x compared with q first and large x compared with p first.
Partitioning strategy determines for next element x whether to comparex with p first or with q first.
An implementation (Y./S./. . . ) implicitly defines such a strategy.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 6/23
Assumptions of the Analysis
Input is random permutation of {1, . . . , n}Choose two elements as pivots
Relabel small elements as σ, medium elements as µ, and largeelements as λ
pp q
= =
3 2 8 5 1 4 7 6
Read input left to right, classify each element by one or two comparisons.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 7/23
Assumptions of the Analysis
Input is random permutation of {1, . . . , n}Choose two elements as pivots
Relabel small elements as σ, medium elements as µ, and largeelements as λ
pp q
= =
3 2 8 5 1 4 7 6
Read input left to right, classify each element by one or two comparisons.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 7/23
Assumptions of the Analysis
Input is random permutation of {1, . . . , n}Choose two elements as pivots
Relabel small elements as σ, medium elements as µ, and largeelements as λ
pp q
= =
3 8 5 1 4 7 6σ
Read input left to right, classify each element by one or two comparisons.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 7/23
Assumptions of the Analysis
Input is random permutation of {1, . . . , n}Choose two elements as pivots
Relabel small elements as σ, medium elements as µ, and largeelements as λ
pp q
= =
3 5 1 4 7 6σ λ
Read input left to right, classify each element by one or two comparisons.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 7/23
Assumptions of the Analysis
Input is random permutation of {1, . . . , n}Choose two elements as pivots
Relabel small elements as σ, medium elements as µ, and largeelements as λ
pp q
= =
3 1 4 7 6σ λ µ
Read input left to right, classify each element by one or two comparisons.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 7/23
Assumptions of the Analysis
Input is random permutation of {1, . . . , n}Choose two elements as pivots
Relabel small elements as σ, medium elements as µ, and largeelements as λ
pp q
= =
3 4 7 6σ λ µ σ
Read input left to right, classify each element by one or two comparisons.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 7/23
Assumptions of the Analysis
Input is random permutation of {1, . . . , n}Choose two elements as pivots
Relabel small elements as σ, medium elements as µ, and largeelements as λ
pp q
= =
3 7 6σ λ µ σ µ
Read input left to right, classify each element by one or two comparisons.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 7/23
Assumptions of the Analysis
Input is random permutation of {1, . . . , n}Choose two elements as pivots
Relabel small elements as σ, medium elements as µ, and largeelements as λ
pp q
= =
3 6σ λ µ σ µ λ
Read input left to right, classify each element by one or two comparisons.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 7/23
Assumptions of the Analysis
Input is random permutation of {1, . . . , n}Choose two elements as pivots
Relabel small elements as σ, medium elements as µ, and largeelements as λ
pp q
= =
3 6σ λ µ σ µ λ
Read input left to right, classify each element by one or two comparisons.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 7/23
Average Cost
For the average partitioning/classification cost E(Pn) we get:
E(Pn) = 43(n − 2) + 1 + “average number of extra comparisons”
For all classification algorithms:
small [large] elements haveto be compared to p [q]
medium elements have to becompared to both
“Extra” comparisons:
small x compared to q first
large x compared to p first
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 8/23
Yaroslavskiy’s Dual-Pivot Quicksort Algorithm
Assume we are in round i .
Strategy “Yaroslavskiy”
−→
σ λ σ µ ? ? ? ?
Next element
Seen elements in
Classification Strategy:
previous element large: compare with larger pivot first.
otherwise: compare with smaller pivot first.
[WN12]: Average sorting cost is 1.9n ln n + O(n).
[WNN15]: Analysis of the distribution of Cn (and other cost measures).
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 9/23
Yaroslavskiy’s Dual-Pivot Quicksort Algorithm
Assume we are in round i .
Strategy “Yaroslavskiy”
−→
σ λ σ µ ? ? ? ?
Next element
Seen elements in
Classification Strategy:
previous element large: compare with larger pivot first.
otherwise: compare with smaller pivot first.
[WN12]: Average sorting cost is 1.9n ln n + O(n).
[WNN15]: Analysis of the distribution of Cn (and other cost measures).
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 9/23
Yaroslavskiy’s Dual-Pivot Quicksort Algorithm
Assume we are in round i .
Strategy “Yaroslavskiy”
−→
σ λ σ µ ? ? ? ?
Next element
Seen elements in
Classification Strategy:
previous element large: compare with larger pivot first.
otherwise: compare with smaller pivot first.
[WN12]: Average sorting cost is 1.9n ln n + O(n).
[WNN15]: Analysis of the distribution of Cn (and other cost measures).
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 9/23
Yaroslavskiy’s Dual-Pivot Quicksort Algorithm
Assume we are in round i .
Strategy “Yaroslavskiy”
−→
σ λ σ µ ? ? ? ?
Next element
Seen elements in
Classification Strategy:
previous element large: compare with larger pivot first.
otherwise: compare with smaller pivot first.
[WN12]: Average sorting cost is 1.9n ln n + O(n).
[WNN15]: Analysis of the distribution of Cn (and other cost measures).
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 9/23
The Comparison-Optimal Dual-Pivot Quicksort AlgorithmAssume we are in round i .
Strategy “Count”
−→
σ λ σ µ ? ? ? ?
Next element
Seen si−1 small and `i−1 large elements in
Classification Strategy:
`i−1 > si−1: compare with larger pivot first.
`i−1 ≤ si−1: compare with smaller pivot first.
[AD15]: Average sorting cost is 1.8n ln n + O(n).
Here: “Count” is optimal + exact average comparison count.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 10/23
The Comparison-Optimal Dual-Pivot Quicksort AlgorithmAssume we are in round i .
Strategy “Count”
−→
σ λ σ µ ? ? ? ?
Next element
Seen si−1 small and `i−1 large elements in
Classification Strategy:
`i−1 > si−1: compare with larger pivot first.
`i−1 ≤ si−1: compare with smaller pivot first.
[AD15]: Average sorting cost is 1.8n ln n + O(n).
Here: “Count” is optimal + exact average comparison count.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 10/23
The Comparison-Optimal Dual-Pivot Quicksort AlgorithmAssume we are in round i .
Strategy “Count”
−→
σ λ σ µ ? ? ? ?
Next element
Seen si−1 small and `i−1 large elements in
Classification Strategy:
`i−1 > si−1: compare with larger pivot first.
`i−1 ≤ si−1: compare with smaller pivot first.
[AD15]: Average sorting cost is 1.8n ln n + O(n).
Here: “Count” is optimal + exact average comparison count.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 10/23
The Comparison-Optimal Dual-Pivot Quicksort AlgorithmAssume we are in round i .
Strategy “Count”
−→
σ λ σ µ ? ? ? ?
Next element
Seen si−1 small and `i−1 large elements in
Classification Strategy:
`i−1 > si−1: compare with larger pivot first.
`i−1 ≤ si−1: compare with smaller pivot first.
[AD15]: Average sorting cost is 1.8n ln n + O(n).
Here: “Count” is optimal + exact average comparison count.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 10/23
Proof idea: “Count” is optimal (among all algorithms)Polya urn with three colors to model input sequence of {σ, µ, λ}.
1 Put one light green ball, one green ball, one dark green ball in urn.2 Round i = 1, . . . , n − 2:
Choose ball from urn at random. Take down its color c, put it back,and put another ball of the same color in the urn.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 11/23
Proof idea: “Count” is optimal (among all algorithms)Polya urn with three colors to model input sequence of {σ, µ, λ}.
1 Put one light green ball, one green ball, one dark green ball in urn.
2 Round i = 1, . . . , n − 2:
Choose ball from urn at random. Take down its color c, put it back,and put another ball of the same color in the urn.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 11/23
Proof idea: “Count” is optimal (among all algorithms)Polya urn with three colors to model input sequence of {σ, µ, λ}.
1 Put one light green ball, one green ball, one dark green ball in urn.2 Round i = 1, . . . , n − 2:
Choose ball from urn at random. Take down its color c, put it back,and put another ball of the same color in the urn.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 11/23
Proof idea: “Count” is optimal (among all algorithms)Polya urn with three colors to model input sequence of {σ, µ, λ}.
1 Put one light green ball, one green ball, one dark green ball in urn.2 Round i = 1, . . . , n − 2:
Choose ball from urn at random.
Take down its color c, put it back,and put another ball of the same color in the urn.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 11/23
Proof idea: “Count” is optimal (among all algorithms)Polya urn with three colors to model input sequence of {σ, µ, λ}.
1 Put one light green ball, one green ball, one dark green ball in urn.2 Round i = 1, . . . , n − 2:
Choose ball from urn at random.
Take down its color c, put it back,and put another ball of the same color in the urn.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 11/23
Proof idea: “Count” is optimal (among all algorithms)Polya urn with three colors to model input sequence of {σ, µ, λ}.
1 Put one light green ball, one green ball, one dark green ball in urn.2 Round i = 1, . . . , n − 2:
Choose ball from urn at random. Take down its color c, put it back,
and put another ball of the same color in the urn.
λ
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 11/23
Proof idea: “Count” is optimal (among all algorithms)Polya urn with three colors to model input sequence of {σ, µ, λ}.
1 Put one light green ball, one green ball, one dark green ball in urn.2 Round i = 1, . . . , n − 2:
Choose ball from urn at random. Take down its color c, put it back,
and put another ball of the same color in the urn.
λ
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 11/23
Proof idea: “Count” is optimal (among all algorithms)Polya urn with three colors to model input sequence of {σ, µ, λ}.
1 Put one light green ball, one green ball, one dark green ball in urn.2 Round i = 1, . . . , n − 2:
Choose ball from urn at random. Take down its color c, put it back,and put another ball of the same color in the urn.
λ
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 11/23
Proof idea: “Count” is optimal (among all algorithms)Polya urn with three colors to model input sequence of {σ, µ, λ}.
1 Put one light green ball, one green ball, one dark green ball in urn.2 Round i = 1, . . . , n − 2:
Choose ball from urn at random. Take down its color c, put it back,and put another ball of the same color in the urn.
λ
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 11/23
Proof idea: “Count” is optimal (among all algorithms)Polya urn with three colors to model input sequence of {σ, µ, λ}.
1 Put one light green ball, one green ball, one dark green ball in urn.2 Round i = 1, . . . , n − 2:
Choose ball from urn at random. Take down its color c, put it back,and put another ball of the same color in the urn.
λ σ
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 11/23
Proof idea: “Count” is optimal (among all algorithms)Polya urn with three colors to model input sequence of {σ, µ, λ}.
1 Put one light green ball, one green ball, one dark green ball in urn.2 Round i = 1, . . . , n − 2:
Choose ball from urn at random. Take down its color c, put it back,and put another ball of the same color in the urn.
λ σ
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 11/23
Proof idea: “Count” is optimal (among all algorithms)Polya urn with three colors to model input sequence of {σ, µ, λ}.
1 Put one light green ball, one green ball, one dark green ball in urn.2 Round i = 1, . . . , n − 2:
Choose ball from urn at random. Take down its color c, put it back,and put another ball of the same color in the urn.
λ σ
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 11/23
Proof idea: “Count” is optimal (among all algorithms)Polya urn with three colors to model input sequence of {σ, µ, λ}.
1 Put one light green ball, one green ball, one dark green ball in urn.2 Round i = 1, . . . , n − 2:
Choose ball from urn at random. Take down its color c, put it back,and put another ball of the same color in the urn.
λ σ µ
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 11/23
Proof idea: “Count” is optimal (among all algorithms)Polya urn with three colors to model input sequence of {σ, µ, λ}.
1 Put one light green ball, one green ball, one dark green ball in urn.2 Round i = 1, . . . , n − 2:
Choose ball from urn at random. Take down its color c, put it back,and put another ball of the same color in the urn.
λ σ µ
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 11/23
Proof idea: “Count” is optimal (among all algorithms)Polya urn with three colors to model input sequence of {σ, µ, λ}.
1 Put one light green ball, one green ball, one dark green ball in urn.2 Round i = 1, . . . , n − 2:
Choose ball from urn at random. Take down its color c, put it back,and put another ball of the same color in the urn.
λ σ µ
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 11/23
Proof idea: “Count” is optimal (among all algorithms)Polya urn with three colors to model input sequence of {σ, µ, λ}.
1 Put one light green ball, one green ball, one dark green ball in urn.2 Round i = 1, . . . , n − 2:
Choose ball from urn at random. Take down its color c, put it back,and put another ball of the same color in the urn.
λ σ µ σ
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 11/23
Proof idea: “Count” is optimal (among all algorithms)Polya urn with three colors to model input sequence of {σ, µ, λ}.
1 Put one light green ball, one green ball, one dark green ball in urn.2 Round i = 1, . . . , n − 2:
Choose ball from urn at random. Take down its color c, put it back,and put another ball of the same color in the urn.
λ σ µ σ
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 11/23
Proof idea: “Count” is optimal (among all algorithms)Polya urn with three colors to model input sequence of {σ, µ, λ}.
1 Put one light green ball, one green ball, one dark green ball in urn.2 Round i = 1, . . . , n − 2:
Choose ball from urn at random. Take down its color c, put it back,and put another ball of the same color in the urn.
λ σ µ σ
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 11/23
Proof idea: “Count” is optimal (among all algorithms)Polya urn with three colors to model input sequence of {σ, µ, λ}.
1 Put one light green ball, one green ball, one dark green ball in urn.2 Round i = 1, . . . , n − 2:
Choose ball from urn at random. Take down its color c, put it back,and put another ball of the same color in the urn.
λ σ µ σ
Can show: This leads to the same distribution on {σ, µ, λ} sequences as theone generated by random permutation/pivots/relabel.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 11/23
Proof idea: “Count” is optimal (among all algorithms)Polya urn with three colors to model input sequence of {σ, µ, λ}.
1 Put one light green ball, one green ball, one dark green ball in urn.2 Round i = 1, . . . , n − 2:
Choose ball from urn at random. Take down its color c, put it back,and put another ball of the same color in the urn.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 11/23
Proof idea: “Count” is optimal (among all algorithms)Polya urn with three colors to model input sequence of {σ, µ, λ}.
1 Put one light green ball, one green ball, one dark green ball in urn.2 Round i = 1, . . . , n − 2:
Choose ball from urn at random. Take down its color c, put it back,and put another ball of the same color in the urn.
In Round i :
Pr(element is small) =si−1 + 1
i + 2.
(analogous formulæ for medium/large elements.)
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 11/23
Proof idea: “Count” is optimal (among all algorithms)Polya urn with three colors to model input sequence of {σ, µ, λ}.
1 Put one light green ball, one green ball, one dark green ball in urn.2 Round i = 1, . . . , n − 2:
Choose ball from urn at random. Take down its color c, put it back,and put another ball of the same color in the urn.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 11/23
Proof idea: “Count” is optimal (among all algorithms)Polya urn with three colors to model input sequence of {σ, µ, λ}.
1 Put one light green ball, one green ball, one dark green ball in urn.2 Round i = 1, . . . , n − 2:
Choose ball from urn at random. Take down its color c, put it back,and put another ball of the same color in the urn.
Next step: Show that every partitioning strategy S with decision in round ibased on the elements seen in rounds up to i − 1 makes at least as manycomparisons in round i as “Count” (on average).
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 11/23
“Count” is optimal (among all algorithms)
If si−1 ≥ `i−1, “Count” compares with small pivot first. Probability togenerate additional cost 1 in this step:
Pr(“Count” gets extra comparison in step i) =`i−1 + 1
i + 2.
If S uses small pivot first, no difference.
If S takes large pivot first:
Pr(S gets extra comparison in step i) =si−1 + 1
i + 2,
at least as big as the probability for “Count”.
Conclusion: “Count” is the comparison-optimal strategy.
Next: Analysis of average comparison count.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 12/23
“Count” is optimal (among all algorithms)
If si−1 ≥ `i−1, “Count” compares with small pivot first. Probability togenerate additional cost 1 in this step:
Pr(“Count” gets extra comparison in step i) =`i−1 + 1
i + 2.
If S uses small pivot first, no difference.
If S takes large pivot first:
Pr(S gets extra comparison in step i) =si−1 + 1
i + 2,
at least as big as the probability for “Count”.
Conclusion: “Count” is the comparison-optimal strategy.
Next: Analysis of average comparison count.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 12/23
“Count” is optimal (among all algorithms)
If si−1 ≥ `i−1, “Count” compares with small pivot first. Probability togenerate additional cost 1 in this step:
Pr(“Count” gets extra comparison in step i) =`i−1 + 1
i + 2.
If S uses small pivot first, no difference.
If S takes large pivot first:
Pr(S gets extra comparison in step i) =si−1 + 1
i + 2,
at least as big as the probability for “Count”.
Conclusion: “Count” is the comparison-optimal strategy.
Next: Analysis of average comparison count.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 12/23
“Count” is optimal (among all algorithms)
If si−1 ≥ `i−1, “Count” compares with small pivot first. Probability togenerate additional cost 1 in this step:
Pr(“Count” gets extra comparison in step i) =`i−1 + 1
i + 2.
If S uses small pivot first, no difference.
If S takes large pivot first:
Pr(S gets extra comparison in step i) =si−1 + 1
i + 2,
at least as big as the probability for “Count”.
Conclusion: “Count” is the comparison-optimal strategy.
Next: Analysis of average comparison count.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 12/23
Define “random walk” :
Xi = si−1 − `i−1, for 1 ≤ i ≤ n − 1.
Classification Strategy:
Xi ≥ 0: compare with smaller pivot first.
Xi < 0: compare with larger pivot first.
Study simplified situation, ignore medium elements.
We classify n′ = s + ` elements.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 13/23
Define “random walk” :
Xi = si−1 − `i−1, for 1 ≤ i ≤ n − 1.
Classification Strategy:
Xi ≥ 0: compare with smaller pivot first.
Xi < 0: compare with larger pivot first.
Study simplified situation, ignore medium elements.
We classify n′ = s + ` elements.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 13/23
Define “random walk” :
Xi = si−1 − `i−1, for 1 ≤ i ≤ n − 1.
Classification Strategy:
Xi ≥ 0: compare with smaller pivot first.
Xi < 0: compare with larger pivot first.
Study simplified situation, ignore medium elements.
We classify n′ = s + ` elements.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 13/23
Define “random walk” :
Xi = si−1 − `i−1, for 1 ≤ i ≤ n − 1.
Classification Strategy:
Xi ≥ 0: compare with smaller pivot first.
Xi < 0: compare with larger pivot first.
Study simplified situation, ignore medium elements.
We classify n′ = s + ` elements.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 13/23
Random Walks & Analysis of Count
1
n′i
Xi
−3
−2
−1
0
1
2
3
−→ λ λ σ σ σ λ σ σ λ λ λ σ λ λ σ σ σ
Observation: Extra comparison in round i ⇔move towards zero in round i , plus move down from a zero.
Exactly min(s, `) many “move towards zero ”situations.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 14/23
Random Walks & Analysis of Count
1
n′i
Xi
−3
−2
−1
0
1
2
3
λ
−→ λ λ σ σ σ λ σ σ λ λ λ σ λ λ σ σ σ
Observation: Extra comparison in round i ⇔move towards zero in round i , plus move down from a zero.
Exactly min(s, `) many “move towards zero ”situations.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 14/23
Random Walks & Analysis of Count
1
n′i
Xi
−3
−2
−1
0
1
2
3
λ
λ
−→ λ λ σ σ σ λ σ σ λ λ λ σ λ λ σ σ σ
Observation: Extra comparison in round i ⇔move towards zero in round i , plus move down from a zero.
Exactly min(s, `) many “move towards zero ”situations.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 14/23
Random Walks & Analysis of Count
1
n′i
Xi
−3
−2
−1
0
1
2
3
λ
λ σ
−→ λ λ σ σ σ λ σ σ λ λ λ σ λ λ σ σ σ
Observation: Extra comparison in round i ⇔move towards zero in round i , plus move down from a zero.
Exactly min(s, `) many “move towards zero ”situations.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 14/23
Random Walks & Analysis of Count
1
n′i
Xi
−3
−2
−1
0
1
2
3
λ
λ σ
σ
−→ λ λ σ σ σ λ σ σ λ λ λ σ λ λ σ σ σ
Observation: Extra comparison in round i ⇔move towards zero in round i , plus move down from a zero.
Exactly min(s, `) many “move towards zero ”situations.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 14/23
Random Walks & Analysis of Count
1
n′i
Xi
−3
−2
−1
0
1
2
3
λ
λ σ
σ
σ
−→ λ λ σ σ σ λ σ σ λ λ λ σ λ λ σ σ σ
Observation: Extra comparison in round i ⇔move towards zero in round i , plus move down from a zero.
Exactly min(s, `) many “move towards zero ”situations.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 14/23
Random Walks & Analysis of Count
1
n′i
Xi
−3
−2
−1
0
1
2
3
λ
λ σ
σ
σ λ
−→ λ λ σ σ σ λ σ σ λ λ λ σ λ λ σ σ σ
Observation: Extra comparison in round i ⇔move towards zero in round i , plus move down from a zero.
Exactly min(s, `) many “move towards zero ”situations.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 14/23
Random Walks & Analysis of Count
1
n′i
Xi
−3
−2
−1
0
1
2
3
λ
λ σ
σ
σ λ σ
−→ λ λ σ σ σ λ σ σ λ λ λ σ λ λ σ σ σ
Observation: Extra comparison in round i ⇔move towards zero in round i , plus move down from a zero.
Exactly min(s, `) many “move towards zero ”situations.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 14/23
Random Walks & Analysis of Count
1
n′i
Xi
−3
−2
−1
0
1
2
3
λ
λ σ
σ
σ λ σ
σ
−→ λ λ σ σ σ λ σ σ λ λ λ σ λ λ σ σ σ
Observation: Extra comparison in round i ⇔move towards zero in round i , plus move down from a zero.
Exactly min(s, `) many “move towards zero ”situations.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 14/23
Random Walks & Analysis of Count
1
n′i
Xi
−3
−2
−1
0
1
2
3
λ
λ σ
σ
σ λ σ
σ λ
−→ λ λ σ σ σ λ σ σ λ λ λ σ λ λ σ σ σ
Observation: Extra comparison in round i ⇔move towards zero in round i , plus move down from a zero.
Exactly min(s, `) many “move towards zero ”situations.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 14/23
Random Walks & Analysis of Count
1
n′i
Xi
−3
−2
−1
0
1
2
3
λ
λ σ
σ
σ λ σ
σ λ
λ
−→ λ λ σ σ σ λ σ σ λ λ λ σ λ λ σ σ σ
Observation: Extra comparison in round i ⇔move towards zero in round i , plus move down from a zero.
Exactly min(s, `) many “move towards zero ”situations.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 14/23
Random Walks & Analysis of Count
1
n′i
Xi
−3
−2
−1
0
1
2
3
λ
λ σ
σ
σ λ σ
σ λ
λ
λ
−→ λ λ σ σ σ λ σ σ λ λ λ σ λ λ σ σ σ
Observation: Extra comparison in round i ⇔move towards zero in round i , plus move down from a zero.
Exactly min(s, `) many “move towards zero ”situations.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 14/23
Random Walks & Analysis of Count
1
n′i
Xi
−3
−2
−1
0
1
2
3
λ
λ σ
σ
σ λ σ
σ λ
λ
λ σ
−→ λ λ σ σ σ λ σ σ λ λ λ σ λ λ σ σ σ
Observation: Extra comparison in round i ⇔move towards zero in round i , plus move down from a zero.
Exactly min(s, `) many “move towards zero ”situations.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 14/23
Random Walks & Analysis of Count
1
n′i
Xi
−3
−2
−1
0
1
2
3
λ
λ σ
σ
σ λ σ
σ λ
λ
λ σ λ
−→ λ λ σ σ σ λ σ σ λ λ λ σ λ λ σ σ σ
Observation: Extra comparison in round i ⇔move towards zero in round i , plus move down from a zero.
Exactly min(s, `) many “move towards zero ”situations.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 14/23
Random Walks & Analysis of Count
1
n′i
Xi
−3
−2
−1
0
1
2
3
λ
λ σ
σ
σ λ σ
σ λ
λ
λ σ λ
λ
−→ λ λ σ σ σ λ σ σ λ λ λ σ λ λ σ σ σ
Observation: Extra comparison in round i ⇔move towards zero in round i , plus move down from a zero.
Exactly min(s, `) many “move towards zero ”situations.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 14/23
Random Walks & Analysis of Count
1
n′i
Xi
−3
−2
−1
0
1
2
3
λ
λ σ
σ
σ λ σ
σ λ
λ
λ σ λ
λ σ
−→ λ λ σ σ σ λ σ σ λ λ λ σ λ λ σ σ σ
Observation: Extra comparison in round i ⇔move towards zero in round i , plus move down from a zero.
Exactly min(s, `) many “move towards zero ”situations.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 14/23
Random Walks & Analysis of Count
1
n′i
Xi
−3
−2
−1
0
1
2
3
λ
λ σ
σ
σ λ σ
σ λ
λ
λ σ λ
λ σ
σ
−→ λ λ σ σ σ λ σ σ λ λ λ σ λ λ σ σ σ
Observation: Extra comparison in round i ⇔move towards zero in round i , plus move down from a zero.
Exactly min(s, `) many “move towards zero ”situations.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 14/23
Random Walks & Analysis of Count
1
n′i
Xi
−3
−2
−1
0
1
2
3
λ
λ σ
σ
σ λ σ
σ λ
λ
λ σ λ
λ σ
σ
σ
−→ λ λ σ σ σ λ σ σ λ λ λ σ λ λ σ σ σ
Observation: Extra comparison in round i ⇔move towards zero in round i , plus move down from a zero.
Exactly min(s, `) many “move towards zero ”situations.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 14/23
Random Walks & Analysis of Count
1
n′i
Xi
−3
−2
−1
0
1
2
3
λ
λ σ
σ
σ λ σ
σ λ
λ
λ σ λ
λ σ
σ
σ
−→ λ λ σ σ σ λ σ σ λ λ λ σ λ λ σ σ σ
Observation: Extra comparison in round i ⇔
move towards zero in round i , plus move down from a zero.
Exactly min(s, `) many “move towards zero ”situations.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 14/23
Random Walks & Analysis of Count
1
n′i
Xi
−3
−2
−1
0
1
2
3
λ
λ σ
σ
σ λ σ
σ λ
λ
λ σ λ
λ σ
σ
σ
−→ λ λ σ σ σ λ σ σ λ λ λ σ λ λ σ σ σ
Observation: Extra comparison in round i ⇔move towards zero in round i ,
plus move down from a zero.
Exactly min(s, `) many “move towards zero ”situations.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 14/23
Random Walks & Analysis of Count
1
n′i
Xi
−3
−2
−1
0
1
2
3
λ
λ σ
σ
σ λ σ
σ λ
λ
λ σ λ
λ σ
σ
σ
−→ λ λ σ σ σ λ σ σ λ λ λ σ λ λ σ σ σ
Observation: Extra comparison in round i ⇔move towards zero in round i , plus move down from a zero.
Exactly min(s, `) many “move towards zero ”situations.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 14/23
Random Walks & Analysis of Count
1
n′i
Xi
−3
−2
−1
0
1
2
3
λ
λ σ
σ
σ λ σ
σ λ
λ
λ σ λ
λ σ
σ
σ
−→ λ λ σ σ σ λ σ σ λ λ λ σ λ λ σ σ σ
Observation: Extra comparison in round i ⇔move towards zero in round i , plus move down from a zero.
Exactly min(s, `) many “move towards zero ”situations.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 14/23
Counting Zeros in Random WalksDefine the “number of zeros”: Zn′ := #{i | Xi = 0}.
Can only have zero at odd positions i .
Central argument
For each i ∈ {1, 3, 5, . . .}, i ≤ n′ + 1,
P(Xi = 0) =1
i.
Linearity of expectation yields:
E(Zn′) =∑
1≤i≤n′+1i odd
1
i.
E(#(extra comps) | s + ` = n′) = min(s, `) +1
2
(E(Zn′)−
[n′ even]
n′ + 1
).
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 15/23
Counting Zeros in Random WalksDefine the “number of zeros”: Zn′ := #{i | Xi = 0}.Can only have zero at odd positions i .
Central argument
For each i ∈ {1, 3, 5, . . .}, i ≤ n′ + 1,
P(Xi = 0) =1
i.
Linearity of expectation yields:
E(Zn′) =∑
1≤i≤n′+1i odd
1
i.
E(#(extra comps) | s + ` = n′) = min(s, `) +1
2
(E(Zn′)−
[n′ even]
n′ + 1
).
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 15/23
Counting Zeros in Random WalksDefine the “number of zeros”: Zn′ := #{i | Xi = 0}.Can only have zero at odd positions i .
Central argument
For each i ∈ {1, 3, 5, . . .}, i ≤ n′ + 1,
P(Xi = 0) =1
i.
Linearity of expectation yields:
E(Zn′) =∑
1≤i≤n′+1i odd
1
i.
E(#(extra comps) | s + ` = n′) = min(s, `) +1
2
(E(Zn′)−
[n′ even]
n′ + 1
).
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 15/23
Counting Zeros in Random WalksDefine the “number of zeros”: Zn′ := #{i | Xi = 0}.Can only have zero at odd positions i .
Central argument
For each i ∈ {1, 3, 5, . . .}, i ≤ n′ + 1,
P(Xi = 0) =1
i.
Linearity of expectation yields:
E(Zn′) =∑
1≤i≤n′+1i odd
1
i.
E(#(extra comps) | s + ` = n′) = min(s, `) +1
2
(E(Zn′)−
[n′ even]
n′ + 1
).
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 15/23
Counting Zeros in Random WalksDefine the “number of zeros”: Zn′ := #{i | Xi = 0}.Can only have zero at odd positions i .
Central argument
For each i ∈ {1, 3, 5, . . .}, i ≤ n′ + 1,
P(Xi = 0) =1
i.
Linearity of expectation yields:
E(Zn′) =∑
1≤i≤n′+1i odd
1
i.
E(#(extra comps) | s + ` = n′) = min(s, `) +1
2
(E(Zn′)−
[n′ even]
n′ + 1
).
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 15/23
Exact Average Partitioning Cost
Back to general situation including medium elements.Averaging over all pivot choices yields:
E(Pn) =3n
2− 19
8+
2 + (−1)n
8(n − [n even])+
1
2
∑1≤i≤n−2
i odd
1
i.
Can also write down generating functions.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 16/23
Exact Average Sorting Cost
Solving dual-pivot quicksort recurrence with generating functions [Wild’13]gives expected comparison count, explicitly, and then asymptotically:
E(Cn) = 1.8n ln n + A′n + B ′ ln n + C ′ + O
(1
n
),
where A′ ≈ −2.38, B’ = 1.675, C ′ ≈ 1.82.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 17/23
Part II
Memory Accesses in Multi-Pivot Quicksort
http://arxiv.org/abs/1510.04676
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 18/23
Quicksort with k Pivots
Input: Random permutation of {1, . . . , n}.First k entries sorted are pivots p1, . . . , pk .
Partitioning: Split n − k remaining entries x into k + 1 classes
x < p1 p1 p1 < x < p2 p2 pk x > pk· · ·
A0 A1 Ak
Then sort classes recursively.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 19/23
Other Cost Measures
Number of branch misses: Thorough analysis of classical QS andYaroslavskiy’s algorithm by Martınez/Nebel/Wild (ANALCO’15).
Conclusion: No improvement over classical QS.
Number of cache misses: (Kushagra et al., ALENEX’14):Dual-Pivot and three-pivot algorithm causefar fewer cache misses than classical QS.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 20/23
A Generalized Partitioning Algorithm
A0 A1 A2 A3 A4 A5 A6? ?
g1 g2 g3 i j g4 g5
5 pointer visits.
Analysis for partitioning yields:
E(Pptr-visitn ) =
k + 3
4· (n − k), for odd k ,(
k + 3
4+
3
k + 1
)· (n − k), for even k.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 21/23
A Generalized Partitioning Algorithm
A0 A1 A2 A3 A4 A5 A6A5 A1
g1 g2 g3 i j g4 g5
5 pointer visits.
Analysis for partitioning yields:
E(Pptr-visitn ) =
k + 3
4· (n − k), for odd k ,(
k + 3
4+
3
k + 1
)· (n − k), for even k.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 21/23
A Generalized Partitioning Algorithm
A0 A1 A2 A3 A4 A5 A6A5 A1
g1 g2 g3 i j g4 g5
5 pointer visits.
Analysis for partitioning yields:
E(Pptr-visitn ) =
k + 3
4· (n − k), for odd k ,(
k + 3
4+
3
k + 1
)· (n − k), for even k.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 21/23
A Generalized Partitioning Algorithm
A0 A1 A2 A3 A4 A5 A6A5 A1
g1 g2 g3 i j g4 g5
5 pointer visits.
Analysis for partitioning yields:
E(Pptr-visitn ) =
k + 3
4· (n − k), for odd k ,(
k + 3
4+
3
k + 1
)· (n − k), for even k.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 21/23
A Generalized Partitioning Algorithm
A0 A1 A2 A3 A4 A5 A6A5 A1
g1 g2 g3 i j g4 g5
5 pointer visits.
Analysis for partitioning yields:
E(Pptr-visitn ) =
k + 3
4· (n − k), for odd k ,(
k + 3
4+
3
k + 1
)· (n − k), for even k.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 21/23
A Generalized Partitioning Algorithm
A0 A1 A2 A3 A4 A5 A6A5 A1
g1 g2 g3 i j g4 g5
5 pointer visits.
Analysis for partitioning yields:
E(Pptr-visitn ) =
k + 3
4· (n − k), for odd k ,(
k + 3
4+
3
k + 1
)· (n − k), for even k.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 21/23
A Generalized Partitioning Algorithm
A0 A1 A2 A3 A4 A5 A6A5 A1
g1 g2 g3 i j g4 g5
5 pointer visits.
Analysis for partitioning yields:
E(Pptr-visitn ) =
k + 3
4· (n − k), for odd k ,(
k + 3
4+
3
k + 1
)· (n − k), for even k.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 21/23
A Generalized Partitioning Algorithm
A0 A1 A2 A3 A4 A5 A6A5 A1
g1 g2 g3 i j g4 g5
5 pointer visits.
Analysis for partitioning yields:
E(Pptr-visitn ) =
k + 3
4· (n − k), for odd k ,(
k + 3
4+
3
k + 1
)· (n − k), for even k.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 21/23
A Generalized Partitioning Algorithm
A0 A1 A2 A3 A4 A5 A6A5 A1
g1 g2 g3 i j g4 g5
5 pointer visits.
Analysis for partitioning yields:
E(Pptr-visitn ) =
k + 3
4· (n − k), for odd k ,(
k + 3
4+
3
k + 1
)· (n − k), for even k.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 21/23
A Generalized Partitioning Algorithm
A0 A1 A2 A3 A4 A5 A6A5 A1
g1 g2 g3 i j g4 g5
5 pointer visits.
Analysis for partitioning yields:
E(Pptr-visitn ) =
k + 3
4· (n − k), for odd k ,(
k + 3
4+
3
k + 1
)· (n − k), for even k.
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 21/23
A Generalized Partitioning Algorithm
“Pointer visits” for sorting with k pivots: Minimum at k = 5.
1 2 3 4 5 6 7 8 9
1.4
1.6
1.8
2
# pivots
avg.
#p
oin
ter
visi
ts/n
lnn
Observations:
“pointer visits” predict L1 cache misses accuratelyclosest w.r.t. predicting empirical running time
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 22/23
A Generalized Partitioning Algorithm
“Pointer visits” for sorting with k pivots: Minimum at k = 5.
1 2 3 4 5 6 7 8 9
1.4
1.6
1.8
2
# pivots
avg.
#p
oin
ter
visi
ts/n
lnn
Observations:
“pointer visits” predict L1 cache misses accuratelyclosest w.r.t. predicting empirical running time
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 22/23
Conclusion & Open Problems
Theory:
Intuitive algorithm “Count” is comparison-optimal
Exact average comparison count for “Count”
“Count” variant for multi-pivot quicksort also optimal
But: Average comparison count unknown for k ≥ 5!
Theory & Practice:
Multi-Pivot quicksort has advantages in cache behavior
Does “pointer visits”/“scanned elements” cost measure producereliable running time predictions for other algorithms?
Thank you!
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 23/23
Conclusion & Open Problems
Theory:
Intuitive algorithm “Count” is comparison-optimal
Exact average comparison count for “Count”
“Count” variant for multi-pivot quicksort also optimal
But: Average comparison count unknown for k ≥ 5!
Theory & Practice:
Multi-Pivot quicksort has advantages in cache behavior
Does “pointer visits”/“scanned elements” cost measure producereliable running time predictions for other algorithms?
Thank you!
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 23/23
Running Time Experiments
21 22 23 24 25 26 27
4
4.2
4.4
Items [log2(n)]
Tim
e/n
lnn
[ns]
QS15 QS1 QS9 QS7 QS2 QS3 QS5
M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 1/1