it university of copenhagendual-pivot quicksort p 3 2 8 5 1 4 7 6 input: permutation of f1;:::;ng....

113
Multi-Pivot Quicksort Comparison-Optimal Algorithms and Beyond Martin Aum¨ uller IT University of Copenhagen April 4, 2016 ARCO Workshop M. Aum¨ uller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 1/23

Upload: others

Post on 14-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 2: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 3: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 4: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 5: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 6: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 7: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 8: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 9: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 10: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

Why Consider It?

(Talk “Sorting in JDK”, V. Yaroslavskiy, 2015.)

M. Aumuller Multi-Pivot Quicksort: Comparison-Optimal Algorithms and Beyond 3/23

Page 11: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 12: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 13: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 14: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 15: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 16: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 17: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 18: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 19: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 20: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 21: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 22: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 23: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 24: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 25: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 26: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 27: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 28: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 29: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 30: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 31: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 32: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 33: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 34: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 35: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 36: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 37: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 38: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 39: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 40: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 41: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 42: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 43: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 44: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 45: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 46: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 47: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 48: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 49: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 50: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 51: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 52: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 53: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 54: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 55: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 56: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 57: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 58: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 59: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

“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

Page 60: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

“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

Page 61: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

“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

Page 62: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

“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

Page 63: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 64: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 65: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 66: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 67: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 68: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 69: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 70: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 71: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 72: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 73: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 74: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 75: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 76: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 77: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 78: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 79: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 80: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 81: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 82: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 83: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 84: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 85: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 86: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 87: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 88: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 89: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 90: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 91: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 92: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 93: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 94: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 95: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 96: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 97: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 98: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 99: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 100: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 101: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 102: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 103: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 104: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 105: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 106: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 107: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 108: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 109: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 110: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 111: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 112: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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

Page 113: IT University of CopenhagenDual-Pivot Quicksort p 3 2 8 5 1 4 7 6 Input: Permutation of f1;:::;ng. 1.Choose two pivots p;q with p < q. 2.Partition, i.e., re-arrange elements. Partition:

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