History 1962 2009 1975 Classic Quicksort

Tony Hoare Dual Pivot Quicksort Robert Sedgewick New Dual Pivot Quicksort Yaroslavskiy 2012 One pivot

Crossing pointers technique Two pivots

More comparisons and swaps than classic Quicksort Also two pivots

Now used in Java 7 runtime library

Seems superior!

No good analysis available Why? 3 7 4 8 1 9 2 6 5 3 7 4 8 1 9 2 6 5 3 2 4 8 1 9 7 6 5 3 2 4 8 1 9 7 6 5 3 2 4 1 8 9 7 6 5 3 2 4 1 5 9 7 6 8 Quicksort: A close look to a recent improvement Michelle Meekes

15-11-2012 Classic Quicksort Example Sedgewick's Quicksort Algorithm Yaroslavskiy's Quicksort Example Algorithm Algorithm 1x 1x per value for k 1x per value for g 1x per value for k with A[k] ≥ p, "non-small elements" 1x per value for g with A[g] ≤ q, "non-large elements" Invariant Analysis

(New) Dual Pivot Quicksort Analysis Dual Pivot Quicksort Assumptions:

Input sequences are random permutations

First and last element are chosen as pivot All subfiles are preserving randomness Steps to make:

Solve the recurrence relation

Calculate costs per partitioning step (is linear)

Compare the results for Classic Quicksort, Sedgewick and Yaroslavskiy in:

# swaps

# comparisons Recurrence relation Closed form: for expected costs Analysis Dual Pivot Quicksort Conjecture p < q are pivots

For every x, determine x < p, p < x < q or q < x.

How often a compare-instruction is reached, depends on p and q.

The number of elements smaller, between and larger than p and q, depends on p an q too. 1x per value for i (without left) 1x per value for j (without right) 1x per value for i with A[i] ≤ q (without the last) 1x per value for j with A[j] ≥ p Analysis Results Least comparisons: Yaroslavskiy

Least swaps: Classic Quicksort Yaroslavskiy can outperform Classic Quicksort.

Depends on the relative runtime contribution. Sedgewick and Yaroslavskiy are based on the same idea.. Why do they differ so much in costs? Notation c@P = number of c-typed elements at positions in P l@M = 2, m@L=1, l@L=2, etc. Asymmetry! So... Yaroslavskiy's Quicksort takes advantage of these asymmetries... Where? l3: 1 comparison

l6 and l11: together n-1

l10: E[m@ K] + E[l@K]

l14: E[s@G] +E[m@G] c = n + E[m@K] + E[l@K] + E[s@G] + E[m@G]

≈ n + E[m@S] + E[m@M] + E[l@S] + E[l@M] + E[s@L] + E[m@L]

≈ 19/12 n ≈ 1.58 n n l3: 1 comparison

l6 and l12: together n-2

l8: at least E[s@I]

l13: at least E[l@J]

l8 and l13: together also E[#m] c' = n - 1 + E[#m] + E[s@I] + E[l@J]

≈ n + E[m@S,M,L] + E[s@S] + 0.5 E[s@M] + 0.5 E[l@M] + E[l@L]

≈ 1.75n n Sedgewick Yaroslavskiy c ≈ n + E[m@S] + E[m@M] + E[l@S] + E[l@M] + E[s@L] + E[m@L] c' ≈ n + E[m@S] + E[m@M] + [l@L]

+ E[s@S] + 0.5 E[s@M] + 0.5 E[l@M] + E[l@L] n n Exact variants of the three shown algorithms.

Tested on an Intel Core 2 Duo P8700 Laptop

Average of 1000 random permutations of each size Running times by Nebel and Wild Further work Why is the higher amount of swaps not a problem at all?

They did only show an analysis for random permutations.

Known improvements on Classic Quicksort can be applicable to Dual Pivot Quicksort. Conjecture Results S. Wild and M. E. Nebel History

Classic Quicksort

Dual Pivot Quicksort

Dual Pivot Quicksort Analysis

Running times

Further research 3 5 1 8 4 7 2 9 6 set pointers

3 5 1 8 4 7 2 9 6 5 is medium

3 5 1 8 4 7 2 9 6 1 is small

3 1 5 8 4 7 2 9 6 swap

3 1 5 8 4 7 2 9 6 8 is large

3 1 5 8 4 7 2 9 6 find swap partner

3 1 5 2 4 7 8 9 6 swap; 2 is small

3 1 2 5 4 7 8 9 6 swap

3 1 2 5 4 7 8 9 6 4 is medium; 7 is large

3 1 2 5 4 7 8 9 6 find swap partner; pointers cross

2 1 3 5 4 6 8 9 7 set pivots in place

### Present Remotely

Send the link below via email or IM

CopyPresent to your audience

Start remote presentation- Invited audience members
**will follow you**as you navigate and present - People invited to a presentation
**do not need a Prezi account** - This link expires
**10 minutes**after you close the presentation - A maximum of
**30 users**can follow your presentation - Learn more about this feature in our knowledge base article

# Dual Pivot Quicksort

No description

by

Tweet