Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Quicksorts of the 21st Century

Quicksorts of the 21st Century

Abstract
Quicksort is one of most well-understood algorithms – both in terms of theoretical performance guarantees and practical running time – and an implementation of quicksort is part of almost every programming library. While the first 3 decades after quicksort's publication have seen extensive engineering efforts, implementations seemed to have converged to a stable state. But now, there is again excitement within the algorithms community, triggered by the success of a new dual-pivot quicksort introduced in the Java 7 runtime library.
I will introduce this algorithm and present analytical evidence for my hypothesis why (a) dual-pivot quicksort is faster than the previously used (standard) quicksort and (b) why this basic improvement was not already found much earlier. Moreover, I will spotlight new algorithms inspired by quicksort and some unsolved riddles encountered during my decade with quicksort.

This talk was part of the special session celebrating quicksort's 60th birthday during the “Verified software: tools and experiments” workshop at the Isaac Newton Institute (https://www.newton.ac.uk/event/vsow04/).
A video recording of the talk is available here: https://www.newton.ac.uk/seminar/20210607173018001/

2a8430a8f786043f73f42dc9d88a7e5a?s=128

Sebastian Wild

June 16, 2021
Tweet

Transcript

  1. Quicksorts of the 21st century Sebastian Wild based on joint

    work with Stefan Edelkamp, Conrado Martínez, Markus Nebel, Ralph Neininger, and Armin Weiß Isaac Newton Institute – Verified Software Workshop Sebastian Wild Quicksorts of the 21st century 2021-06-07 0 / 17
  2. Outline 1 Context 1 Context 2 Dual-Pivot Quicksort 2 Dual-Pivot

    Quicksort 3 Average-Case Analysis 3 Average-Case Analysis 4 Quicksort + X 4 Quicksort + X 5 Conclusion 5 Conclusion Sebastian Wild Quicksorts of the 21st century 2021-06-07 0 / 17
  3. 1 Context 1 Context Sebastian Wild Quicksorts of the 21st

    century 2021-06-07 0 / 17
  4. Algorithm Science Experimental Algorithmics Algorithm Theory Sebastian Wild Quicksorts of

    the 21st century 2021-06-07 1 / 17
  5. Algorithm Science Experimental Algorithmics measured performance Algorithm Theory Sebastian Wild

    Quicksorts of the 21st century 2021-06-07 1 / 17
  6. Algorithm Science Experimental Algorithmics measured performance specific to machine Algorithm

    Theory Sebastian Wild Quicksorts of the 21st century 2021-06-07 1 / 17
  7. Algorithm Science Experimental Algorithmics measured performance specific to machine reproducible?

    Algorithm Theory Sebastian Wild Quicksorts of the 21st century 2021-06-07 1 / 17
  8. Algorithm Science Experimental Algorithmics measured performance specific to machine reproducible?

    does it generalize? Algorithm Theory Sebastian Wild Quicksorts of the 21st century 2021-06-07 1 / 17
  9. Algorithm Science Experimental Algorithmics measured performance specific to machine reproducible?

    does it generalize? Algorithm Theory Big-Oh worst case analysis Sebastian Wild Quicksorts of the 21st century 2021-06-07 1 / 17
  10. Algorithm Science Experimental Algorithmics measured performance specific to machine reproducible?

    does it generalize? Algorithm Theory Big-Oh worst case analysis no actual prediction! (hidden constants!) Sebastian Wild Quicksorts of the 21st century 2021-06-07 1 / 17
  11. Algorithm Science Experimental Algorithmics measured performance specific to machine reproducible?

    does it generalize? Algorithm Theory Big-Oh worst case analysis no actual prediction! (hidden constants!) pessimistic (worst case) Sebastian Wild Quicksorts of the 21st century 2021-06-07 1 / 17
  12. Algorithm Science Experimental Algorithmics measured performance specific to machine reproducible?

    does it generalize? Algorithm Theory Big-Oh worst case analysis no actual prediction! (hidden constants!) pessimistic (worst case) does it say anything? Sebastian Wild Quicksorts of the 21st century 2021-06-07 1 / 17
  13. Algorithm Science Experimental Algorithmics measured performance specific to machine reproducible?

    does it generalize? Algorithm Theory Big-Oh worst case analysis no actual prediction! (hidden constants!) pessimistic (worst case) does it say anything? Platform-independent performance guarantees concrete, useful predictions concrete, analyzable model validated by experiments Sebastian Wild Quicksorts of the 21st century 2021-06-07 1 / 17
  14. Case Study: Sorting in Java Library Why Java? Open-Source reference

    implementation OpenJDK Basis for Oracle JRE, Android JRE might contain executed algorithms in existence! within new millenium: all-new sorting methods! Sebastian Wild Quicksorts of the 21st century 2021-06-07 2 / 17
  15. Case Study: Sorting in Java Library Why Java? Open-Source reference

    implementation OpenJDK Basis for Oracle JRE, Android JRE might contain executed algorithms in existence! within new millenium: all-new sorting methods! Sebastian Wild Quicksorts of the 21st century 2021-06-07 2 / 17
  16. Case Study: Sorting in Java Library Why Java? Open-Source reference

    implementation OpenJDK Basis for Oracle JRE, Android JRE might contain executed algorithms in existence! within new millenium: all-new sorting methods! Sebastian Wild Quicksorts of the 21st century 2021-06-07 2 / 17
  17. Case Study: Sorting in Java Library Why Java? Open-Source reference

    implementation OpenJDK Basis for Oracle JRE, Android JRE might contain executed algorithms in existence! within new millenium: all-new sorting methods! Sebastian Wild Quicksorts of the 21st century 2021-06-07 2 / 17
  18. Case Study: Sorting in Java Library Why Java? Open-Source reference

    implementation OpenJDK Basis for Oracle JRE, Android JRE might contain executed algorithms in existence! within new millenium: all-new sorting methods! 1 Dual-Pivot Quicksort for primitive types (int[], double[] etc.) 2008 Started as hobby project by Vladimir Yaroslavskiy (Sun Microsystems) since 2009 in OpenJDK Sebastian Wild Quicksorts of the 21st century 2021-06-07 2 / 17
  19. Case Study: Sorting in Java Library Why Java? Open-Source reference

    implementation OpenJDK Basis for Oracle JRE, Android JRE might contain executed algorithms in existence! within new millenium: all-new sorting methods! 1 Dual-Pivot Quicksort for primitive types (int[], double[] etc.) 2008 Started as hobby project by Vladimir Yaroslavskiy (Sun Microsystems) since 2009 in OpenJDK 2 Timsort for objects (Object[]) adaptive stable mergesort 2002 developed for list.sort in CPython by Tim Peters since 2009 in OpenJDK Sebastian Wild Quicksorts of the 21st century 2021-06-07 2 / 17
  20. Case Study: Sorting in Java Library Why Java? Open-Source reference

    implementation OpenJDK Basis for Oracle JRE, Android JRE might contain executed algorithms in existence! within new millenium: all-new sorting methods! 1 Dual-Pivot Quicksort for primitive types (int[], double[] etc.) 2008 Started as hobby project by Vladimir Yaroslavskiy (Sun Microsystems) since 2009 in OpenJDK 2 Timsort for objects (Object[]) adaptive stable mergesort 2002 developed for list.sort in CPython by Tim Peters since 2009 in OpenJDK both algorithms from developers Sebastian Wild Quicksorts of the 21st century 2021-06-07 2 / 17
  21. Case Study: Sorting in Java Library Why Java? Open-Source reference

    implementation OpenJDK Basis for Oracle JRE, Android JRE might contain executed algorithms in existence! within new millenium: all-new sorting methods! 1 Dual-Pivot Quicksort for primitive types (int[], double[] etc.) 2008 Started as hobby project by Vladimir Yaroslavskiy (Sun Microsystems) since 2009 in OpenJDK 2 Timsort for objects (Object[]) adaptive stable mergesort 2002 developed for list.sort in CPython by Tim Peters since 2009 in OpenJDK both algorithms from developers Focus: Benchmarks & Unit-Tests not on proofs or analysis Sebastian Wild Quicksorts of the 21st century 2021-06-07 2 / 17
  22. Why a new Quicksort? What happened in 2009? Why retire

    a well-tested sorting method? till 2009, Java used standard quicksort Java 7 uses new dual-pivot quicksort Sebastian Wild Quicksorts of the 21st century 2021-06-07 3 / 17
  23. Why a new Quicksort? What happened in 2009? Why retire

    a well-tested sorting method? till 2009, Java used standard quicksort Java 7 uses new dual-pivot quicksort Sebastian Wild Quicksorts of the 21st century 2021-06-07 3 / 17
  24. Why a new Quicksort? What happened in 2009? Why retire

    a well-tested sorting method? till 2009, Java used standard quicksort Java 7 uses new dual-pivot quicksort Sebastian Wild Quicksorts of the 21st century 2021-06-07 3 / 17
  25. Why a new Quicksort? What happened in 2009? Why retire

    a well-tested sorting method? till 2009, Java used standard crossing-pointer partitioning + median-of-three + Insertionsort quicksort Java 7 uses new dual-pivot quicksort Sebastian Wild Quicksorts of the 21st century 2021-06-07 3 / 17
  26. Why a new Quicksort? What happened in 2009? Why retire

    a well-tested sorting method? till 2009, Java used standard crossing-pointer partitioning + median-of-three + Insertionsort quick like (almost) all libraries sort Java 7 uses new dual-pivot quicksort 0 0.5 1 1.5 2 ·106 7 8 9 (121ms to sort 1M items) n time 10−6 · n ln n Java 6 sorting method Normalized Java runtimes (in ms). Average and standard deviation of 1000 random permutations per size. Sebastian Wild Quicksorts of the 21st century 2021-06-07 3 / 17
  27. Why a new Quicksort? What happened in 2009? Why retire

    a well-tested sorting method? till 2009, Java used standard crossing-pointer partitioning + median-of-three + Insertionsort quick like (almost) all libraries sort Java 7 uses new dual-pivot quicksort 0 0.5 1 1.5 2 ·106 7 8 9 (121ms to sort 1M items) n time 10−6 · n ln n Java 6 sorting method Normalized Java runtimes (in ms). Average and standard deviation of 1000 random permutations per size. Sebastian Wild Quicksorts of the 21st century 2021-06-07 3 / 17
  28. Why a new Quicksort? What happened in 2009? Why retire

    a well-tested sorting method? till 2009, Java used standard crossing-pointer partitioning + median-of-three + Insertionsort quick like (almost) all libraries sort Java 7 uses new dual-pivot quicksort 20 % faster than highly-tuned Java 6! 0 0.5 1 1.5 2 ·106 7 8 9 (121ms to sort 1M items) (93ms to sort 1M items) n time 10−6 · n ln n Java 6 sorting method Java 7 sorting method Normalized Java runtimes (in ms). Average and standard deviation of 1000 random permutations per size. Sebastian Wild Quicksorts of the 21st century 2021-06-07 3 / 17
  29. Why a new Quicksort? What happened in 2009? Why retire

    a well-tested sorting method? till 2009, Java used standard crossing-pointer partitioning + median-of-three + Insertionsort quick like (almost) all libraries sort Java 7 uses new dual-pivot quicksort 20 % faster than highly-tuned Java 6! similarly for core algorithms! 0 0.5 1 1.5 2 ·106 7 8 9 (121ms to sort 1M items) (93ms to sort 1M items) n time 10−6 · n ln n Java 6 sorting method Java 7 sorting method classic quicksort YBB quicksort Normalized Java runtimes (in ms). Average and standard deviation of 1000 random permutations per size. Sebastian Wild Quicksorts of the 21st century 2021-06-07 3 / 17
  30. Why a new Quicksort? What happened in 2009? Why retire

    a well-tested sorting method? till 2009, Java used standard crossing-pointer partitioning + median-of-three + Insertionsort quick like (almost) all libraries sort Java 7 uses new dual-pivot quicksort 20 % faster than highly-tuned Java 6! similarly for core algorithms! 0 0.5 1 1.5 2 ·106 7 8 9 (121ms to sort 1M items) (93ms to sort 1M items) n time 10−6 · n ln n Java 6 sorting method Java 7 sorting method classic quicksort YBB quicksort Normalized Java runtimes (in ms). Average and standard deviation of 1000 random permutations per size. No sound explanation for running time known in 2009! Sebastian Wild Quicksorts of the 21st century 2021-06-07 3 / 17
  31. Why a new Quicksort? What happened in 2009? Why retire

    a well-tested sorting method? till 2009, Java used standard crossing-pointer partitioning + median-of-three + Insertionsort quick like (almost) all libraries sort Java 7 uses new dual-pivot quicksort 20 % faster than highly-tuned Java 6! similarly for core algorithms! 0 0.5 1 1.5 2 ·106 7 8 9 (121ms to sort 1M items) (93ms to sort 1M items) n time 10−6 · n ln n Java 6 sorting method Java 7 sorting method classic quicksort YBB quicksort Normalized Java runtimes (in ms). Average and standard deviation of 1000 random permutations per size. No sound explanation for running time known in 2009! Is it an artifact of Java? Sebastian Wild Quicksorts of the 21st century 2021-06-07 3 / 17
  32. Why a new Quicksort? What happened in 2009? Why retire

    a well-tested sorting method? till 2009, Java used standard crossing-pointer partitioning + median-of-three + Insertionsort quick like (almost) all libraries sort Java 7 uses new dual-pivot quicksort 20 % faster than highly-tuned Java 6! similarly for core algorithms! 0 0.5 1 1.5 2 ·106 7 8 9 (121ms to sort 1M items) (93ms to sort 1M items) n time 10−6 · n ln n Java 6 sorting method Java 7 sorting method classic quicksort YBB quicksort Normalized Java runtimes (in ms). Average and standard deviation of 1000 random permutations per size. No sound explanation for running time known in 2009! Is it an artifact of Java? Rather no; similar results in C++ ... Sebastian Wild Quicksorts of the 21st century 2021-06-07 3 / 17
  33. Why a new Quicksort? What happened in 2009? Why retire

    a well-tested sorting method? till 2009, Java used standard crossing-pointer partitioning + median-of-three + Insertionsort quick like (almost) all libraries sort Java 7 uses new dual-pivot quicksort 20 % faster than highly-tuned Java 6! similarly for core algorithms! 0 0.5 1 1.5 2 ·106 7 8 9 (121ms to sort 1M items) (93ms to sort 1M items) n time 10−6 · n ln n Java 6 sorting method Java 7 sorting method classic quicksort YBB quicksort Normalized Java runtimes (in ms). Average and standard deviation of 1000 random permutations per size. No sound explanation for running time known in 2009! Is it an artifact of Java? Rather no; similar results in C++ ... Why did no-one come up with this earlier? Sebastian Wild Quicksorts of the 21st century 2021-06-07 3 / 17
  34. Outline 1 Context 1 Context 2 Dual-Pivot Quicksort 2 Dual-Pivot

    Quicksort 3 Average-Case Analysis 3 Average-Case Analysis 4 Quicksort + X 4 Quicksort + X 5 Conclusion 5 Conclusion Sebastian Wild Quicksorts of the 21st century 2021-06-07 3 / 17
  35. 2 Dual-Pivot Quicksort 2 Dual-Pivot Quicksort Sebastian Wild Quicksorts of

    the 21st century 2021-06-07 3 / 17
  36. Dual-Pivot Quicksort: Conceptual View Standard Quicksort 1 Choose a pivot

    element P. 2 For each element x, determine its class by comparing x to P. small if x < P large if P < x 3 Rearrange elements according to classes: P 4 Recurse on left & right subarray. Sebastian Wild Quicksorts of the 21st century 2021-06-07 4 / 17
  37. Dual-Pivot Quicksort: Conceptual View Standard Quicksort 1 Choose a pivot

    element P. 2 For each element x, determine its class by comparing x to P. small if x < P large if P < x 3 Rearrange elements according to classes: P 4 Recurse on left & right subarray. Sebastian Wild Quicksorts of the 21st century 2021-06-07 4 / 17
  38. Dual-Pivot Quicksort: Conceptual View Standard Quicksort 1 Choose a pivot

    element P. 2 For each element x, determine its class by comparing x to P. small if x < P large if P < x 3 Rearrange elements according to classes: P 4 Recurse on left & right subarray. Sebastian Wild Quicksorts of the 21st century 2021-06-07 4 / 17
  39. Dual-Pivot Quicksort: Conceptual View Standard Quicksort 1 Choose a pivot

    element P. 2 For each element x, determine its class by comparing x to P. small if x < P assuming distinct elements in this talk large if P < x 3 Rearrange elements according to classes: P 4 Recurse on left & right subarray. Sebastian Wild Quicksorts of the 21st century 2021-06-07 4 / 17
  40. Dual-Pivot Quicksort: Conceptual View Standard Quicksort 1 Choose a pivot

    element P. 2 For each element x, determine its class by comparing x to P. small if x < P assuming distinct elements in this talk large if P < x 3 Rearrange elements according to classes: P 4 Recurse on left & right subarray. Sebastian Wild Quicksorts of the 21st century 2021-06-07 4 / 17
  41. Dual-Pivot Quicksort: Conceptual View Standard Quicksort 1 Choose a pivot

    element P. 2 For each element x, determine its class by comparing x to P. small if x < P assuming distinct elements in this talk large if P < x 3 Rearrange elements according to classes: P 4 Recurse on left & right subarray. Sebastian Wild Quicksorts of the 21st century 2021-06-07 4 / 17
  42. Dual-Pivot Quicksort: Conceptual View Standard Quicksort 1 Choose a pivot

    element P. 2 For each element x, determine its class by comparing x to P. small if x < P assuming distinct elements in this talk large if P < x 3 Rearrange elements according to classes: P 4 Recurse on left & right subarray. Dual-Pivot Quicksort 1 Choose two pivots P1 P2 2 For each element x, determine its class by comparing x to pivots P1 and P2 small for x < P1 medium for P1 < x < P2 large for P2 < x 3 Arrange elements according to classes: P1 P2 4 Sort subarrays recursively. Sebastian Wild Quicksorts of the 21st century 2021-06-07 4 / 17
  43. Dual-Pivot Quicksort: Conceptual View Standard Quicksort 1 Choose a pivot

    element P. 2 For each element x, determine its class by comparing x to P. small if x < P assuming distinct elements in this talk large if P < x 3 Rearrange elements according to classes: P 4 Recurse on left & right subarray. Dual-Pivot Quicksort 1 Choose two pivots P1 P2 2 For each element x, determine its class by comparing x to pivots P1 and P2 small for x < P1 medium for P1 < x < P2 large for P2 < x 3 Arrange elements according to classes: P1 P2 4 Sort subarrays recursively. Sebastian Wild Quicksorts of the 21st century 2021-06-07 4 / 17
  44. Dual-Pivot Quicksort: Conceptual View Standard Quicksort 1 Choose a pivot

    element P. 2 For each element x, determine its class by comparing x to P. small if x < P assuming distinct elements in this talk large if P < x 3 Rearrange elements according to classes: P 4 Recurse on left & right subarray. Dual-Pivot Quicksort 1 Choose two pivots P1 P2 2 For each element x, determine its class by comparing x to pivots P1 and P2 small for x < P1 medium for P1 < x < P2 large for P2 < x 3 Arrange elements according to classes: P1 P2 4 Sort subarrays recursively. Sebastian Wild Quicksorts of the 21st century 2021-06-07 4 / 17
  45. Dual-Pivot Quicksort: Conceptual View Standard Quicksort 1 Choose a pivot

    element P. 2 For each element x, determine its class by comparing x to P. small if x < P assuming distinct elements in this talk large if P < x 3 Rearrange elements according to classes: P 4 Recurse on left & right subarray. Dual-Pivot Quicksort 1 Choose two pivots P1 P2 2 For each element x, determine its class by comparing x to pivots P1 and P2 small for x < P1 medium for P1 < x < P2 large for P2 < x 3 Ar Dijkstra’s Dutch National Flag Problem range elements according to classes: P1 P2 4 Sort subarrays recursively. Sebastian Wild Quicksorts of the 21st century 2021-06-07 4 / 17
  46. Dual-Pivot Quicksort: Conceptual View Standard Quicksort 1 Choose a pivot

    element P. 2 For each element x, determine its class by comparing x to P. small if x < P assuming distinct elements in this talk large if P < x 3 Rearrange elements according to classes: P 4 Recurse on left & right subarray. Dual-Pivot Quicksort 1 Choose two pivots P1 P2 2 For each element x, determine its class by comparing x to pivots P1 and P2 small for x < P1 medium for P1 < x < P2 large for P2 < x 3 Ar Dijkstra’s Dutch National Flag Problem range elements according to classes: P1 P2 4 Sort subarrays recursively. Sebastian Wild Quicksorts of the 21st century 2021-06-07 4 / 17
  47. Dual-Pivot Quicksort: Conceptual View Standard Quicksort 1 Choose a pivot

    element P. 2 For each element x, determine its class by comparing x to P. small if x < P assuming distinct elements in this talk large if P < x 3 Rearrange elements according to classes: P 4 Recurse on left & right subarray. Dual-Pivot Quicksort 1 Choose two pivots P1 P2 2 For each element x, determine its class by comparing x to pivots P1 and P2 small for x < P1 medium for P1 < x < P2 large for P2 < x 3 Ar Dijkstra’s Dutch National Flag Problem range elements according to classes: P1 P2 4 Sort subarrays recursively. How to do 3 efficiently and in place? YBB Partitioning Sebastian Wild Quicksorts of the 21st century 2021-06-07 4 / 17
  48. Dual-Pivot Quicksort: Conceptual View Standard Quicksort 1 Choose a pivot

    element P. 2 For each element x, determine its class by comparing x to P. small if x < P assuming distinct elements in this talk large if P < x 3 Rearrange elements according to classes: P 4 Recurse on left & right subarray. Dual-Pivot Quicksort 1 Choose two pivots P1 P2 2 For each element x, determine its class by comparing x to pivots P1 and P2 small for x < P1 medium for P1 < x < P2 large for P2 < x 3 Ar Dijkstra’s Dutch National Flag Problem range elements according to classes: P1 P2 4 Sort subarrays recursively. How to do 3 efficiently and in place? YBB Partitioning Sebastian Wild Quicksorts of the 21st century 2021-06-07 4 / 17
  49. YBB Partitioning Example < P1 ? swap < P2 ?

    skip swap g     > P2 ? < P1 ? skip swap swap k     P1 P2 3 5 1 7 4 2 8 6 g k P1 P2 Invariant: YBBQUICKSORT(A, left, right) 1 if right left then return 2 P1 := A[left]; P2 := A[right] 3 if P1 > P2 then Swap P1 and P2 end if 4 := left + 1; g := right − 1; k := 5 while k g 6 if A[k] < P1 7 Swap A[k] and A[ ] ; := + 1 8 else if A[k] P2 9 while A[g] > P2 and k < g 10 g := g − 1 11 end while 12 Swap A[k] and A[g] ; g := g − 1 13 if A[k] < P1 14 Swap A[k] and A[ ] ; := + 1 15 end if 16 end if 17 k := k + 1 18 end while 19 := − 1; g := g + 1 20 Swap A[left] and A[ ] ; Swap A[right] and A[g] 21 YBBQUICKSORT(A, left , − 1) 22 YBBQUICKSORT(A, + 1, g − 1) 23 YBBQUICKSORT(A, g + 1, right ) Sebastian Wild Quicksorts of the 21st century 2021-06-07 5 / 17
  50. YBB Partitioning Example < P1 ? swap < P2 ?

    skip swap g     > P2 ? < P1 ? skip swap swap k     P1 P2 3 5 1 7 4 2 8 6 g k P1 P2 Invariant: YBBQUICKSORT(A, left, right) 1 if right left then return 2 P1 := A[left]; P2 := A[right] 3 if P1 > P2 then Swap P1 and P2 end if 4 := left + 1; g := right − 1; k := 5 while k g 6 if A[k] < P1 7 Swap A[k] and A[ ] ; := + 1 8 else if A[k] P2 9 while A[g] > P2 and k < g 10 g := g − 1 11 end while 12 Swap A[k] and A[g] ; g := g − 1 13 if A[k] < P1 14 Swap A[k] and A[ ] ; := + 1 15 end if 16 end if 17 k := k + 1 18 end while 19 := − 1; g := g + 1 20 Swap A[left] and A[ ] ; Swap A[right] and A[g] 21 YBBQUICKSORT(A, left , − 1) 22 YBBQUICKSORT(A, + 1, g − 1) 23 YBBQUICKSORT(A, g + 1, right ) Sebastian Wild Quicksorts of the 21st century 2021-06-07 5 / 17
  51. YBB Partitioning Example < P1 ? swap < P2 ?

    skip swap g     > P2 ? < P1 ? skip swap swap k     P1 P2 3 5 1 7 4 2 8 6 g k P1 P2 Invariant: YBBQUICKSORT(A, left, right) 1 if right left then return 2 P1 := A[left]; P2 := A[right] 3 if P1 > P2 then Swap P1 and P2 end if 4 := left + 1; g := right − 1; k := 5 while k g 6 if A[k] < P1 7 Swap A[k] and A[ ] ; := + 1 8 else if A[k] P2 9 while A[g] > P2 and k < g 10 g := g − 1 11 end while 12 Swap A[k] and A[g] ; g := g − 1 13 if A[k] < P1 14 Swap A[k] and A[ ] ; := + 1 15 end if 16 end if 17 k := k + 1 18 end while 19 := − 1; g := g + 1 20 Swap A[left] and A[ ] ; Swap A[right] and A[g] 21 YBBQUICKSORT(A, left , − 1) 22 YBBQUICKSORT(A, + 1, g − 1) 23 YBBQUICKSORT(A, g + 1, right ) Sebastian Wild Quicksorts of the 21st century 2021-06-07 5 / 17
  52. YBB Partitioning Example < P1 ? swap < P2 ?

    skip swap g     > P2 ? < P1 ? skip swap swap k     P1 P2 3 5 1 7 4 2 8 6 g k P1 P2 Invariant: YBBQUICKSORT(A, left, right) 1 if right left then return 2 P1 := A[left]; P2 := A[right] 3 if P1 > P2 then Swap P1 and P2 end if 4 := left + 1; g := right − 1; k := 5 while k g 6 if A[k] < P1 7 Swap A[k] and A[ ] ; := + 1 8 else if A[k] P2 9 while A[g] > P2 and k < g 10 g := g − 1 11 end while 12 Swap A[k] and A[g] ; g := g − 1 13 if A[k] < P1 14 Swap A[k] and A[ ] ; := + 1 15 end if 16 end if 17 k := k + 1 18 end while 19 := − 1; g := g + 1 20 Swap A[left] and A[ ] ; Swap A[right] and A[g] 21 YBBQUICKSORT(A, left , − 1) 22 YBBQUICKSORT(A, + 1, g − 1) 23 YBBQUICKSORT(A, g + 1, right ) Sebastian Wild Quicksorts of the 21st century 2021-06-07 5 / 17
  53. YBB Partitioning Example < P1 ? swap < P2 ?

    skip swap g     > P2 ? < P1 ? skip swap swap k     P1 P2 3 5 1 7 4 2 8 6 g k P1≤◦≤P2 P1 P2 Invariant: YBBQUICKSORT(A, left, right) 1 if right left then return 2 P1 := A[left]; P2 := A[right] 3 if P1 > P2 then Swap P1 and P2 end if 4 := left + 1; g := right − 1; k := 5 while k g 6 if A[k] < P1 7 Swap A[k] and A[ ] ; := + 1 8 else if A[k] P2 9 while A[g] > P2 and k < g 10 g := g − 1 11 end while 12 Swap A[k] and A[g] ; g := g − 1 13 if A[k] < P1 14 Swap A[k] and A[ ] ; := + 1 15 end if 16 end if 17 k := k + 1 18 end while 19 := − 1; g := g + 1 20 Swap A[left] and A[ ] ; Swap A[right] and A[g] 21 YBBQUICKSORT(A, left , − 1) 22 YBBQUICKSORT(A, + 1, g − 1) 23 YBBQUICKSORT(A, g + 1, right ) Sebastian Wild Quicksorts of the 21st century 2021-06-07 5 / 17
  54. YBB Partitioning Example < P1 ? swap < P2 ?

    skip swap g     > P2 ? < P1 ? skip swap swap k     P1 P2 3 5 1 7 4 2 8 6 g k P1≤◦≤P2 P1 P2 Invariant: YBBQUICKSORT(A, left, right) 1 if right left then return 2 P1 := A[left]; P2 := A[right] 3 if P1 > P2 then Swap P1 and P2 end if 4 := left + 1; g := right − 1; k := 5 while k g 6 if A[k] < P1 7 Swap A[k] and A[ ] ; := + 1 8 else if A[k] P2 9 while A[g] > P2 and k < g 10 g := g − 1 11 end while 12 Swap A[k] and A[g] ; g := g − 1 13 if A[k] < P1 14 Swap A[k] and A[ ] ; := + 1 15 end if 16 end if 17 k := k + 1 18 end while 19 := − 1; g := g + 1 20 Swap A[left] and A[ ] ; Swap A[right] and A[g] 21 YBBQUICKSORT(A, left , − 1) 22 YBBQUICKSORT(A, + 1, g − 1) 23 YBBQUICKSORT(A, g + 1, right ) Sebastian Wild Quicksorts of the 21st century 2021-06-07 5 / 17
  55. YBB Partitioning Example < P1 ? swap < P2 ?

    skip swap g     > P2 ? < P1 ? skip swap swap k     P1 P2 3 1 5 7 4 2 8 6 g k P1≤◦≤P2 P1 P2 Invariant: YBBQUICKSORT(A, left, right) 1 if right left then return 2 P1 := A[left]; P2 := A[right] 3 if P1 > P2 then Swap P1 and P2 end if 4 := left + 1; g := right − 1; k := 5 while k g 6 if A[k] < P1 7 Swap A[k] and A[ ] ; := + 1 8 else if A[k] P2 9 while A[g] > P2 and k < g 10 g := g − 1 11 end while 12 Swap A[k] and A[g] ; g := g − 1 13 if A[k] < P1 14 Swap A[k] and A[ ] ; := + 1 15 end if 16 end if 17 k := k + 1 18 end while 19 := − 1; g := g + 1 20 Swap A[left] and A[ ] ; Swap A[right] and A[g] 21 YBBQUICKSORT(A, left , − 1) 22 YBBQUICKSORT(A, + 1, g − 1) 23 YBBQUICKSORT(A, g + 1, right ) Sebastian Wild Quicksorts of the 21st century 2021-06-07 5 / 17
  56. YBB Partitioning Example < P1 ? swap < P2 ?

    skip swap g     > P2 ? < P1 ? skip swap swap k     P1 P2 3 1 5 7 4 2 8 6 g k < P1 P1≤◦≤P2 P1 P2 Invariant: YBBQUICKSORT(A, left, right) 1 if right left then return 2 P1 := A[left]; P2 := A[right] 3 if P1 > P2 then Swap P1 and P2 end if 4 := left + 1; g := right − 1; k := 5 while k g 6 if A[k] < P1 7 Swap A[k] and A[ ] ; := + 1 8 else if A[k] P2 9 while A[g] > P2 and k < g 10 g := g − 1 11 end while 12 Swap A[k] and A[g] ; g := g − 1 13 if A[k] < P1 14 Swap A[k] and A[ ] ; := + 1 15 end if 16 end if 17 k := k + 1 18 end while 19 := − 1; g := g + 1 20 Swap A[left] and A[ ] ; Swap A[right] and A[g] 21 YBBQUICKSORT(A, left , − 1) 22 YBBQUICKSORT(A, + 1, g − 1) 23 YBBQUICKSORT(A, g + 1, right ) Sebastian Wild Quicksorts of the 21st century 2021-06-07 5 / 17
  57. YBB Partitioning Example < P1 ? swap < P2 ?

    skip swap g     > P2 ? < P1 ? skip swap swap k     P1 P2 3 1 5 7 4 2 8 6 g k < P1 P1≤◦≤P2 P1 P2 Invariant: YBBQUICKSORT(A, left, right) 1 if right left then return 2 P1 := A[left]; P2 := A[right] 3 if P1 > P2 then Swap P1 and P2 end if 4 := left + 1; g := right − 1; k := 5 while k g 6 if A[k] < P1 7 Swap A[k] and A[ ] ; := + 1 8 else if A[k] P2 9 while A[g] > P2 and k < g 10 g := g − 1 11 end while 12 Swap A[k] and A[g] ; g := g − 1 13 if A[k] < P1 14 Swap A[k] and A[ ] ; := + 1 15 end if 16 end if 17 k := k + 1 18 end while 19 := − 1; g := g + 1 20 Swap A[left] and A[ ] ; Swap A[right] and A[g] 21 YBBQUICKSORT(A, left , − 1) 22 YBBQUICKSORT(A, + 1, g − 1) 23 YBBQUICKSORT(A, g + 1, right ) Sebastian Wild Quicksorts of the 21st century 2021-06-07 5 / 17
  58. YBB Partitioning Example < P1 ? swap < P2 ?

    skip swap g     > P2 ? < P1 ? skip swap swap k     P1 P2 3 1 5 7 4 2 8 6 g k < P1 P1≤◦≤P2 P1 P2 Invariant: YBBQUICKSORT(A, left, right) 1 if right left then return 2 P1 := A[left]; P2 := A[right] 3 if P1 > P2 then Swap P1 and P2 end if 4 := left + 1; g := right − 1; k := 5 while k g 6 if A[k] < P1 7 Swap A[k] and A[ ] ; := + 1 8 else if A[k] P2 9 while A[g] > P2 and k < g 10 g := g − 1 11 end while 12 Swap A[k] and A[g] ; g := g − 1 13 if A[k] < P1 14 Swap A[k] and A[ ] ; := + 1 15 end if 16 end if 17 k := k + 1 18 end while 19 := − 1; g := g + 1 20 Swap A[left] and A[ ] ; Swap A[right] and A[g] 21 YBBQUICKSORT(A, left , − 1) 22 YBBQUICKSORT(A, + 1, g − 1) 23 YBBQUICKSORT(A, g + 1, right ) Sebastian Wild Quicksorts of the 21st century 2021-06-07 5 / 17
  59. YBB Partitioning Example < P1 ? swap < P2 ?

    skip swap g     > P2 ? < P1 ? skip swap swap k     P1 P2 3 1 5 7 4 2 8 6 g k < P1 P1≤◦≤P2 P1 P2 Invariant: YBBQUICKSORT(A, left, right) 1 if right left then return 2 P1 := A[left]; P2 := A[right] 3 if P1 > P2 then Swap P1 and P2 end if 4 := left + 1; g := right − 1; k := 5 while k g 6 if A[k] < P1 7 Swap A[k] and A[ ] ; := + 1 8 else if A[k] P2 9 while A[g] > P2 and k < g 10 g := g − 1 11 end while 12 Swap A[k] and A[g] ; g := g − 1 13 if A[k] < P1 14 Swap A[k] and A[ ] ; := + 1 15 end if 16 end if 17 k := k + 1 18 end while 19 := − 1; g := g + 1 20 Swap A[left] and A[ ] ; Swap A[right] and A[g] 21 YBBQUICKSORT(A, left , − 1) 22 YBBQUICKSORT(A, + 1, g − 1) 23 YBBQUICKSORT(A, g + 1, right ) Sebastian Wild Quicksorts of the 21st century 2021-06-07 5 / 17
  60. YBB Partitioning Example < P1 ? swap < P2 ?

    skip swap g     > P2 ? < P1 ? skip swap swap k     P1 P2 3 1 5 7 4 2 8 6 g k < P1 P1≤◦≤P2 P1 P2 Invariant: YBBQUICKSORT(A, left, right) 1 if right left then return 2 P1 := A[left]; P2 := A[right] 3 if P1 > P2 then Swap P1 and P2 end if 4 := left + 1; g := right − 1; k := 5 while k g 6 if A[k] < P1 7 Swap A[k] and A[ ] ; := + 1 8 else if A[k] P2 9 while A[g] > P2 and k < g 10 g := g − 1 11 end while 12 Swap A[k] and A[g] ; g := g − 1 13 if A[k] < P1 14 Swap A[k] and A[ ] ; := + 1 15 end if 16 end if 17 k := k + 1 18 end while 19 := − 1; g := g + 1 20 Swap A[left] and A[ ] ; Swap A[right] and A[g] 21 YBBQUICKSORT(A, left , − 1) 22 YBBQUICKSORT(A, + 1, g − 1) 23 YBBQUICKSORT(A, g + 1, right ) Sebastian Wild Quicksorts of the 21st century 2021-06-07 5 / 17
  61. YBB Partitioning Example < P1 ? swap < P2 ?

    skip swap g     > P2 ? < P1 ? skip swap swap k     P1 P2 3 1 5 7 4 2 8 6 g k < P1 P1≤◦≤P2 ≥ P2 P1 P2 Invariant: YBBQUICKSORT(A, left, right) 1 if right left then return 2 P1 := A[left]; P2 := A[right] 3 if P1 > P2 then Swap P1 and P2 end if 4 := left + 1; g := right − 1; k := 5 while k g 6 if A[k] < P1 7 Swap A[k] and A[ ] ; := + 1 8 else if A[k] P2 9 while A[g] > P2 and k < g 10 g := g − 1 11 end while 12 Swap A[k] and A[g] ; g := g − 1 13 if A[k] < P1 14 Swap A[k] and A[ ] ; := + 1 15 end if 16 end if 17 k := k + 1 18 end while 19 := − 1; g := g + 1 20 Swap A[left] and A[ ] ; Swap A[right] and A[g] 21 YBBQUICKSORT(A, left , − 1) 22 YBBQUICKSORT(A, + 1, g − 1) 23 YBBQUICKSORT(A, g + 1, right ) Sebastian Wild Quicksorts of the 21st century 2021-06-07 5 / 17
  62. YBB Partitioning Example < P1 ? swap < P2 ?

    skip swap g     > P2 ? < P1 ? skip swap swap k     P1 P2 3 1 5 7 4 2 8 6 g k < P1 P1≤◦≤P2 ≥ P2 P1 P2 Invariant: YBBQUICKSORT(A, left, right) 1 if right left then return 2 P1 := A[left]; P2 := A[right] 3 if P1 > P2 then Swap P1 and P2 end if 4 := left + 1; g := right − 1; k := 5 while k g 6 if A[k] < P1 7 Swap A[k] and A[ ] ; := + 1 8 else if A[k] P2 9 while A[g] > P2 and k < g 10 g := g − 1 11 end while 12 Swap A[k] and A[g] ; g := g − 1 13 if A[k] < P1 14 Swap A[k] and A[ ] ; := + 1 15 end if 16 end if 17 k := k + 1 18 end while 19 := − 1; g := g + 1 20 Swap A[left] and A[ ] ; Swap A[right] and A[g] 21 YBBQUICKSORT(A, left , − 1) 22 YBBQUICKSORT(A, + 1, g − 1) 23 YBBQUICKSORT(A, g + 1, right ) Sebastian Wild Quicksorts of the 21st century 2021-06-07 5 / 17
  63. YBB Partitioning Example < P1 ? swap < P2 ?

    skip swap g     > P2 ? < P1 ? skip swap swap k     P1 P2 3 1 5 7 4 2 8 6 g k < P1 P1≤◦≤P2 ≥ P2 P1 P2 Invariant: YBBQUICKSORT(A, left, right) 1 if right left then return 2 P1 := A[left]; P2 := A[right] 3 if P1 > P2 then Swap P1 and P2 end if 4 := left + 1; g := right − 1; k := 5 while k g 6 if A[k] < P1 7 Swap A[k] and A[ ] ; := + 1 8 else if A[k] P2 9 while A[g] > P2 and k < g 10 g := g − 1 11 end while 12 Swap A[k] and A[g] ; g := g − 1 13 if A[k] < P1 14 Swap A[k] and A[ ] ; := + 1 15 end if 16 end if 17 k := k + 1 18 end while 19 := − 1; g := g + 1 20 Swap A[left] and A[ ] ; Swap A[right] and A[g] 21 YBBQUICKSORT(A, left , − 1) 22 YBBQUICKSORT(A, + 1, g − 1) 23 YBBQUICKSORT(A, g + 1, right ) Sebastian Wild Quicksorts of the 21st century 2021-06-07 5 / 17
  64. YBB Partitioning Example < P1 ? swap < P2 ?

    skip swap g     > P2 ? < P1 ? skip swap swap k     P1 P2 3 1 2 5 4 7 8 6 g k < P1 P1≤◦≤P2 ≥ P2 P1 P2 Invariant: YBBQUICKSORT(A, left, right) 1 if right left then return 2 P1 := A[left]; P2 := A[right] 3 if P1 > P2 then Swap P1 and P2 end if 4 := left + 1; g := right − 1; k := 5 while k g 6 if A[k] < P1 7 Swap A[k] and A[ ] ; := + 1 8 else if A[k] P2 9 while A[g] > P2 and k < g 10 g := g − 1 11 end while 12 Swap A[k] and A[g] ; g := g − 1 13 if A[k] < P1 14 Swap A[k] and A[ ] ; := + 1 15 end if 16 end if 17 k := k + 1 18 end while 19 := − 1; g := g + 1 20 Swap A[left] and A[ ] ; Swap A[right] and A[g] 21 YBBQUICKSORT(A, left , − 1) 22 YBBQUICKSORT(A, + 1, g − 1) 23 YBBQUICKSORT(A, g + 1, right ) Sebastian Wild Quicksorts of the 21st century 2021-06-07 5 / 17
  65. YBB Partitioning Example < P1 ? swap < P2 ?

    skip swap g     > P2 ? < P1 ? skip swap swap k     P1 P2 3 1 2 5 4 7 8 6 g k < P1 P1≤◦≤P2 ≥ P2 P1 P2 Invariant: YBBQUICKSORT(A, left, right) 1 if right left then return 2 P1 := A[left]; P2 := A[right] 3 if P1 > P2 then Swap P1 and P2 end if 4 := left + 1; g := right − 1; k := 5 while k g 6 if A[k] < P1 7 Swap A[k] and A[ ] ; := + 1 8 else if A[k] P2 9 while A[g] > P2 and k < g 10 g := g − 1 11 end while 12 Swap A[k] and A[g] ; g := g − 1 13 if A[k] < P1 14 Swap A[k] and A[ ] ; := + 1 15 end if 16 end if 17 k := k + 1 18 end while 19 := − 1; g := g + 1 20 Swap A[left] and A[ ] ; Swap A[right] and A[g] 21 YBBQUICKSORT(A, left , − 1) 22 YBBQUICKSORT(A, + 1, g − 1) 23 YBBQUICKSORT(A, g + 1, right ) Sebastian Wild Quicksorts of the 21st century 2021-06-07 5 / 17
  66. YBB Partitioning Example < P1 ? swap < P2 ?

    skip swap g     > P2 ? < P1 ? skip swap swap k     P1 P2 3 1 2 5 4 7 8 6 g k < P1 P1≤◦≤P2 ≥ P2 P1 P2 Invariant: YBBQUICKSORT(A, left, right) 1 if right left then return 2 P1 := A[left]; P2 := A[right] 3 if P1 > P2 then Swap P1 and P2 end if 4 := left + 1; g := right − 1; k := 5 while k g 6 if A[k] < P1 7 Swap A[k] and A[ ] ; := + 1 8 else if A[k] P2 9 while A[g] > P2 and k < g 10 g := g − 1 11 end while 12 Swap A[k] and A[g] ; g := g − 1 13 if A[k] < P1 14 Swap A[k] and A[ ] ; := + 1 15 end if 16 end if 17 k := k + 1 18 end while 19 := − 1; g := g + 1 20 Swap A[left] and A[ ] ; Swap A[right] and A[g] 21 YBBQUICKSORT(A, left , − 1) 22 YBBQUICKSORT(A, + 1, g − 1) 23 YBBQUICKSORT(A, g + 1, right ) Sebastian Wild Quicksorts of the 21st century 2021-06-07 5 / 17
  67. YBB Partitioning Example < P1 ? swap < P2 ?

    skip swap g     > P2 ? < P1 ? skip swap swap k     P1 P2 3 1 2 5 4 7 8 6 g k < P1 P1≤◦≤P2 ≥ P2 P1 P2 Invariant: YBBQUICKSORT(A, left, right) 1 if right left then return 2 P1 := A[left]; P2 := A[right] 3 if P1 > P2 then Swap P1 and P2 end if 4 := left + 1; g := right − 1; k := 5 while k g 6 if A[k] < P1 7 Swap A[k] and A[ ] ; := + 1 8 else if A[k] P2 9 while A[g] > P2 and k < g 10 g := g − 1 11 end while 12 Swap A[k] and A[g] ; g := g − 1 13 if A[k] < P1 14 Swap A[k] and A[ ] ; := + 1 15 end if 16 end if 17 k := k + 1 18 end while 19 := − 1; g := g + 1 20 Swap A[left] and A[ ] ; Swap A[right] and A[g] 21 YBBQUICKSORT(A, left , − 1) 22 YBBQUICKSORT(A, + 1, g − 1) 23 YBBQUICKSORT(A, g + 1, right ) Sebastian Wild Quicksorts of the 21st century 2021-06-07 5 / 17
  68. YBB Partitioning Example < P1 ? swap < P2 ?

    skip swap g     > P2 ? < P1 ? skip swap swap k     P1 P2 3 1 2 5 4 7 8 6 YBBQUICKSORT(A, left, right) 1 if right left then return 2 P1 := A[left]; P2 := A[right] 3 if P1 > P2 then Swap P1 and P2 end if 4 := left + 1; g := right − 1; k := 5 while k g 6 if A[k] < P1 7 Swap A[k] and A[ ] ; := + 1 8 else if A[k] P2 9 while A[g] > P2 and k < g 10 g := g − 1 11 end while 12 Swap A[k] and A[g] ; g := g − 1 13 if A[k] < P1 14 Swap A[k] and A[ ] ; := + 1 15 end if 16 end if 17 k := k + 1 18 end while 19 := − 1; g := g + 1 20 Swap A[left] and A[ ] ; Swap A[right] and A[g] 21 YBBQUICKSORT(A, left , − 1) 22 YBBQUICKSORT(A, + 1, g − 1) 23 YBBQUICKSORT(A, g + 1, right ) Sebastian Wild Quicksorts of the 21st century 2021-06-07 5 / 17
  69. YBB Partitioning Example < P1 ? swap < P2 ?

    skip swap g     > P2 ? < P1 ? skip swap swap k     P1 P2 2 1 3 5 4 6 8 7 YBBQUICKSORT(A, left, right) 1 if right left then return 2 P1 := A[left]; P2 := A[right] 3 if P1 > P2 then Swap P1 and P2 end if 4 := left + 1; g := right − 1; k := 5 while k g 6 if A[k] < P1 7 Swap A[k] and A[ ] ; := + 1 8 else if A[k] P2 9 while A[g] > P2 and k < g 10 g := g − 1 11 end while 12 Swap A[k] and A[g] ; g := g − 1 13 if A[k] < P1 14 Swap A[k] and A[ ] ; := + 1 15 end if 16 end if 17 k := k + 1 18 end while 19 := − 1; g := g + 1 20 Swap A[left] and A[ ] ; Swap A[right] and A[g] 21 YBBQUICKSORT(A, left , − 1) 22 YBBQUICKSORT(A, + 1, g − 1) 23 YBBQUICKSORT(A, g + 1, right ) Sebastian Wild Quicksorts of the 21st century 2021-06-07 5 / 17
  70. YBB Partitioning Example < P1 ? swap < P2 ?

    skip swap g     > P2 ? < P1 ? skip swap swap k     P1 P2 1 2 3 4 5 6 7 8 YBBQUICKSORT(A, left, right) 1 if right left then return 2 P1 := A[left]; P2 := A[right] 3 if P1 > P2 then Swap P1 and P2 end if 4 := left + 1; g := right − 1; k := 5 while k g 6 if A[k] < P1 7 Swap A[k] and A[ ] ; := + 1 8 else if A[k] P2 9 while A[g] > P2 and k < g 10 g := g − 1 11 end while 12 Swap A[k] and A[g] ; g := g − 1 13 if A[k] < P1 14 Swap A[k] and A[ ] ; := + 1 15 end if 16 end if 17 k := k + 1 18 end while 19 := − 1; g := g + 1 20 Swap A[left] and A[ ] ; Swap A[right] and A[g] 21 YBBQUICKSORT(A, left , − 1) 22 YBBQUICKSORT(A, + 1, g − 1) 23 YBBQUICKSORT(A, g + 1, right ) Sebastian Wild Quicksorts of the 21st century 2021-06-07 5 / 17
  71. Outline 1 Context 1 Context 2 Dual-Pivot Quicksort 2 Dual-Pivot

    Quicksort 3 Average-Case Analysis 3 Average-Case Analysis 4 Quicksort + X 4 Quicksort + X 5 Conclusion 5 Conclusion Sebastian Wild Quicksorts of the 21st century 2021-06-07 5 / 17
  72. 3 Average-Case Analysis 3 Average-Case Analysis Sebastian Wild Quicksorts of

    the 21st century 2021-06-07 5 / 17
  73. Comparisons How many comparisons on average? < P1 ? swap

    < P2 ? skip swap g     > P2 ? < P1 ? skip swap swap k     Consider first partitioning step. How many #cmps per element? Well, either 2 or 1 . How many on average? Analysis Sketch: Have to compare with one pivot first, say P1 . (Similar if P2 used first.) 1 cmp for small elements, 2 cmps for medium and large . On average, ∼ 1 3 n elements each kind. on average 2 · 2 3 + 1 · 1 3 = 5 3 cmps per element Sebastian Wild Quicksorts of the 21st century 2021-06-07 6 / 17
  74. Comparisons How many comparisons on average? < P1 ? swap

    < P2 ? skip swap g     > P2 ? < P1 ? skip swap swap k     Consider first partitioning step. How many #cmps per element? Well, either 2 or 1 . How many on average? Analysis Sketch: Have to compare with one pivot first, say P1 . (Similar if P2 used first.) 1 cmp for small elements, 2 cmps for medium and large . On average, ∼ 1 3 n elements each kind. on average 2 · 2 3 + 1 · 1 3 = 5 3 cmps per element Sebastian Wild Quicksorts of the 21st century 2021-06-07 6 / 17
  75. Comparisons How many comparisons on average? < P1 ? swap

    < P2 ? skip swap g     > P2 ? < P1 ? skip swap swap k     Consider first partitioning step. How many #cmps per element? Well, either 2 or 1 . How many on average? Analysis Sketch: Have to compare with one pivot first, say P1 . (Similar if P2 used first.) 1 cmp for small elements, 2 cmps for medium and large . On average, ∼ 1 3 n elements each kind. on average 2 · 2 3 + 1 · 1 3 = 5 3 cmps per element Sebastian Wild Quicksorts of the 21st century 2021-06-07 6 / 17
  76. Comparisons How many comparisons on average? < P1 ? swap

    < P2 ? skip swap g     > P2 ? < P1 ? skip swap swap k     Consider first partitioning step. How many #cmps per element? Well, either 2 or 1. How many on average? Analysis Sketch: Have to compare with one pivot first, say P1 . (Similar if P2 used first.) 1 cmp for small elements, 2 cmps for medium and large . On average, ∼ 1 3 n elements each kind. on average 2 · 2 3 + 1 · 1 3 = 5 3 cmps per element Sebastian Wild Quicksorts of the 21st century 2021-06-07 6 / 17
  77. Comparisons How many comparisons on average? < P1 ? swap

    < P2 ? skip swap g     > P2 ? < P1 ? skip swap swap k     Consider first partitioning step. How many #cmps per element? Well, either 2 or 1 . How many on average? Analysis Sketch: Have to compare with one pivot first, say P1 . (Similar if P2 used first.) 1 cmp for small elements, 2 cmps for medium and large . On average, ∼ 1 3 n elements each kind. on average 2 · 2 3 + 1 · 1 3 = 5 3 cmps per element Sebastian Wild Quicksorts of the 21st century 2021-06-07 6 / 17
  78. Comparisons How many comparisons on average? < P1 ? swap

    < P2 ? skip swap g     > P2 ? < P1 ? skip swap swap k     Consider first partitioning step. How many #cmps per element? Well, either 2 or 1 . How many on average? Analysis Sketch: Have to compare with one pivot first, say P1 . (Similar if P2 used first.) 1 cmp for small elements, 2 cmps for medium and large . On average, ∼ 1 3 n elements each kind. on average 2 · 2 3 + 1 · 1 3 = 5 3 cmps per element Sebastian Wild Quicksorts of the 21st century 2021-06-07 6 / 17
  79. Comparisons How many comparisons on average? < P1 ? swap

    < P2 ? skip swap g     > P2 ? < P1 ? skip swap swap k     Consider first partitioning step. How many #cmps per element? Well, either 2 or 1 . How many on average? Analysis Sketch: Have to compare with one pivot first, say P1 . (Similar if P2 used first.) 1 cmp for small elements, 2 cmps for medium and large . On average, ∼ 1 3 n elements each kind. on average 2 · 2 3 + 1 · 1 3 = 5 3 cmps per element Sebastian Wild Quicksorts of the 21st century 2021-06-07 6 / 17
  80. Comparisons How many comparisons on average? < P1 ? swap

    < P2 ? skip swap g     > P2 ? < P1 ? skip swap swap k     Consider first partitioning step. How many #cmps per element? Well, either 2 or 1 . How many on average? Analysis Sketch: Have to compare with one pivot first, say P1 . (Similar if P2 used first.) 1 cmp for small elements, 2 cmps for medium and large . On average, ∼ 1 3 n elements each kind. on average 2 · 2 3 + 1 · 1 3 = 5 3 cmps per element Sebastian Wild Quicksorts of the 21st century 2021-06-07 6 / 17
  81. Comparisons How many comparisons on average? < P1 ? swap

    < P2 ? skip swap g     > P2 ? < P1 ? skip swap swap k     Consider first partitioning step. How many #cmps per element? Well, either 2 or 1 . How many on average? Analysis Sketch: Have to compare with one pivot first, say P1 . (Similar if P2 used first.) 1 cmp for small elements, 2 cmps for medium and large . On average, ∼ 1 3 n elements each kind. on average 2 · 2 3 + 1 · 1 3 = 5 3 cmps per element Sebastian Wild Quicksorts of the 21st century 2021-06-07 6 / 17
  82. Comparisons How many comparisons on average? < P1 ? swap

    < P2 ? skip swap g     > P2 ? < P1 ? skip swap swap k     Consider first partitioning step. How many #cmps per element? Well, either 2 or 1 . How many on average? Analysis Sketch: Have to compare with one pivot first, say P1 . (Similar if P2 used first.) 1 cmp for small elements, 2 cmps for medium and large . On average, ∼ 1 3 n elements each kind. on average 2 · 2 3 + 1 · 1 3 = 5 3 cmps per element Sebastian Wild Quicksorts of the 21st century 2021-06-07 6 / 17
  83. Comparisons How many comparisons on average? < P1 ? swap

    < P2 ? skip swap g     > P2 ? < P1 ? skip swap swap k     Consider first partitioning step. How many #cmps per element? Well, either 2 or 1 . How many on average? Analysis Sketch: Have to compare with one pivot first, say P1 . (Similar if P2 used first.) 1 cmp for small elements, 2 cmps for medium and large . On average, ∼ 1 3 n elements each kind. on average 2 · 2 3 + 1 · 1 3 = 5 3 cmps per element Sounds plausible? Sebastian Wild Quicksorts of the 21st century 2021-06-07 6 / 17
  84. Comparisons How many comparisons on average? < P1 ? swap

    < P2 ? skip swap g     > P2 ? < P1 ? skip swap swap k     Consider first partitioning step. How many #cmps per element? Well, either 2 or 1 . How many on average? Analysis Sketch: Have to compare with one pivot first, say P1 . (Similar if P2 used first.) 1 cmp for small elements, 2 cmps for medium and large . On average, ∼ 1 3 n elements each kind. on average 2 · 2 3 + 1 · 1 3 = 5 3 cmps per element Sounds plausible? ...but it’s wrong! Sebastian Wild Quicksorts of the 21st century 2021-06-07 6 / 17
  85. Comparisons How many comparisons on average? < P1 ? swap

    < P2 ? skip swap g     > P2 ? < P1 ? skip swap swap k     Consider first partitioning step. How many #cmps per element? Well, either 2 or 1 . How many on average? Analysis Sketch: Have to compare with one pivot first, say P1 . (Similar if P2 used first.) 1 cmp for small elements, 2 cmps for medium and large . On average, ∼ 1 3 n elements each kind. on average 2 · 2 3 + 1 · 1 3 = 5 3 cmps per element Sounds plausible? ...but it’s wrong! Problem: There are correlations. many small elements k’s range big many large elements g’s range big > 1 3 n cheap elements on average. How many? ... Analysis of Algorithms to the rescue! Sebastian Wild Quicksorts of the 21st century 2021-06-07 6 / 17
  86. Comparisons How many comparisons on average? < P1 ? swap

    < P2 ? skip swap g     > P2 ? < P1 ? skip swap swap k     Consider first partitioning step. How many #cmps per element? Well, either 2 or 1 . How many on average? Analysis Sketch: Have to compare with one pivot first, say P1 . (Similar if P2 used first.) 1 cmp for small elements, 2 cmps for medium and large . On average, ∼ 1 3 n elements each kind. on average 2 · 2 3 + 1 · 1 3 = 5 3 cmps per element Sounds plausible? ...but it’s wrong! Problem: There are correlations. many small elements k’s range big many large elements g’s range big > 1 3 n cheap elements on average. How many? ... Analysis of Algorithms to the rescue! Sebastian Wild Quicksorts of the 21st century 2021-06-07 6 / 17
  87. Comparisons How many comparisons on average? < P1 ? swap

    < P2 ? skip swap g     > P2 ? < P1 ? skip swap swap k     Consider first partitioning step. How many #cmps per element? Well, either 2 or 1 . How many on average? Analysis Sketch: Have to compare with one pivot first, say P1 . (Similar if P2 used first.) 1 cmp for small elements, 2 cmps for medium and large . On average, ∼ 1 3 n elements each kind. on average 2 · 2 3 + 1 · 1 3 = 5 3 cmps per element Sounds plausible? ...but it’s wrong! Problem: There are correlations. many small elements k’s range big many large elements g’s range big > 1 3 n cheap elements on average. How many? ... Analysis of Algorithms to the rescue! Sebastian Wild Quicksorts of the 21st century 2021-06-07 6 / 17
  88. Comparisons How many comparisons on average? < P1 ? swap

    < P2 ? skip swap g     > P2 ? < P1 ? skip swap swap k     Consider first partitioning step. How many #cmps per element? Well, either 2 or 1 . How many on average? Analysis Sketch: Have to compare with one pivot first, say P1 . (Similar if P2 used first.) 1 cmp for small elements, 2 cmps for medium and large . On average, ∼ 1 3 n elements each kind. on average 2 · 2 3 + 1 · 1 3 = 5 3 cmps per element Sounds plausible? ...but it’s wrong! Problem: There are correlations. many small elements k’s range big many large elements g’s range big > 1 3 n cheap elements on average. How many? ... Analysis of Algorithms to the rescue! Sebastian Wild Quicksorts of the 21st century 2021-06-07 6 / 17
  89. Comparisons How many comparisons on average? < P1 ? swap

    < P2 ? skip swap g     > P2 ? < P1 ? skip swap swap k     Consider first partitioning step. How many #cmps per element? Well, either 2 or 1 . How many on average? Analysis Sketch: Have to compare with one pivot first, say P1 . (Similar if P2 used first.) 1 cmp for small elements, 2 cmps for medium and large . On average, ∼ 1 3 n elements each kind. on average 2 · 2 3 + 1 · 1 3 = 5 3 cmps per element Sounds plausible? ...but it’s wrong! Problem: There are correlations. many small elements k’s range big many large elements g’s range big > 1 3 n cheap elements on average. How many? ... Analysis of Algorithms to the rescue! Sebastian Wild Quicksorts of the 21st century 2021-06-07 6 / 17
  90. Mathematical Analysis of Algorithms Sebastian Wild Quicksorts of the 21st

    century 2021-06-07 7 / 17
  91. Mathematical Analysis of Algorithms Random Model n i.i.d. elements chosen

    uniformly in (0, 1) 0 1 U1 U2 U3 U4 U5 U6 U7 U8 pairwise distinct almost surely relative ranking is a random permutation equivalent to classic model For dual-pivot Quicksort with fixed pivots P1 P2 Pr U < P1 = D1 Pr P1 < U < P2 = D2 Pr U > P2 = D3 0 1 P1 P2 D1 D2 D3 Known distribution: D D is a uniformly chosen probability distribution! = (D1, D2, D3 ) D = Dirichlet(1, 1, 1). (D1, D2 ) uniform in 2D simplex x, y ∈ (0, 1) : x + y < 1 0 0.5 1 0 0.5 1 0 0.5 1 Sebastian Wild Sorting Today 2020-10-06 16 / 27 Sebastian Wild Quicksorts of the 21st century 2021-06-07 7 / 17
  92. Mathematical Analysis of Algorithms Random Model n i.i.d. elements chosen

    uniformly in (0, 1) 0 1 U1 U2 U3 U4 U5 U6 U7 U8 pairwise distinct almost surely relative ranking is a random permutation equivalent to classic model For dual-pivot Quicksort with fixed pivots P1 P2 Pr U < P1 = D1 Pr P1 < U < P2 = D2 Pr U > P2 = D3 0 1 P1 P2 D1 D2 D3 Known distribution: D D is a uniformly chosen probability distribution! = (D1, D2, D3 ) D = Dirichlet(1, 1, 1). (D1, D2 ) uniform in 2D simplex x, y ∈ (0, 1) : x + y < 1 0 0.5 1 0 0.5 1 0 0.5 1 Sebastian Wild Sorting Today 2020-10-06 16 / 27 Comparisons in YBB Partitioning t(n): E[#cmps] in first partitioning step I1 : number of small elements; I2 , I3 similar K : #elements scanned by k; K ∼ I1 + I2 s@ K : # small elements in k ’s range; l @ G similar < P1 P2 P1 ◦ P2 k’s range K G I1 I2 I3 t(n) ∼ 2n − E[s@ K ] − E[ l @ G ] ...what is E[s@ K ]? Consider fixed D, I = (I1, I2, I3 ) D = Mult(n − 2, D) I = Dn ± O(n1/2+ε) whp 0 1 P1 P2 D1 D2 D3 I1 I2 I3 E[ l @ G To be precise, we have conditional on I: l @ G D = HypG(n − 2; I3 , I3 ) E[ l @ G | I] = I3 · I3 n − 2 1 3 4 6 7 2 8 5 Draw positions of large elements 1 2 3 4 5 6 7 8 l @ G = 1 ] ∼ E D3 fraction of large elements · D3 fraction of elements in K · n, E[s@ K ] ∼ E D1 · (D1 + D2 ) · n Sebastian Wild Sorting Today 2020-10-06 17 / 27 Sebastian Wild Quicksorts of the 21st century 2021-06-07 7 / 17
  93. Mathematical Analysis of Algorithms Random Model n i.i.d. elements chosen

    uniformly in (0, 1) 0 1 U1 U2 U3 U4 U5 U6 U7 U8 pairwise distinct almost surely relative ranking is a random permutation equivalent to classic model For dual-pivot Quicksort with fixed pivots P1 P2 Pr U < P1 = D1 Pr P1 < U < P2 = D2 Pr U > P2 = D3 0 1 P1 P2 D1 D2 D3 Known distribution: D D is a uniformly chosen probability distribution! = (D1, D2, D3 ) D = Dirichlet(1, 1, 1). (D1, D2 ) uniform in 2D simplex x, y ∈ (0, 1) : x + y < 1 0 0.5 1 0 0.5 1 0 0.5 1 Sebastian Wild Sorting Today 2020-10-06 16 / 27 Comparisons in YBB Partitioning t(n): E[#cmps] in first partitioning step I1 : number of small elements; I2 , I3 similar K : #elements scanned by k; K ∼ I1 + I2 s@ K : # small elements in k ’s range; l @ G similar < P1 P2 P1 ◦ P2 k’s range K G I1 I2 I3 t(n) ∼ 2n − E[s@ K ] − E[ l @ G ] ...what is E[s@ K ]? Consider fixed D, I = (I1, I2, I3 ) D = Mult(n − 2, D) I = Dn ± O(n1/2+ε) whp 0 1 P1 P2 D1 D2 D3 I1 I2 I3 E[ l @ G To be precise, we have conditional on I: l @ G D = HypG(n − 2; I3 , I3 ) E[ l @ G | I] = I3 · I3 n − 2 1 3 4 6 7 2 8 5 Draw positions of large elements 1 2 3 4 5 6 7 8 l @ G = 1 ] ∼ E D3 fraction of large elements · D3 fraction of elements in K · n, E[s@ K ] ∼ E D1 · (D1 + D2 ) · n Sebastian Wild Sorting Today 2020-10-06 17 / 27 Comparisons in YBB Partitioning t(n): E[#cmps] in first partitioning step I1 : number of small elements; I2 , I3 similar K : #elements scanned by k; K ∼ I1 + I2 s@ K : # small elements in k ’s range; l @ G similar < P1 P2 P1 ◦ P2 k’s range K G I1 I2 I3 t(n) ∼ 2n − E[s@ K ] − E[ l @ G ] ...what is E[s@ K ]? Consider fixed D, I = (I1, I2, I3 ) D = Mult(n − 2, D) I = Dn ± O(n1/2+ε) whp 0 1 P1 P2 D1 D2 D3 I1 I2 I3 E[ l @ G ] ∼ E D3 fraction of large elements · D3 fraction of elements in K = 1 6 > 1 3 · 1 3 · n, E[s@ K ] ∼ E D1 · (D1 + D2 ) · n Sebastian Wild Sorting Today 2020-10-06 17 / 27 Sebastian Wild Quicksorts of the 21st century 2021-06-07 7 / 17
  94. Mathematical Analysis of Algorithms Random Model n i.i.d. elements chosen

    uniformly in (0, 1) 0 1 U1 U2 U3 U4 U5 U6 U7 U8 pairwise distinct almost surely relative ranking is a random permutation equivalent to classic model For dual-pivot Quicksort with fixed pivots P1 P2 Pr U < P1 = D1 Pr P1 < U < P2 = D2 Pr U > P2 = D3 0 1 P1 P2 D1 D2 D3 Known distribution: D D is a uniformly chosen probability distribution! = (D1, D2, D3 ) D = Dirichlet(1, 1, 1). (D1, D2 ) uniform in 2D simplex x, y ∈ (0, 1) : x + y < 1 0 0.5 1 0 0.5 1 0 0.5 1 Sebastian Wild Sorting Today 2020-10-06 16 / 27 Comparisons in YBB Partitioning t(n): E[#cmps] in first partitioning step I1 : number of small elements; I2 , I3 similar K : #elements scanned by k; K ∼ I1 + I2 s@ K : # small elements in k ’s range; l @ G similar < P1 P2 P1 ◦ P2 k’s range K G I1 I2 I3 t(n) ∼ 2n − E[s@ K ] − E[ l @ G ] ...what is E[s@ K ]? Consider fixed D, I = (I1, I2, I3 ) D = Mult(n − 2, D) I = Dn ± O(n1/2+ε) whp 0 1 P1 P2 D1 D2 D3 I1 I2 I3 E[ l @ G To be precise, we have conditional on I: l @ G D = HypG(n − 2; I3 , I3 ) E[ l @ G | I] = I3 · I3 n − 2 1 3 4 6 7 2 8 5 Draw positions of large elements 1 2 3 4 5 6 7 8 l @ G = 1 ] ∼ E D3 fraction of large elements · D3 fraction of elements in K · n, E[s@ K ] ∼ E D1 · (D1 + D2 ) · n Sebastian Wild Sorting Today 2020-10-06 17 / 27 Comparisons in YBB Partitioning t(n): E[#cmps] in first partitioning step I1 : number of small elements; I2 , I3 similar K : #elements scanned by k; K ∼ I1 + I2 s@ K : # small elements in k ’s range; l @ G similar < P1 P2 P1 ◦ P2 k’s range K G I1 I2 I3 t(n) ∼ 2n − E[s@ K ] − E[ l @ G ] ...what is E[s@ K ]? Consider fixed D, I = (I1, I2, I3 ) D = Mult(n − 2, D) I = Dn ± O(n1/2+ε) whp 0 1 P1 P2 D1 D2 D3 I1 I2 I3 E[ l @ G ] ∼ E D3 fraction of large elements · D3 fraction of elements in K = 1 6 > 1 3 · 1 3 · n, E[s@ K ] ∼ E D1 · (D1 + D2 ) · n Sebastian Wild Sorting Today 2020-10-06 17 / 27 Recurrence c(n): E[#cmps] for sorting c(n) = t(n) + E c(I1 ) + E c(I2 ) + E c(I3 ) = 19 12 n + E c( D1 · n) + E c( D2 · n) + E c( D3 · n) ± O(n1/2+ε) Solve recurrence without error term by ansatz: c(n) = αnln(n) αn ln(n) = 19 12 n + αn ln(n) =1 3 r=1 E[Dr ] + αn =−E[H base e Shannon entropy of D (D)] 3 r=1 E[Dr ln(Dr)] ⇐⇒ α = 19 12 E[H(D)] 5 6 = 19 10 indeed (by induction): c(n) = 19 10 n ln n ± O(n). Sebastian Wild Sorting Today 2020-10-06 18 / 27 Sebastian Wild Quicksorts of the 21st century 2021-06-07 7 / 17
  95. Mathematical Analysis of Algorithms Random Model n i.i.d. elements chosen

    uniformly in (0, 1) 0 1 U1 U2 U3 U4 U5 U6 U7 U8 pairwise distinct almost surely relative ranking is a random permutation equivalent to classic model For dual-pivot Quicksort with fixed pivots P1 P2 Pr U < P1 = D1 Pr P1 < U < P2 = D2 Pr U > P2 = D3 0 1 P1 P2 D1 D2 D3 Known distribution: D D is a uniformly chosen probability distribution! = (D1, D2, D3 ) D = Dirichlet(1, 1, 1). (D1, D2 ) uniform in 2D simplex x, y ∈ (0, 1) : x + y < 1 0 0.5 1 0 0.5 1 0 0.5 1 Sebastian Wild Sorting Today 2020-10-06 16 / 27 Comparisons in YBB Partitioning t(n): E[#cmps] in first partitioning step I1 : number of small elements; I2 , I3 similar K : #elements scanned by k; K ∼ I1 + I2 s@ K : # small elements in k ’s range; l @ G similar < P1 P2 P1 ◦ P2 k’s range K G I1 I2 I3 t(n) ∼ 2n − E[s@ K ] − E[ l @ G ] ...what is E[s@ K ]? Consider fixed D, I = (I1, I2, I3 ) D = Mult(n − 2, D) I = Dn ± O(n1/2+ε) whp 0 1 P1 P2 D1 D2 D3 I1 I2 I3 E[ l @ G To be precise, we have conditional on I: l @ G D = HypG(n − 2; I3 , I3 ) E[ l @ G | I] = I3 · I3 n − 2 1 3 4 6 7 2 8 5 Draw positions of large elements 1 2 3 4 5 6 7 8 l @ G = 1 ] ∼ E D3 fraction of large elements · D3 fraction of elements in K · n, E[s@ K ] ∼ E D1 · (D1 + D2 ) · n Sebastian Wild Sorting Today 2020-10-06 17 / 27 Comparisons in YBB Partitioning t(n): E[#cmps] in first partitioning step I1 : number of small elements; I2 , I3 similar K : #elements scanned by k; K ∼ I1 + I2 s@ K : # small elements in k ’s range; l @ G similar < P1 P2 P1 ◦ P2 k’s range K G I1 I2 I3 t(n) ∼ 2n − E[s@ K ] − E[ l @ G ] ...what is E[s@ K ]? Consider fixed D, I = (I1, I2, I3 ) D = Mult(n − 2, D) I = Dn ± O(n1/2+ε) whp 0 1 P1 P2 D1 D2 D3 I1 I2 I3 E[ l @ G ] ∼ E D3 fraction of large elements · D3 fraction of elements in K = 1 6 > 1 3 · 1 3 · n, E[s@ K ] ∼ E D1 · (D1 + D2 ) · n Sebastian Wild Sorting Today 2020-10-06 17 / 27 Recurrence c(n): E[#cmps] for sorting c(n) = t(n) + E c(I1 ) + E c(I2 ) + E c(I3 ) = 19 12 n + E c( D1 · n) + E c( D2 · n) + E c( D3 · n) ± O(n1/2+ε) Solve recurrence without error term by ansatz: c(n) = αnln(n) αn ln(n) = 19 12 n + αn ln(n) =1 3 r=1 E[Dr ] + αn =−E[H base e Shannon entropy of D (D)] 3 r=1 E[Dr ln(Dr)] ⇐⇒ α = 19 12 E[H(D)] 5 6 = 19 10 indeed (by induction): c(n) = 19 10 n ln n ± O(n). Sebastian Wild Sorting Today 2020-10-06 18 / 27 Recurrence c(n): E[#cmps] for sorting c(n) = t(n) + E c(I1 ) + E c(I2 ) + E c(I3 ) = 19 12 n + E c( D1 · n) + E c( D2 · n) + E c( D3 · n) ± O(n1/2+ε) Solve recurrence without error term by ansatz: c(n) = αnln(n) αn ln(n) = 19 12 n + αn ln(n) =1 3 r=1 E[Dr ] + αn =−E[H base e Shannon entropy of D (D)] 3 r=1 E[Dr ln(Dr)] ⇐⇒ α = 19 12 E[H(D)] 5 6 = 19 10 indeed (by i Salvador Roura’s Continuous Master Theorem nduction): c(n) = 19 10 n ln n ± O(n). Why does Shannon entropy show up here? Must learn lg(n!) ∼ n lg n bits of information to sort. For fixed D, one classification ( small / medium / large ) yields Hlg binary entropy (D) = − 3 r=1 Dr lg(Dr ) bits of information definition of entropy! costs a = 2 − E[ D3 D3 ] − E[ D1 (D1 + D2 ) ] = 19 12 cmps Average information yield: I = E[Hlg(D)] a bits per comparison Need lg(n!) I ∼ a E[Hlg(D)] n lg n = a E[H(D)] n ln n cmps! Sebastian Wild Sorting Today 2020-10-06 18 / 27 Sebastian Wild Quicksorts of the 21st century 2021-06-07 7 / 17
  96. Mathematical Analysis of Algorithms Random Model n i.i.d. elements chosen

    uniformly in (0, 1) 0 1 U1 U2 U3 U4 U5 U6 U7 U8 pairwise distinct almost surely relative ranking is a random permutation equivalent to classic model For dual-pivot Quicksort with fixed pivots P1 P2 Pr U < P1 = D1 Pr P1 < U < P2 = D2 Pr U > P2 = D3 0 1 P1 P2 D1 D2 D3 Known distribution: D D is a uniformly chosen probability distribution! = (D1, D2, D3 ) D = Dirichlet(1, 1, 1). (D1, D2 ) uniform in 2D simplex x, y ∈ (0, 1) : x + y < 1 0 0.5 1 0 0.5 1 0 0.5 1 Sebastian Wild Sorting Today 2020-10-06 16 / 27 Comparisons in YBB Partitioning t(n): E[#cmps] in first partitioning step I1 : number of small elements; I2 , I3 similar K : #elements scanned by k; K ∼ I1 + I2 s@ K : # small elements in k ’s range; l @ G similar < P1 P2 P1 ◦ P2 k’s range K G I1 I2 I3 t(n) ∼ 2n − E[s@ K ] − E[ l @ G ] ...what is E[s@ K ]? Consider fixed D, I = (I1, I2, I3 ) D = Mult(n − 2, D) I = Dn ± O(n1/2+ε) whp 0 1 P1 P2 D1 D2 D3 I1 I2 I3 E[ l @ G To be precise, we have conditional on I: l @ G D = HypG(n − 2; I3 , I3 ) E[ l @ G | I] = I3 · I3 n − 2 1 3 4 6 7 2 8 5 Draw positions of large elements 1 2 3 4 5 6 7 8 l @ G = 1 ] ∼ E D3 fraction of large elements · D3 fraction of elements in K · n, E[s@ K ] ∼ E D1 · (D1 + D2 ) · n Sebastian Wild Sorting Today 2020-10-06 17 / 27 Comparisons in YBB Partitioning t(n): E[#cmps] in first partitioning step I1 : number of small elements; I2 , I3 similar K : #elements scanned by k; K ∼ I1 + I2 s@ K : # small elements in k ’s range; l @ G similar < P1 P2 P1 ◦ P2 k’s range K G I1 I2 I3 t(n) ∼ 2n − E[s@ K ] − E[ l @ G ] ...what is E[s@ K ]? Consider fixed D, I = (I1, I2, I3 ) D = Mult(n − 2, D) I = Dn ± O(n1/2+ε) whp 0 1 P1 P2 D1 D2 D3 I1 I2 I3 E[ l @ G ] ∼ E D3 fraction of large elements · D3 fraction of elements in K = 1 6 > 1 3 · 1 3 · n, E[s@ K ] ∼ E D1 · (D1 + D2 ) · n Sebastian Wild Sorting Today 2020-10-06 17 / 27 Recurrence c(n): E[#cmps] for sorting c(n) = t(n) + E c(I1 ) + E c(I2 ) + E c(I3 ) = 19 12 n + E c( D1 · n) + E c( D2 · n) + E c( D3 · n) ± O(n1/2+ε) Solve recurrence without error term by ansatz: c(n) = αnln(n) αn ln(n) = 19 12 n + αn ln(n) =1 3 r=1 E[Dr ] + αn =−E[H base e Shannon entropy of D (D)] 3 r=1 E[Dr ln(Dr)] ⇐⇒ α = 19 12 E[H(D)] 5 6 = 19 10 indeed (by induction): c(n) = 19 10 n ln n ± O(n). Sebastian Wild Sorting Today 2020-10-06 18 / 27 Recurrence c(n): E[#cmps] for sorting c(n) = t(n) + E c(I1 ) + E c(I2 ) + E c(I3 ) = 19 12 n + E c( D1 · n) + E c( D2 · n) + E c( D3 · n) ± O(n1/2+ε) Solve recurrence without error term by ansatz: c(n) = αnln(n) αn ln(n) = 19 12 n + αn ln(n) =1 3 r=1 E[Dr ] + αn =−E[H base e Shannon entropy of D (D)] 3 r=1 E[Dr ln(Dr)] ⇐⇒ α = 19 12 E[H(D)] 5 6 = 19 10 indeed (by i Salvador Roura’s Continuous Master Theorem nduction): c(n) = 19 10 n ln n ± O(n). Why does Shannon entropy show up here? Must learn lg(n!) ∼ n lg n bits of information to sort. For fixed D, one classification ( small / medium / large ) yields Hlg binary entropy (D) = − 3 r=1 Dr lg(Dr ) bits of information definition of entropy! costs a = 2 − E[ D3 D3 ] − E[ D1 (D1 + D2 ) ] = 19 12 cmps Average information yield: I = E[Hlg(D)] a bits per comparison Need lg(n!) I ∼ a E[Hlg(D)] n lg n = a E[H(D)] n ln n cmps! Sebastian Wild Sorting Today 2020-10-06 18 / 27 Sebastian Wild Quicksorts of the 21st century 2021-06-07 7 / 17
  97. Time for a Competition Sebastian Wild Quicksorts of the 21st

    century 2021-06-07 8 / 17
  98. Time for a Competition Dual-Pivot Quicksort k g Standard Quicksort

    k g Relative Difference Reference Sebastian Wild Quicksorts of the 21st century 2021-06-07 8 / 17
  99. Time for a Competition Dual-Pivot Quicksort k g Standard Quicksort

    k g Relative Difference Reference comparisons 1.9 2 −5% ESA 2012 ·n ln n + O(n) average case results Sebastian Wild Quicksorts of the 21st century 2021-06-07 8 / 17
  100. Time for a Competition Dual-Pivot Quicksort k g Standard Quicksort

    k g Relative Difference Reference comparisons 1.9 2 −5% ESA 2012 write accesses 11 10 ≈ 1.1 2 3 ≈ 0.6 +65% ESA 2012 ·n ln n + O(n) average case results Sebastian Wild Quicksorts of the 21st century 2021-06-07 8 / 17
  101. Time for a Competition Dual-Pivot Quicksort k g Standard Quicksort

    k g Relative Difference Reference comparisons 1.9 2 −5% ESA 2012 write accesses 11 10 ≈ 1.1 2 3 ≈ 0.6 +65% ESA 2012 Java Bytecode instr. 23.8 18 +32.2% TALG 2015 ·n ln n + O(n) average case results Sebastian Wild Quicksorts of the 21st century 2021-06-07 8 / 17
  102. Time for a Competition Dual-Pivot Quicksort k g Standard Quicksort

    k g Relative Difference Reference comparisons 1.9 2 −5% ESA 2012 write accesses 11 10 ≈ 1.1 2 3 ≈ 0.6 +65% ESA 2012 Java Bytecode instr. 23.8 18 +32.2% TALG 2015 branch mispredictions (2-bit saturating counter) ·n ln n + O(n) average case results Sebastian Wild Quicksorts of the 21st century 2021-06-07 8 / 17
  103. Time for a Competition Dual-Pivot Quicksort k g Standard Quicksort

    k g Relative Difference Reference comparisons 1.9 2 −5% ESA 2012 write accesses 11 10 ≈ 1.1 2 3 ≈ 0.6 +65% ESA 2012 Java Bytecode instr. 23.8 18 +32.2% TALG 2015 branch mispredictions CPU tries to predict whether a conditional branch will be taken 1 taken 2 taken 3 not t. 4 not t. taken not t. not t. not t. not t. taken taken taken Prediction wrong Pipeline Stall (2-bit saturating counter) ·n ln n + O(n) average case results Sebastian Wild Quicksorts of the 21st century 2021-06-07 8 / 17
  104. Time for a Competition Dual-Pivot Quicksort k g Standard Quicksort

    k g Relative Difference Reference comparisons 1.9 2 −5% ESA 2012 write accesses 11 10 ≈ 1.1 2 3 ≈ 0.6 +65% ESA 2012 Java Bytecode instr. 23.8 18 +32.2% TALG 2015 branch mispredictions CPU tries to predict whether a conditional branch will be taken 1 taken 2 taken 3 not t. 4 not t. taken not t. not t. not t. not t. taken taken taken Prediction wrong Pipeline Stall (2-bit saturating counter) 31π 40 − 37 20 ≈ 0.5847 π 2 − 1 ≈ 0.5708 +2.4% ANALCO 2015 ·n ln n + O(n) average case results Sebastian Wild Quicksorts of the 21st century 2021-06-07 8 / 17
  105. Time for a Competition Dual-Pivot Quicksort k g Standard Quicksort

    k g Relative Difference Reference comparisons 1.9 2 −5% ESA 2012 write accesses 11 10 ≈ 1.1 2 3 ≈ 0.6 +65% ESA 2012 Java Bytecode instr. 23.8 18 +32.2% TALG 2015 branch mispredictions (2-bit saturating counter) 31π 40 − 37 20 ≈ 0.5847 π 2 − 1 ≈ 0.5708 +2.4% ANALCO 2015 ·n ln n + O(n) average case results ??? ??? Sebastian Wild Quicksorts of the 21st century 2021-06-07 8 / 17
  106. What happened? What do comparisons, write accesses, Bytecodes and branch

    misses have in common? they happen inside the CPU Sebastian Wild Quicksorts of the 21st century 2021-06-07 9 / 17
  107. What happened? What do comparisons, write accesses, Bytecodes and branch

    misses have in common? they happen inside the CPU Sebastian Wild Quicksorts of the 21st century 2021-06-07 9 / 17
  108. What happened? What do comparisons, write accesses, Bytecodes and branch

    misses have in common? they happen inside the CPU ...but we need to get the data there, as well! Sebastian Wild Quicksorts of the 21st century 2021-06-07 9 / 17
  109. What happened? What do comparisons, write accesses, Bytecodes and branch

    misses have in common? they happen inside the CPU ...but we need to get the data there, as well! Average annual growth rates: CPU speed: 46% Memory Bandwidth: 37% Sebastian Wild Quicksorts of the 21st century 2021-06-07 9 / 17
  110. What happened? What do comparisons, write accesses, Bytecodes and branch

    misses have in common? they happen inside the CPU ...but we need to get the data there, as well! 1991 1995 2000 2005 2010 2015 100 101 102 “Imbalance”: CPU speed (MFLOPS) bandwidth (MW/s) STREAM Benchmark data with linear regression www.cs.virginia.edu/stream/by_date/Balance.html Average annual growth rates: CPU speed: 46% Memory Bandwidth: 37% Sebastian Wild Quicksorts of the 21st century 2021-06-07 9 / 17
  111. What happened? What do comparisons, write accesses, Bytecodes and branch

    misses have in common? they happen inside the CPU ...but we need to get the data there, as well! 1991 1995 2000 2005 2010 2015 100 101 102 “Imbalance”: CPU speed (MFLOPS) bandwidth (MW/s) STREAM Benchmark data with linear regression www.cs.virginia.edu/stream/by_date/Balance.html Average annual growth rates: CPU speed: 46% Memory Bandwidth: 37% Imbalance: 5% Sebastian Wild Quicksorts of the 21st century 2021-06-07 9 / 17
  112. What happened? What do comparisons, write accesses, Bytecodes and branch

    misses have in common? they happen inside the CPU ...but we need to get the data there, as well! 1991 1995 2000 2005 2010 2015 100 101 102 “Imbalance”: CPU speed (MFLOPS) bandwidth (MW/s) STREAM Benchmark data with linear regression www.cs.virginia.edu/stream/by_date/Balance.html Average annual growth rates: CPU speed: 46% Memory Bandwidth: 37% Imbalance: 5% Sebastian Wild Quicksorts of the 21st century 2021-06-07 9 / 17
  113. What happened? What do comparisons, write accesses, Bytecodes and branch

    misses have in common? they happen inside the CPU ...but we need to get the data there, as well! 1991 1995 2000 2005 2010 2015 100 101 102 “Imbalance”: CPU speed (MFLOPS) bandwidth (MW/s) STREAM Benchmark data with linear regression www.cs.virginia.edu/stream/by_date/Balance.html Average annual growth rates: CPU speed: 46% Memory Bandwidth: 37% Imbalance: 5% Classic Quicksort (Bentley & McIlroy) 1993 1 mem transfer ≈ 7 instructions focus on CPU costs Sebastian Wild Quicksorts of the 21st century 2021-06-07 9 / 17
  114. What happened? What do comparisons, write accesses, Bytecodes and branch

    misses have in common? they happen inside the CPU ...but we need to get the data there, as well! 1991 1995 2000 2005 2010 2015 100 101 102 “Imbalance”: CPU speed (MFLOPS) bandwidth (MW/s) STREAM Benchmark data with linear regression www.cs.virginia.edu/stream/by_date/Balance.html Average annual growth rates: CPU speed: 46% Memory Bandwidth: 37% Imbalance: 5% Classic Quicksort (Bentley & McIlroy) 1993 1 mem transfer ≈ 7 instructions focus on CPU costs Sebastian Wild Quicksorts of the 21st century 2021-06-07 9 / 17
  115. What happened? What do comparisons, write accesses, Bytecodes and branch

    misses have in common? they happen inside the CPU ...but we need to get the data there, as well! 1991 1995 2000 2005 2010 2015 100 101 102 “Imbalance”: CPU speed (MFLOPS) bandwidth (MW/s) STREAM Benchmark data with linear regression www.cs.virginia.edu/stream/by_date/Balance.html Average annual growth rates: CPU speed: 46% Memory Bandwidth: 37% Imbalance: 5% Classic Quicksort (Bentley & McIlroy) 1993 1 mem transfer ≈ 7 instructions focus on CPU costs Sebastian Wild Quicksorts of the 21st century 2021-06-07 9 / 17
  116. What happened? What do comparisons, write accesses, Bytecodes and branch

    misses have in common? they happen inside the CPU ...but we need to get the data there, as well! 1991 1995 2000 2005 2010 2015 100 101 102 “Imbalance”: CPU speed (MFLOPS) bandwidth (MW/s) STREAM Benchmark data with linear regression www.cs.virginia.edu/stream/by_date/Balance.html Average annual growth rates: CPU speed: 46% Memory Bandwidth: 37% Imbalance: 5% Classic Quicksort (Bentley & McIlroy) 1993 1 mem transfer ≈ 7 instructions focus on CPU costs YBB Quicksort, 2009 – today 1 transfer ≈ 30 instructions focus on CPU and bandwidth Sebastian Wild Quicksorts of the 21st century 2021-06-07 9 / 17
  117. What happened? What do comparisons, write accesses, Bytecodes and branch

    misses have in common? they happen inside the CPU ...but we need to get the data there, as well! 1991 1995 2000 2005 2010 2015 100 101 102 “Imbalance”: CPU speed (MFLOPS) bandwidth (MW/s) STREAM Benchmark data with linear regression www.cs.virginia.edu/stream/by_date/Balance.html Average annual growth rates: CPU speed: 46% Memory Bandwidth: 37% Imbalance: 5% Classic Quicksort (Bentley & McIlroy) 1993 1 mem transfer ≈ 7 instructions focus on CPU costs YBB Quicksort, 2009 – today 1 transfer ≈ 30 instructions focus on CPU and bandwidth Sebastian Wild Quicksorts of the 21st century 2021-06-07 9 / 17
  118. What happened? What do comparisons, write accesses, Bytecodes and branch

    misses have in common? they happen inside the CPU ...but we need to get the data there, as well! 1991 1995 2000 2005 2010 2015 100 101 102 “Imbalance”: CPU speed (MFLOPS) bandwidth (MW/s) STREAM Benchmark data with linear regression www.cs.virginia.edu/stream/by_date/Balance.html Average annual growth rates: CPU speed: 46% Memory Bandwidth: 37% Imbalance: 5% Classic Quicksort (Bentley & McIlroy) 1993 1 mem transfer ≈ 7 instructions focus on CPU costs YBB Quicksort, 2009 – today 1 transfer ≈ 30 instructions focus on CPU and bandwidth Sebastian Wild Quicksorts of the 21st century 2021-06-07 9 / 17
  119. Time for a Competition Dual-Pivot Quicksort k g Standard Quicksort

    k g Relative Difference Reference comparisons 1.9 2 −5% ESA 2012 write accesses 11 10 ≈ 1.1 2 3 ≈ 0.6 +65% ESA 2012 Java Bytecode instr. 23.8 18 +32.2% TALG 2015 branch mispredictions (2-bit saturating counter) 31π 40 − 37 20 ≈ 0.5847 π 2 − 1 ≈ 0.5708 +2.4% ANALCO 2015 ·n ln n + O(n) average case results Sebastian Wild Quicksorts of the 21st century 2021-06-07 10 / 17
  120. Time for a Competition Dual-Pivot Quicksort k g Standard Quicksort

    k g Relative Difference Reference comparisons 1.9 2 −5% ESA 2012 write accesses 11 10 ≈ 1.1 2 3 ≈ 0.6 +65% ESA 2012 Java Bytecode instr. 23.8 18 +32.2% TALG 2015 branch mispredictions (2-bit saturating counter) 31π 40 − 37 20 ≈ 0.5847 π 2 − 1 ≈ 0.5708 +2.4% ANALCO 2015 scanned elements (∝ memory transfers / IOs) ·n ln n + O(n) average case results Sebastian Wild Quicksorts of the 21st century 2021-06-07 10 / 17
  121. Time for a Competition Dual-Pivot Quicksort k g Standard Quicksort

    k g Relative Difference Reference comparisons 1.9 2 −5% ESA 2012 write accesses 11 10 ≈ 1.1 2 3 ≈ 0.6 +65% ESA 2012 Java Bytecode instr. 23.8 18 +32.2% TALG 2015 branch mispredictions (2-bit saturating counter) 31π 40 − 37 20 ≈ 0.5847 π 2 − 1 ≈ 0.5708 +2.4% ANALCO 2015 scanned elements (∝ memory transfers / IOs) 1.6 2 −20% Algorithmica 2016 ·n ln n + O(n) average case results Sebastian Wild Quicksorts of the 21st century 2021-06-07 10 / 17
  122. Time for a Competition Dual-Pivot Quicksort k g Standard Quicksort

    k g Relative Difference Reference comparisons 1.9 2 −5% ESA 2012 write accesses 11 10 ≈ 1.1 2 3 ≈ 0.6 +65% ESA 2012 Java Bytecode instr. 23.8 18 +32.2% TALG 2015 branch mispredictions (2-bit saturating counter) 31π 40 − 37 20 ≈ 0.5847 π 2 − 1 ≈ 0.5708 +2.4% ANALCO 2015 scanned elements (∝ memory transfers / IOs) 1.6 2 −20% Algorithmica 2016 ·n ln n + O(n) average case results Summary: Sebastian Wild Quicksorts of the 21st century 2021-06-07 10 / 17
  123. Time for a Competition Dual-Pivot Quicksort k g Standard Quicksort

    k g Relative Difference Reference comparisons 1.9 2 −5% ESA 2012 write accesses 11 10 ≈ 1.1 2 3 ≈ 0.6 +65% ESA 2012 Java Bytecode instr. 23.8 18 +32.2% TALG 2015 branch mispredictions (2-bit saturating counter) 31π 40 − 37 20 ≈ 0.5847 π 2 − 1 ≈ 0.5708 +2.4% ANALCO 2015 scanned elements (∝ memory transfers / IOs) 1.6 2 −20% Algorithmica 2016 ·n ln n + O(n) average case results Summary: 1 Only convincing explanation for running times: memory transfers Sebastian Wild Quicksorts of the 21st century 2021-06-07 10 / 17
  124. Time for a Competition Dual-Pivot Quicksort k g Standard Quicksort

    k g Relative Difference Reference comparisons 1.9 2 −5% ESA 2012 write accesses 11 10 ≈ 1.1 2 3 ≈ 0.6 +65% ESA 2012 Java Bytecode instr. 23.8 18 +32.2% TALG 2015 branch mispredictions (2-bit saturating counter) 31π 40 − 37 20 ≈ 0.5847 π 2 − 1 ≈ 0.5708 +2.4% ANALCO 2015 scanned elements (∝ memory transfers / IOs) 1.6 2 −20% Algorithmica 2016 ·n ln n + O(n) average case results Summary: 1 Only convincing explanation for running times: memory transfers 2 Why was dual-pivot Quicksort not used earlier? Sebastian Wild Quicksorts of the 21st century 2021-06-07 10 / 17
  125. Time for a Competition Dual-Pivot Quicksort k g Standard Quicksort

    k g Relative Difference Reference comparisons 1.9 2 −5% ESA 2012 write accesses 11 10 ≈ 1.1 2 3 ≈ 0.6 +65% ESA 2012 Java Bytecode instr. 23.8 18 +32.2% TALG 2015 branch mispredictions (2-bit saturating counter) 31π 40 − 37 20 ≈ 0.5847 π 2 − 1 ≈ 0.5708 +2.4% ANALCO 2015 scanned elements (∝ memory transfers / IOs) 1.6 2 −20% Algorithmica 2016 ·n ln n + O(n) average case results Summary: 1 Only convincing explanation for running times: memory transfers 2 Why was dual-pivot Quicksort not used earlier? Because it was not faster then! Sebastian Wild Quicksorts of the 21st century 2021-06-07 10 / 17
  126. Beyond Dual-Pivot: Multiway Partitioning Can we do even better than

    YBB Quicksort? Setup: Generic s-way one-pass partitioning s-way partitioning: split input into s 2 parts one pass over input m segments grow from left, s − m from right classify elements using BSTs: λk and λg pivots P1 , . . . , Ps−1 chosen from sample Sebastian Wild Quicksorts of the 21st century 2021-06-07 11 / 17
  127. Beyond Dual-Pivot: Multiway Partitioning Can we do even better than

    YBB Quicksort? Setup: Generic s-way one-pass partitioning s-way partitioning: split input into s 2 parts one pass over input m segments grow from left, s − m from right classify elements using BSTs: λk and λg pivots P1 , . . . , Ps−1 chosen from sample Sebastian Wild Quicksorts of the 21st century 2021-06-07 11 / 17
  128. Beyond Dual-Pivot: Multiway Partitioning Can we do even better than

    YBB Quicksort? Setup: Generic s-way one-pass partitioning s-way partitioning: split input into s 2 parts one pass over input m segments grow from left, s − m from right classify elements using BSTs: λk and λg pivots P1 , . . . , Ps−1 chosen from sample Sebastian Wild Quicksorts of the 21st century 2021-06-07 11 / 17
  129. Beyond Dual-Pivot: Multiway Partitioning Can we do even better than

    YBB Quicksort? Setup: Generic s-way one-pass partitioning s-way partitioning: split input into s 2 parts one pass over input m segments grow from left, s − m from right classify elements using BSTs: λk and λg pivots P1 , . . . , Ps−1 chosen from sample Sebastian Wild Quicksorts of the 21st century 2021-06-07 11 / 17
  130. Beyond Dual-Pivot: Multiway Partitioning Can we do even better than

    YBB Quicksort? Setup: Generic s-way one-pass partitioning s-way partitioning: split input into s 2 parts one pass over input m segments grow from left, s − m from right classify elements using BSTs: λk and λg pivots P1 , . . . , Ps−1 chosen from sample Sebastian Wild Quicksorts of the 21st century 2021-06-07 11 / 17
  131. Beyond Dual-Pivot: Multiway Partitioning Can we do even better than

    YBB Quicksort? Setup: Generic s-way one-pass partitioning s-way partitioning: split input into s 2 parts one pass over input m segments grow from left, s − m from right classify elements using BSTs: λk and λg pivots P1 , . . . , Ps−1 chosen from sample Sebastian Wild Quicksorts of the 21st century 2021-06-07 11 / 17
  132. Beyond Dual-Pivot: Multiway Partitioning Can we do even better than

    YBB Quicksort? Setup: Generic s-way one-pass partitioning s-way partitioning: split input into s 2 parts one pass over input m segments grow from left, s − m from right classify elements using BSTs: λk and λg Known Quicksort variants Method s m λk λg Classic 2 1 < Lomuto 2 2 YBB 3 2 < < Waterloo 4 2 < < < pivots P1 , . . . , Ps−1 chosen from sample Sebastian Wild Quicksorts of the 21st century 2021-06-07 11 / 17
  133. Beyond Dual-Pivot: Multiway Partitioning Can we do even better than

    YBB Quicksort? Setup: Generic s-way one-pass partitioning s-way partitioning: split input into s 2 parts one pass over input m segments grow from left, s − m from right classify elements using BSTs: λk and λg Known Quicksort variants Method s m λk λg Classic 2 1 < Lomuto 2 2 YBB 3 2 < < Waterloo 4 2 < < < pivots P1 , . . . , Ps−1 chosen from sample Sebastian Wild Quicksorts of the 21st century 2021-06-07 11 / 17
  134. Beyond Dual-Pivot: Multiway Partitioning Can we do even better than

    YBB Quicksort? Setup: Generic s-way one-pass partitioning s-way partitioning: split input into s 2 parts one pass over input m segments grow from left, s − m from right classify elements using BSTs: λk and λg Known Quicksort variants Method s m λk λg Classic 2 1 < Lomuto 2 2 YBB 3 2 < < Waterloo 4 2 < < < pivots P1 , . . . , Ps−1 chosen from sample Findings for dual-pivot Quicksort are typical: comparisons: negligible improvements scanned elements: significant improvements but: code becomes lengthy likely sweet spot for 2–7 pivots Sebastian Wild Quicksorts of the 21st century 2021-06-07 11 / 17
  135. Outline 1 Context 1 Context 2 Dual-Pivot Quicksort 2 Dual-Pivot

    Quicksort 3 Average-Case Analysis 3 Average-Case Analysis 4 Quicksort + X 4 Quicksort + X 5 Conclusion 5 Conclusion Sebastian Wild Quicksorts of the 21st century 2021-06-07 11 / 17
  136. 4 Quicksort + X 4 Quicksort + X Sebastian Wild

    Quicksorts of the 21st century 2021-06-07 11 / 17
  137. What’s Wrong with Quicksort? When sorting simple objects think: ints

    1 quicksort hard to beat! 2 memory transfers a substantial fraction of cost What if key comparisons are expensive? Quicksort is a factor > 1 off from optimal Heapsort? cache misses Mergesort? linear-size buffer needed (or slow) Sebastian Wild Quicksorts of the 21st century 2021-06-07 12 / 17
  138. What’s Wrong with Quicksort? When sorting simple objects think: ints

    1 quicksort hard to beat! 2 memory transfers a substantial fraction of cost What if key comparisons are expensive? Quicksort is a factor > 1 off from optimal Heapsort? cache misses Mergesort? linear-size buffer needed (or slow) Sebastian Wild Quicksorts of the 21st century 2021-06-07 12 / 17
  139. What’s Wrong with Quicksort? When sorting simple objects think: ints

    1 quicksort hard to beat! 2 memory transfers a substantial fraction of cost What if key comparisons are expensive? Quicksort is a factor > 1 off from optimal Heapsort? cache misses Mergesort? linear-size buffer needed (or slow) Sebastian Wild Quicksorts of the 21st century 2021-06-07 12 / 17
  140. What’s Wrong with Quicksort? When sorting simple objects think: ints

    1 quicksort hard to beat! 2 memory transfers a substantial fraction of cost What if key comparisons are expensive? Quicksort is a factor > 1 off from optimal Heapsort? cache misses Mergesort? linear-size buffer needed (or slow) Sebastian Wild Quicksorts of the 21st century 2021-06-07 12 / 17
  141. What’s Wrong with Quicksort? When sorting simple objects think: ints

    1 quicksort hard to beat! 2 memory transfers a substantial fraction of cost What if key comparisons are expensive? Quicksort is a factor > 1 off from optimal Heapsort? cache misses Mergesort? linear-size buffer needed (or slow) Sebastian Wild Quicksorts of the 21st century 2021-06-07 12 / 17
  142. What’s Wrong with Quicksort? When sorting simple objects think: ints

    1 quicksort hard to beat! 2 memory transfers a substantial fraction of cost What if key comparisons are expensive? Quicksort is a factor > 1 off from optimal Heapsort? cache misses Mergesort? linear-size buffer needed (or slow) Can quicksort help? Sebastian Wild Quicksorts of the 21st century 2021-06-07 12 / 17
  143. What is QuickXsort? QuickXsort = Quicksort + X some sorting

    method Sebastian Wild Quicksorts of the 21st century 2021-06-07 13 / 17
  144. What is QuickXsort? QuickXsort = Quicksort + X some sorting

    method QUICKXSORT(A[l..r]) 1 Choose pivot P. 2 i = PARTITION(A[l..r], P) // subproblems: A[l..i − 1] and A[i + 1..r] 3 Sort one subproblem with X. 4 Sort the other subproblem with QUICKXSORT. Sebastian Wild Quicksorts of the 21st century 2021-06-07 13 / 17
  145. What is QuickXsort? QuickXsort = Quicksort + X some sorting

    method QUICKXSORT(A[l..r]) 1 Choose pivot P. 2 i = PARTITION(A[l..r], P) // subproblems: A[l..i − 1] and A[i + 1..r] 3 Sort one subproblem with X. 4 Sort the other subproblem with QUICKXSORT. Sebastian Wild Quicksorts of the 21st century 2021-06-07 13 / 17
  146. What is QuickXsort? QuickXsort = Quicksort + X some sorting

    method QUICKXSORT(A[l..r]) 1 Choose pivot P median of k = 2t + 1 . 2 i = PARTITION(A[l..r], P) // subproblems: A[l..i − 1] and A[i + 1..r] 3 Sort one subproblem with X. 4 Sort the other subproblem with QUICKXSORT. Sebastian Wild Quicksorts of the 21st century 2021-06-07 13 / 17
  147. What is QuickXsort? QuickXsort = Quicksort + X some sorting

    method QUICKXSORT(A[l..r]) 1 Choose pivot P median of k = 2t + 1 . 2 i = PARTITION(A[l..r], P) // subproblems: A[l..i − 1] and A[i + 1..r] 3 Sort one subproblem with X. 4 Sort the other subproblem with QUICKXSORT. Sebastian Wild Quicksorts of the 21st century 2021-06-07 13 / 17
  148. What is QuickXsort? QuickXsort = Quicksort + X some sorting

    method QUICKXSORT(A[l..r]) 1 Choose pivot P median of k = 2t + 1 . 2 i = PARTITION(A[l..r], P) // subproblems: A[l..i − 1] and A[i + 1..r] 3 Sort one subproblem with X. 4 Sort the other subproblem with QUICKXSORT. sort by X Sebastian Wild Quicksorts of the 21st century 2021-06-07 13 / 17
  149. What is QuickXsort? QuickXsort = Quicksort + X some sorting

    method QUICKXSORT(A[l..r]) 1 Choose pivot P median of k = 2t + 1 . 2 i = PARTITION(A[l..r], P) // subproblems: A[l..i − 1] and A[i + 1..r] 3 Sort one subproblem with X. 4 Sort the other subproblem with QUICKXSORT. sort by X sort recursively Sebastian Wild Quicksorts of the 21st century 2021-06-07 13 / 17
  150. What is QuickXsort? QuickXsort = Quicksort + X some sorting

    method QUICKXSORT(A[l..r]) 1 Choose pivot P median of k = 2t + 1 . 2 i = PARTITION(A[l..r], P) // subproblems: A[l..i − 1] and A[i + 1..r] 3 Sort one subproblem with X. 4 Sort the other subproblem with QUICKXSORT. sort by X sort recursively Sebastian Wild Quicksorts of the 21st century 2021-06-07 13 / 17
  151. What is QuickXsort? QuickXsort = Quicksort + X some sorting

    method QUICKXSORT(A[l..r]) 1 Choose pivot P median of k = 2t + 1 . 2 i = PARTITION(A[l..r], P) // subproblems: A[l..i − 1] and A[i + 1..r] 3 Sort one subproblem with X OK, but why? Why not simple use X? . 4 Sort the other subproblem with QUICKXSORT. sort by X sort recursively Sebastian Wild Quicksorts of the 21st century 2021-06-07 13 / 17
  152. What is QuickXsort? QuickXsort = Quicksort + X some sorting

    method QUICKXSORT(A[l..r]) 1 Choose pivot P median of k = 2t + 1 . 2 i = PARTITION(A[l..r], P) // subproblems: A[l..i − 1] and A[i + 1..r] 3 Sort one subproblem with X OK, but why? Why not simple use X? , using the other subproblem as buffer area. 4 Sort the other subproblem with QUICKXSORT. sort by X sort recursively Sebastian Wild Quicksorts of the 21st century 2021-06-07 13 / 17
  153. What is QuickXsort? QuickXsort = Quicksort + X some sorting

    method QUICKXSORT(A[l..r]) 1 Choose pivot P median of k = 2t + 1 . 2 i = PARTITION(A[l..r], P) // subproblems: A[l..i − 1] and A[i + 1..r] 3 Sort one subproblem with X, using the other subproblem as buffer area. 4 Sort the other subproblem with QUICKXSORT. sort by X sort recursively QuickXsort turns X into an in-place sorting method! Here: X = Mergesort inherit good comparison count, but work in-place! Sebastian Wild Quicksorts of the 21st century 2021-06-07 13 / 17
  154. What is QuickXsort? QuickXsort = Quicksort + X some sorting

    method QUICKXSORT(A[l..r]) 1 Choose pivot P median of k = 2t + 1 . 2 i = PARTITION(A[l..r], P) // subproblems: A[l..i − 1] and A[i + 1..r] 3 Sort one subproblem with X, using the other subproblem as buffer area. 4 Sort the other subproblem with QUICKXSORT. sort by X sort recursively QuickXsort turns X into an in-place sorting method! Here: X = Mergesort inherit good comparison count, but work in-place! Sebastian Wild Quicksorts of the 21st century 2021-06-07 13 / 17
  155. What is QuickXsort? QuickXsort = Quicksort + X some sorting

    method QUICKXSORT(A[l..r]) 1 Choose pivot P median of k = 2t + 1 . 2 i = PARTITION(A[l..r], P) // subproblems: A[l..i − 1] and A[i + 1..r] 3 Sort one subproblem with X, using the other subproblem as buffer area. 4 Sort the other subproblem with QUICKXSORT. sort by X sort recursively QuickXsort turns X into an in-place sorting method! Here: X = Mergesort inherit good comparison count, but work in-place! Sebastian Wild Quicksorts of the 21st century 2021-06-07 13 / 17
  156. What is QuickXsort? QuickXsort = Quicksort + X some sorting

    method QUICKXSORT(A[l..r]) 1 Choose pivot P median of k = 2t + 1 . 2 i = PARTITION(A[l..r], P) // subproblems: A[l..i − 1] and A[i + 1..r] 3 Sort one subproblem with X, using the other subproblem as buffer area. 4 Sort the other subproblem with QUICKXSORT. sort by X sort recursively QuickXsort turns X into an in-place sorting method! Here: X = Mergesort inherit good comparison count Or...do we? , but work in-place! Sebastian Wild Quicksorts of the 21st century 2021-06-07 13 / 17
  157. Merging with swaps Important: Buffer elements must stay intact. run1

    run2 buffer 1 Swap run1 and buffer 2 Merge back (swap min) 3 runs merged, buffer restored (but permuted) Sebastian Wild Quicksorts of the 21st century 2021-06-07 14 / 17
  158. Merging with swaps Important: Buffer elements must stay intact. run1

    run2 buffer 1 Swap run1 and buffer 2 Merge back (swap min) 3 runs merged, buffer restored (but permuted) Sebastian Wild Quicksorts of the 21st century 2021-06-07 14 / 17
  159. Merging with swaps Important: Buffer elements must stay intact. run1

    run2 buffer 1 Swap run1 and buffer 2 Merge back (swap min) 3 runs merged, buffer restored (but permuted) Sebastian Wild Quicksorts of the 21st century 2021-06-07 14 / 17
  160. Merging with swaps Important: Buffer elements must stay intact. run1

    run2 buffer 1 Swap run1 and buffer 2 Merge back (swap min) 3 runs merged, buffer restored (but permuted) Sebastian Wild Quicksorts of the 21st century 2021-06-07 14 / 17
  161. Merging with swaps Important: Buffer elements must stay intact. run1

    run2 buffer 1 Swap run1 and buffer 2 Merge back (swap min) 3 runs merged, buffer restored (but permuted) Sebastian Wild Quicksorts of the 21st century 2021-06-07 14 / 17
  162. Merging with swaps Important: Buffer elements must stay intact. run1

    run2 buffer 1 Swap run1 and buffer 2 Merge back (swap min) 3 runs merged, buffer restored (but permuted) Sebastian Wild Quicksorts of the 21st century 2021-06-07 14 / 17
  163. Merging with swaps Important: Buffer elements must stay intact. run1

    run2 buffer 1 Swap run1 and buffer 2 Merge back (swap min) 3 runs merged, buffer restored (but permuted) Sebastian Wild Quicksorts of the 21st century 2021-06-07 14 / 17
  164. Merging with swaps Important: Buffer elements must stay intact. run1

    run2 buffer 1 Swap run1 and buffer 2 Merge back (swap min) 3 runs merged, buffer restored (but permuted) Sebastian Wild Quicksorts of the 21st century 2021-06-07 14 / 17
  165. Merging with swaps Important: Buffer elements must stay intact. run1

    run2 buffer 1 Swap run1 and buffer 2 Merge back (swap min) 3 runs merged, buffer restored (but permuted) Sebastian Wild Quicksorts of the 21st century 2021-06-07 14 / 17
  166. Merging with swaps Important: Buffer elements must stay intact. run1

    run2 buffer 1 Swap run1 and buffer 2 Merge back (swap min) 3 runs merged, buffer restored (but permuted) Sebastian Wild Quicksorts of the 21st century 2021-06-07 14 / 17
  167. Merging with swaps Important: Buffer elements must stay intact. run1

    run2 buffer 1 Swap run1 and buffer 2 Merge back (swap min) 3 runs merged, buffer restored (but permuted) Sebastian Wild Quicksorts of the 21st century 2021-06-07 14 / 17
  168. Merging with swaps Important: Buffer elements must stay intact. run1

    run2 buffer 1 Swap run1 and buffer 2 Merge back (swap min) 3 runs merged, buffer restored (but permuted) Sebastian Wild Quicksorts of the 21st century 2021-06-07 14 / 17
  169. Merging with swaps Important: Buffer elements must stay intact. run1

    run2 buffer 1 Swap run1 and buffer 2 Merge back (swap min) 3 runs merged, buffer restored (but permuted) Sebastian Wild Quicksorts of the 21st century 2021-06-07 14 / 17
  170. Merging with swaps Important: Buffer elements must stay intact. run1

    run2 buffer 1 Swap run1 and buffer 2 Merge back (swap min) 3 runs merged, buffer restored (but permuted) Sebastian Wild Quicksorts of the 21st century 2021-06-07 14 / 17
  171. Merging with swaps Important: Buffer elements must stay intact. run1

    run2 buffer 1 Swap run1 and buffer 2 Merge back (swap min) 3 runs merged, buffer restored (but permuted) Sebastian Wild Quicksorts of the 21st century 2021-06-07 14 / 17
  172. Merging with swaps Important: Buffer elements must stay intact. run1

    run2 buffer 1 Swap run1 and buffer 2 Merge back (swap min) 3 runs merged, buffer restored (but permuted) Sebastian Wild Quicksorts of the 21st century 2021-06-07 14 / 17
  173. Merging with swaps Important: Buffer elements must stay intact. run1

    run2 buffer 1 Swap run1 and buffer 2 Merge back (swap min) 3 runs merged, buffer restored (but permuted) Sebastian Wild Quicksorts of the 21st century 2021-06-07 14 / 17
  174. Merging with swaps Important: Buffer elements must stay intact. run1

    run2 buffer 1 Swap run1 and buffer 2 Merge back (swap min) 3 runs merged, buffer restored (but permuted) Sebastian Wild Quicksorts of the 21st century 2021-06-07 14 / 17
  175. Merging with swaps Important: Buffer elements must stay intact. run1

    run2 buffer 1 Swap run1 and buffer 2 Merge back (swap min) 3 runs merged, buffer restored (but permuted) Sebastian Wild Quicksorts of the 21st century 2021-06-07 14 / 17
  176. Anlysis of QuickXsort Comparisons in QuickXsort exact average case results

    with pivots as median of 2t + 1 elements c(n) = x(n) + qt · n ± O(log n) Sebastian Wild Quicksorts of the 21st century 2021-06-07 15 / 17
  177. Anlysis of QuickXsort Comparisons in QuickXsort exact average case results

    with pivots as median of 2t + 1 elements c(n) = x(n) + qt · n ± O(log n) Sebastian Wild Quicksorts of the 21st century 2021-06-07 15 / 17
  178. Anlysis of QuickXsort Comparisons in QuickXsort exact average case results

    with pivots as median of 2t + 1 elements c(n) = x(n) same as X ... + qt · n ± O(log n) Sebastian Wild Quicksorts of the 21st century 2021-06-07 15 / 17
  179. Anlysis of QuickXsort Comparisons in QuickXsort exact average case results

    with pivots as median of 2t + 1 elements c(n) = x(n) same as X ... + qt ... + “QuickXsort penalty” · n ± O(log n) Sebastian Wild Quicksorts of the 21st century 2021-06-07 15 / 17
  180. Anlysis of QuickXsort Comparisons in QuickXsort exact average case results

    with pivots as median of 2t + 1 elements c(n) = x(n) same as X ... + qt ... + “QuickXsort penalty” · n ± O(log n) t = 0 0 0.2 0.4 0.6 0.8 1 1.2 qt for QuickMergesort 0.91 random pivots Sebastian Wild Quicksorts of the 21st century 2021-06-07 15 / 17
  181. Anlysis of QuickXsort Comparisons in QuickXsort exact average case results

    with pivots as median of 2t + 1 elements c(n) = x(n) same as X ... + qt ... + “QuickXsort penalty” · n ± O(log n) t = 0 t = 1 0 0.2 0.4 0.6 0.8 1 1.2 qt for QuickMergesort 0.91 0.41 random pivots median of 3 Sebastian Wild Quicksorts of the 21st century 2021-06-07 15 / 17
  182. Anlysis of QuickXsort Comparisons in QuickXsort exact average case results

    with pivots as median of 2t + 1 elements c(n) = x(n) same as X ... + qt ... + “QuickXsort penalty” · n ± O(log n) t = 0 t = 1 t = 2 0 0.2 0.4 0.6 0.8 1 1.2 qt for QuickMergesort 0.91 0.41 0.25 random pivots median of 3 median of 5 Sebastian Wild Quicksorts of the 21st century 2021-06-07 15 / 17
  183. Anlysis of QuickXsort Comparisons in QuickXsort exact average case results

    with pivots as median of 2t + 1 elements c(n) = x(n) same as X ... + qt ... + “QuickXsort penalty” · n ± O(log n) t = 0 t = 1 t = 2 t = 3 0 0.2 0.4 0.6 0.8 1 1.2 qt for QuickMergesort 0.91 0.41 0.25 0.18 random pivots median of 3 median of 5 median of 7 Sebastian Wild Quicksorts of the 21st century 2021-06-07 15 / 17
  184. Anlysis of QuickXsort Comparisons in QuickXsort exact average case results

    with pivots as median of 2t + 1 elements c(n) = x(n) same as X ... + qt ... + “QuickXsort penalty” · n ± O(log n) t = 0 t = 1 t = 2 t = 3 t = 10 0 0.2 0.4 0.6 0.8 1 1.2 qt for QuickMergesort 0.91 0.41 0.25 0.18 0.06 random pivots median of 3 median of 5 median of 7 median of 21 Sebastian Wild Quicksorts of the 21st century 2021-06-07 15 / 17
  185. Anlysis of QuickXsort Comparisons in QuickXsort exact average case results

    with pivots as median of 2t + 1 elements c(n) = x(n) same as X ... + qt ... + “QuickXsort penalty” · n ± O(log n) t = 0 t = 1 t = 2 t = 3 t = 10 t → ∞ 0 0.2 0.4 0.6 0.8 1 1.2 qt for QuickMergesort 0.91 0.41 0.25 0.18 0.06 0 random pivots median of 3 median of 5 median of 7 median of 21 Sebastian Wild Quicksorts of the 21st century 2021-06-07 15 / 17
  186. Anlysis of QuickXsort Comparisons in QuickXsort exact average case results

    with pivots as median of 2t + 1 elements c(n) = x(n) same as X ... + qt ... + “QuickXsort penalty” · n ± O(log n) Old quicksort wisdom rediscovered: small samples give most of gain #comparisons close to optimal running-time efficiency confirmed in experiments Can get in-place with tiny overhead t = 0 t = 1 t = 2 t = 3 t = 10 t → ∞ 0 0.2 0.4 0.6 0.8 1 1.2 qt for QuickMergesort 0.91 0.41 0.25 0.18 0.06 0 random pivots median of 3 median of 5 median of 7 median of 21 Sebastian Wild Quicksorts of the 21st century 2021-06-07 15 / 17
  187. Anlysis of QuickXsort Comparisons in QuickXsort exact average case results

    with pivots as median of 2t + 1 elements c(n) = x(n) same as X ... + qt ... + “QuickXsort penalty” · n ± O(log n) Old quicksort wisdom rediscovered: small samples give most of gain #comparisons close to optimal running-time efficiency confirmed in experiments Can get in-place with tiny overhead t = 0 t = 1 t = 2 t = 3 t = 10 t → ∞ 0 0.2 0.4 0.6 0.8 1 1.2 qt for QuickMergesort 0.91 0.41 0.25 0.18 0.06 0 random pivots median of 3 median of 5 median of 7 median of 21 Sebastian Wild Quicksorts of the 21st century 2021-06-07 15 / 17
  188. Anlysis of QuickXsort Comparisons in QuickXsort exact average case results

    with pivots as median of 2t + 1 elements c(n) = x(n) same as X ... + qt ... + “QuickXsort penalty” · n ± O(log n) Old quicksort wisdom rediscovered: small samples give most of gain #comparisons close to optimal running-time efficiency confirmed in experiments Can get in-place with tiny overhead t = 0 t = 1 t = 2 t = 3 t = 10 t → ∞ 0 0.2 0.4 0.6 0.8 1 1.2 qt for QuickMergesort 0.91 0.41 0.25 0.18 0.06 0 random pivots median of 3 median of 5 median of 7 median of 21 Sebastian Wild Quicksorts of the 21st century 2021-06-07 15 / 17
  189. Anlysis of QuickXsort Comparisons in QuickXsort exact average case results

    with pivots as median of 2t + 1 elements c(n) = x(n) same as X ... + qt ... + “QuickXsort penalty” · n ± O(log n) Old quicksort wisdom rediscovered: small samples give most of gain #comparisons close to optimal running-time efficiency confirmed in experiments Can get in-place with tiny overhead t = 0 t = 1 t = 2 t = 3 t = 10 t → ∞ 0 0.2 0.4 0.6 0.8 1 1.2 qt for QuickMergesort 0.91 0.41 0.25 0.18 0.06 0 random pivots median of 3 median of 5 median of 7 median of 21 Sebastian Wild Quicksorts of the 21st century 2021-06-07 15 / 17
  190. Outline 1 Context 1 Context 2 Dual-Pivot Quicksort 2 Dual-Pivot

    Quicksort 3 Average-Case Analysis 3 Average-Case Analysis 4 Quicksort + X 4 Quicksort + X 5 Conclusion 5 Conclusion Sebastian Wild Quicksorts of the 21st century 2021-06-07 15 / 17
  191. 5 Conclusion 5 Conclusion Sebastian Wild Quicksorts of the 21st

    century 2021-06-07 15 / 17
  192. My decade with Quicksort You have seen: 1 tricky asymmetries

    in YBB Quicksort < < save a few comparisons 2 evidence for growing weight of memory transfer costs 3 Multiway Quicksort reduces memory transfer No other optimization of Quicksort achieves that 4 Quicksort continues to inspire new algorithms and algorithmists! sort by X recurse Sebastian Wild Quicksorts of the 21st century 2021-06-07 16 / 17
  193. My decade with Quicksort You have seen: 1 tricky asymmetries

    in YBB Quicksort < < save a few comparisons 2 evidence for growing weight of memory transfer costs 3 Multiway Quicksort reduces memory transfer No other optimization of Quicksort achieves that 4 Quicksort continues to inspire new algorithms and algorithmists! sort by X recurse Sebastian Wild Quicksorts of the 21st century 2021-06-07 16 / 17
  194. My decade with Quicksort You have seen: 1 tricky asymmetries

    in YBB Quicksort < < save a few comparisons 2 evidence for growing weight of memory transfer costs 3 Multiway Quicksort reduces memory transfer No other optimization of Quicksort achieves that 4 Quicksort continues to inspire new algorithms and algorithmists! sort by X recurse Sebastian Wild Quicksorts of the 21st century 2021-06-07 16 / 17
  195. My decade with Quicksort You have seen: 1 tricky asymmetries

    in YBB Quicksort < < save a few comparisons 2 evidence for growing weight of memory transfer costs 3 Multiway Quicksort reduces memory transfer No other optimization of Quicksort achieves that 4 Quicksort continues to inspire new algorithms and algorithmists! sort by X recurse Sebastian Wild Quicksorts of the 21st century 2021-06-07 16 / 17
  196. My decade with Quicksort You have seen: 1 tricky asymmetries

    in YBB Quicksort < < save a few comparisons 2 evidence for growing weight of memory transfer costs 3 Multiway Quicksort reduces memory transfer No other optimization of Quicksort achieves that 4 Quicksort continues to inspire new algorithms and algorithmists! sort by X recurse Sebastian Wild Quicksorts of the 21st century 2021-06-07 16 / 17
  197. My decade with Quicksort You have seen: 1 tricky asymmetries

    in YBB Quicksort < < save a few comparisons 2 evidence for growing weight of memory transfer costs 3 Multiway Quicksort reduces memory transfer No other optimization of Quicksort achieves that 4 Quicksort continues to inspire new algorithms and algorithmists! sort by X recurse Sebastian Wild Quicksorts of the 21st century 2021-06-07 16 / 17
  198. My decade with Quicksort You have seen: 1 tricky asymmetries

    in YBB Quicksort < < save a few comparisons 2 evidence for growing weight of memory transfer costs 3 Multiway Quicksort reduces memory transfer No other optimization of Quicksort achieves that 4 Quicksort continues to inspire new algorithms and algorithmists! sort by X recurse You have not seen: Treatment of equal keys 4 2 1 3 3 5 4 4 3 5 2 2 1 2 4 5 4 4 5 3 3 3 5 5 4 4 4 2 1 2 5 5 4 2 1 3 3 5 4 4 3 5 2 3 4 5 5 2 1 2 Limiting distributions of costs n = 3 n = 5 n = 10 n = 15 n = 20 n = 25 Sesquickselect (cache-efficient selection) 0 ν 1 − ν 1 Intriguing interplay of parameters Sebastian Wild Quicksorts of the 21st century 2021-06-07 16 / 17
  199. My decade with Quicksort You have seen: 1 tricky asymmetries

    in YBB Quicksort < < save a few comparisons 2 evidence for growing weight of memory transfer costs 3 Multiway Quicksort reduces memory transfer No other optimization of Quicksort achieves that 4 Quicksort continues to inspire new algorithms and algorithmists! sort by X recurse You have not seen: Treatment of equal keys 4 2 1 3 3 5 4 4 3 5 2 2 1 2 4 5 4 4 5 3 3 3 5 5 4 4 4 2 1 2 5 5 4 2 1 3 3 5 4 4 3 5 2 3 4 5 5 2 1 2 Limiting distributions of costs n = 3 n = 5 n = 10 n = 15 n = 20 n = 25 Sesquickselect (cache-efficient selection) 0 ν 1 − ν 1 Intriguing interplay of parameters Sebastian Wild Quicksorts of the 21st century 2021-06-07 16 / 17
  200. My decade with Quicksort You have seen: 1 tricky asymmetries

    in YBB Quicksort < < save a few comparisons 2 evidence for growing weight of memory transfer costs 3 Multiway Quicksort reduces memory transfer No other optimization of Quicksort achieves that 4 Quicksort continues to inspire new algorithms and algorithmists! sort by X recurse You have not seen: Treatment of equal keys 4 2 1 3 3 5 4 4 3 5 2 2 1 2 4 5 4 4 5 3 3 3 5 5 4 4 4 2 1 2 5 5 4 2 1 3 3 5 4 4 3 5 2 3 4 5 5 2 1 2 Limiting distributions of costs n = 3 n = 5 n = 10 n = 15 n = 20 n = 25 Sesquickselect (cache-efficient selection) 0 ν 1 − ν 1 Intriguing interplay of parameters Sebastian Wild Quicksorts of the 21st century 2021-06-07 16 / 17
  201. My decade with Quicksort You have seen: 1 tricky asymmetries

    in YBB Quicksort < < save a few comparisons 2 evidence for growing weight of memory transfer costs 3 Multiway Quicksort reduces memory transfer No other optimization of Quicksort achieves that 4 Quicksort continues to inspire new algorithms and algorithmists! sort by X recurse You have not seen: Treatment of equal keys 4 2 1 3 3 5 4 4 3 5 2 2 1 2 4 5 4 4 5 3 3 3 5 5 4 4 4 2 1 2 5 5 4 2 1 3 3 5 4 4 3 5 2 3 4 5 5 2 1 2 Limiting distributions of costs n = 3 n = 5 n = 10 n = 15 n = 20 n = 25 Sesquickselect (cache-efficient selection) 0 ν 1 − ν 1 Intriguing interplay of parameters Sebastian Wild Quicksorts of the 21st century 2021-06-07 16 / 17
  202. My decade with Quicksort You have seen: 1 tricky asymmetries

    in YBB Quicksort < < save a few comparisons 2 evidence for growing weight of memory transfer costs 3 Multiway Quicksort reduces memory transfer No other optimization of Quicksort achieves that 4 Quicksort continues to inspire new algorithms and algorithmists! sort by X recurse You have not seen: Treatment of equal keys 4 2 1 3 3 5 4 4 3 5 2 2 1 2 4 5 4 4 5 3 3 3 5 5 4 4 4 2 1 2 5 5 4 2 1 3 3 5 4 4 3 5 2 3 4 5 5 2 1 2 Limiting distributions of costs n = 3 n = 5 n = 10 n = 15 n = 20 n = 25 Sesquickselect (cache-efficient selection) 0 ν 1 − ν 1 Intriguing interplay of parameters Sebastian Wild Quicksorts of the 21st century 2021-06-07 16 / 17
  203. Sebastian Wild Quicksorts of the 21st century 2021-06-07 17 /

    17
  204. References Most presented results appear in my thesis: Dual-Pivot Quicksort

    And Beyond Sebastian Wild · TU Kaiserslautern · 2016 Further Articles: Sesquickselect: One and a half pivots for cache-efficient selection Conrado Martínez, Markus Nebel, Sebastian Wild · ANALCO · 2019 QuickXsort – A Fast Sorting Scheme in Theory and Practice Stefan Edelkamp, Armin Weiß, Sebastian Wild · Algorithmica · 2020 Quicksort Is Optimal for Many Equal Keys Sebastian Wild · ANALCO · 2018 Analysis of Quickselect under Yaroslavskiy’s Dual-Pivoting Algorithm Sebastian Wild, Markus Nebel, Hosam Mahmoud · Algorithmica · 2016 Analysis of Pivot Sampling in Dual-Pivot Quicksort Markus Nebel, Sebastian Wild, Conrado Martínez · Algorithmica · 2016 Analysis of Branch Misses in Quicksort Conrado Martínez, Markus Nebel, Sebastian Wild · ANALCO · 2015 Average Case and Distributional Analysis of Dual-Pivot Quicksort Sebastian Wild, Markus Nebel, Ralph Neininger · ACM Trans. Alg. · 2015 Average-Case Analysis of Java 7’s Dual-Pivot Quicksort Sebastian Wild, Markus Nebel · ESA · 2012 Sebastian Wild Quicksorts of the 21st century 2021-06-07 18 / 17
  205. Basic Algorithms CLASSICQUICKSORT(A, left, right) 1 if right left then

    return 2 P := A[right] 3 k := left − 1; g := right 4 while true 5 do k := k + 1 while A[k] < P end while 6 do g := g − 1 while A[g] > P end while 7 if k g then break while end if 8 Swap A[k] and A[g] 9 end while 10 Swap A[k] and A[right] 11 CLASSICQUICKSORT(A, left , k − 1) 12 CLASSICQUICKSORT(A, k + 1, right ) Sebastian Wild Quicksorts of the 21st century 2021-06-07 19 / 17
  206. Basic Algorithms CLASSICQUICKSORT(A, left, right) 1 if right left then

    return 2 P := A[right] 3 k := left − 1; g := right 4 while true 5 do k := k + 1 while A[k] < P end while 6 do g := g − 1 while A[g] > P end while 7 if k g then break while end if 8 Swap A[k] and A[g] 9 end while 10 Swap A[k] and A[right] 11 CLASSICQUICKSORT(A, left , k − 1) 12 CLASSICQUICKSORT(A, k + 1, right ) Sebastian Wild Quicksorts of the 21st century 2021-06-07 19 / 17
  207. Basic Algorithms CLASSICQUICKSORT(A, left, right) 1 if right left then

    return 2 P := A[right] 3 k := left − 1; g := right 4 while true 5 do k := k + 1 while A[k] < P end while 6 do g := g − 1 while A[g] > P end while 7 if k g then break while end if 8 Swap A[k] and A[g] 9 end while 10 Swap A[k] and A[right] 11 CLASSICQUICKSORT(A, left , k − 1) 12 CLASSICQUICKSORT(A, k + 1, right ) Classic Quicksort crossing-pointer scheme symmetric treatment Sebastian Wild Quicksorts of the 21st century 2021-06-07 19 / 17
  208. Basic Algorithms CLASSICQUICKSORT(A, left, right) 1 if right left then

    return 2 P := A[right] 3 k := left − 1; g := right 4 while true 5 do k := k + 1 while A[k] < P end while 6 do g := g − 1 while A[g] > P end while 7 if k g then break while end if 8 Swap A[k] and A[g] 9 end while 10 Swap A[k] and A[right] 11 CLASSICQUICKSORT(A, left , k − 1) 12 CLASSICQUICKSORT(A, k + 1, right ) Classic Quicksort crossing-pointer scheme symmetric treatment Sebastian Wild Quicksorts of the 21st century 2021-06-07 19 / 17
  209. Basic Algorithms CLASSICQUICKSORT(A, left, right) 1 if right left then

    return 2 P := A[right] 3 k := left − 1; g := right 4 while true 5 do k := k + 1 while A[k] < P end while 6 do g := g − 1 while A[g] > P end while 7 if k g then break while end if 8 Swap A[k] and A[g] 9 end while 10 Swap A[k] and A[right] 11 CLASSICQUICKSORT(A, left , k − 1) 12 CLASSICQUICKSORT(A, k + 1, right ) Classic Quicksort crossing-pointer scheme symmetric treatment Sebastian Wild Quicksorts of the 21st century 2021-06-07 19 / 17
  210. Basic Algorithms CLASSICQUICKSORT(A, left, right) 1 if right left then

    return 2 P := A[right] 3 k := left − 1; g := right 4 while true 5 do k := k + 1 while A[k] < P end while 6 do g := g − 1 while A[g] > P end while 7 if k g then break while end if 8 Swap A[k] and A[g] 9 end while 10 Swap A[k] and A[right] 11 CLASSICQUICKSORT(A, left , k − 1) 12 CLASSICQUICKSORT(A, k + 1, right ) Classic Quicksort crossing-pointer scheme symmetric treatment Invariant k g Sebastian Wild Quicksorts of the 21st century 2021-06-07 19 / 17
  211. Basic Algorithms CLASSICQUICKSORT(A, left, right) 1 if right left then

    return 2 P := A[right] 3 k := left − 1; g := right 4 while true 5 do k := k + 1 while A[k] < P end while 6 do g := g − 1 while A[g] > P end while 7 if k g then break while end if 8 Swap A[k] and A[g] 9 end while 10 Swap A[k] and A[right] 11 CLASSICQUICKSORT(A, left , k − 1) 12 CLASSICQUICKSORT(A, k + 1, right ) Classic Quicksort crossing-pointer scheme symmetric treatment Invariant k g YBB Quicksort ...looks messy YBBQUICKSORT(A, left, right) 1 if right left then return 2 P1 := A[left]; P2 := A[right] 3 if P1 > P2 then Swap P1 and P2 end if 4 := left + 1; g := right − 1; k := 5 while k g 6 if A[k] < P1 7 Swap A[k] and A[ ] ; := + 1 8 else if A[k] P2 9 while A[g] > P2 and k < g 10 g := g − 1 11 end while 12 Swap A[k] and A[g] ; g := g − 1 13 if A[k] < P1 14 Swap A[k] and A[ ] ; := + 1 15 end if 16 end if 17 k := k + 1 18 end while 19 := − 1; g := g + 1 20 Swap A[left] and A[ ] ; Swap A[right] and A[g] 21 YBBQUICKSORT(A, left , − 1) 22 YBBQUICKSORT(A, + 1, g − 1) 23 YBBQUICKSORT(A, g + 1, right ) Sebastian Wild Quicksorts of the 21st century 2021-06-07 19 / 17
  212. Basic Algorithms CLASSICQUICKSORT(A, left, right) 1 if right left then

    return 2 P := A[right] 3 k := left − 1; g := right 4 while true 5 do k := k + 1 while A[k] < P end while 6 do g := g − 1 while A[g] > P end while 7 if k g then break while end if 8 Swap A[k] and A[g] 9 end while 10 Swap A[k] and A[right] 11 CLASSICQUICKSORT(A, left , k − 1) 12 CLASSICQUICKSORT(A, k + 1, right ) Classic Quicksort crossing-pointer scheme symmetric treatment Invariant k g YBB Quicksort ...looks messy YBBQUICKSORT(A, left, right) 1 if right left then return 2 P1 := A[left]; P2 := A[right] 3 if P1 > P2 then Swap P1 and P2 end if 4 := left + 1; g := right − 1; k := 5 while k g 6 if A[k] < P1 7 Swap A[k] and A[ ] ; := + 1 8 else if A[k] P2 9 while A[g] > P2 and k < g 10 g := g − 1 11 end while 12 Swap A[k] and A[g] ; g := g − 1 13 if A[k] < P1 14 Swap A[k] and A[ ] ; := + 1 15 end if 16 end if 17 k := k + 1 18 end while 19 := − 1; g := g + 1 20 Swap A[left] and A[ ] ; Swap A[right] and A[g] 21 YBBQUICKSORT(A, left , − 1) 22 YBBQUICKSORT(A, + 1, g − 1) 23 YBBQUICKSORT(A, g + 1, right ) Sebastian Wild Quicksorts of the 21st century 2021-06-07 19 / 17
  213. Basic Algorithms CLASSICQUICKSORT(A, left, right) 1 if right left then

    return 2 P := A[right] 3 k := left − 1; g := right 4 while true 5 do k := k + 1 while A[k] < P end while 6 do g := g − 1 while A[g] > P end while 7 if k g then break while end if 8 Swap A[k] and A[g] 9 end while 10 Swap A[k] and A[right] 11 CLASSICQUICKSORT(A, left , k − 1) 12 CLASSICQUICKSORT(A, k + 1, right ) Classic Quicksort crossing-pointer scheme symmetric treatment Invariant k g YBB Quicksort ...looks messy YBBQUICKSORT(A, left, right) 1 if right left then return 2 P1 := A[left]; P2 := A[right] 3 if P1 > P2 then Swap P1 and P2 end if 4 := left + 1; g := right − 1; k := 5 while k g 6 if A[k] < P1 7 Swap A[k] and A[ ] ; := + 1 8 else if A[k] P2 9 while A[g] > P2 and k < g 10 g := g − 1 11 end while 12 Swap A[k] and A[g] ; g := g − 1 13 if A[k] < P1 14 Swap A[k] and A[ ] ; := + 1 15 end if 16 end if 17 k := k + 1 18 end while 19 := − 1; g := g + 1 20 Swap A[left] and A[ ] ; Swap A[right] and A[g] 21 YBBQUICKSORT(A, left , − 1) 22 YBBQUICKSORT(A, + 1, g − 1) 23 YBBQUICKSORT(A, g + 1, right ) Invariant k g Sebastian Wild Quicksorts of the 21st century 2021-06-07 19 / 17
  214. Basic Algorithms CLASSICQUICKSORT(A, left, right) 1 if right left then

    return 2 P := A[right] 3 k := left − 1; g := right 4 while true 5 do k := k + 1 while A[k] < P end while 6 do g := g − 1 while A[g] > P end while 7 if k g then break while end if 8 Swap A[k] and A[g] 9 end while 10 Swap A[k] and A[right] 11 CLASSICQUICKSORT(A, left , k − 1) 12 CLASSICQUICKSORT(A, k + 1, right ) Classic Quicksort crossing-pointer scheme symmetric treatment Invariant k g YBB Quicksort ...looks messy YBBQUICKSORT(A, left, right) 1 if right left then return 2 P1 := A[left]; P2 := A[right] 3 if P1 > P2 then Swap P1 and P2 end if 4 := left + 1; g := right − 1; k := 5 while k g 6 if A[k] < P1 7 Swap A[k] and A[ ] ; := + 1 8 else if A[k] P2 9 while A[g] > P2 and k < g 10 g := g − 1 11 end while 12 Swap A[k] and A[g] ; g := g − 1 13 if A[k] < P1 14 Swap A[k] and A[ ] ; := + 1 15 end if 16 end if 17 k := k + 1 18 end while 19 := − 1; g := g + 1 20 Swap A[left] and A[ ] ; Swap A[right] and A[g] 21 YBBQUICKSORT(A, left , − 1) 22 YBBQUICKSORT(A, + 1, g − 1) 23 YBBQUICKSORT(A, g + 1, right ) Invariant k g Sebastian Wild Quicksorts of the 21st century 2021-06-07 19 / 17
  215. QuickMergesort QUICKMERGESORT(A[l..r]) 1 Choose pivot P. 2 i = PARTITION(A[l..r],

    P) 5 if ... 6 MERGESORT(A[i + 1..r]) using buffer A[l..i − 1] Mergesort QuickMergesort 7 QUICKMERGESORT(A[l..i − 1]). 8 else 9 MERGESORT(A[l..i − 1]) using buffer A[i + 1..r] Mergesort QuickMergesort 10 QUICKMERGESORT(A[i..r]). 11 end if Sebastian Wild Quicksorts of the 21st century 2021-06-07 20 / 17
  216. QuickMergesort QUICKMERGESORT(A[l..r]) 1 Choose pivot P. 2 i = PARTITION(A[l..r],

    P) 3 J1 := i − l; J2 := r − i 5 if ... 6 MERGESORT(A[i + 1..r]) using buffer A[l..i − 1] Mergesort QuickMergesort 7 QUICKMERGESORT(A[l..i − 1]). 8 else 9 MERGESORT(A[l..i − 1]) using buffer A[i + 1..r] Mergesort QuickMergesort 10 QUICKMERGESORT(A[i..r]). 11 end if Sebastian Wild Quicksorts of the 21st century 2021-06-07 20 / 17
  217. QuickMergesort QUICKMERGESORT(A[l..r]) 1 Choose pivot P. 2 i = PARTITION(A[l..r],

    P) 3 J1 := i − l; J2 := r − i 4 largerWithX := min{J1 , J2 } 1 2 max{J1 , J2 } 5 if ... 6 MERGESORT(A[i + 1..r]) using buffer A[l..i − 1] Mergesort QuickMergesort 7 QUICKMERGESORT(A[l..i − 1]). 8 else 9 MERGESORT(A[l..i − 1]) using buffer A[i + 1..r] Mergesort QuickMergesort 10 QUICKMERGESORT(A[i..r]). 11 end if Sebastian Wild Quicksorts of the 21st century 2021-06-07 20 / 17
  218. QuickMergesort QUICKMERGESORT(A[l..r]) 1 Choose pivot P. 2 i = PARTITION(A[l..r],

    P) 3 J1 := i − l; J2 := r − i 4 largerWithX := min{J1 , J2 } 1 2 max{J1 , J2 } 5 if largerWithX ∧ J2 J1 6 MERGESORT(A[i + 1..r]) using buffer A[l..i − 1] Mergesort QuickMergesort 7 QUICKMERGESORT(A[l..i − 1]). 8 else 9 MERGESORT(A[l..i − 1]) using buffer A[i + 1..r] Mergesort QuickMergesort 10 QUICKMERGESORT(A[i..r]). 11 end if Sebastian Wild Quicksorts of the 21st century 2021-06-07 20 / 17
  219. QuickMergesort QUICKMERGESORT(A[l..r]) 1 Choose pivot P. 2 i = PARTITION(A[l..r],

    P) 3 J1 := i − l; J2 := r − i 4 largerWithX := min{J1 , J2 } 1 2 max{J1 , J2 } 5 if largerWithX ∧ J2 J1 ∨ ¬largerWithX ∧ J2 < J1 6 MERGESORT(A[i + 1..r]) using buffer A[l..i − 1] Mergesort QuickMergesort 7 QUICKMERGESORT(A[l..i − 1]). 8 else 9 MERGESORT(A[l..i − 1]) using buffer A[i + 1..r] Mergesort QuickMergesort 10 QUICKMERGESORT(A[i..r]). 11 end if Sebastian Wild Quicksorts of the 21st century 2021-06-07 20 / 17
  220. What happened? – Details What do comparisons, write accesses, Bytecodes

    and branch misses have in common? they happen inside the CPU Sebastian Wild Quicksorts of the 21st century 2021-06-07 21 / 17
  221. What happened? – Details What do comparisons, write accesses, Bytecodes

    and branch misses have in common? they happen inside the CPU Sebastian Wild Quicksorts of the 21st century 2021-06-07 21 / 17
  222. What happened? – Details What do comparisons, write accesses, Bytecodes

    and branch misses have in common? they happen inside the CPU ...but we need to get the data there, as well! Sebastian Wild Quicksorts of the 21st century 2021-06-07 21 / 17
  223. What happened? – Details What do comparisons, write accesses, Bytecodes

    and branch misses have in common? they happen inside the CPU ...but we need to get the data there, as well! 1991 1995 2000 2005 2010 2015 100 102 104 106 CPU speed (MFLOPS) STREAM Benchmark data with linear regression www.cs.virginia.edu/stream/by_date/Balance.html Sebastian Wild Quicksorts of the 21st century 2021-06-07 21 / 17
  224. What happened? – Details What do comparisons, write accesses, Bytecodes

    and branch misses have in common? they happen inside the CPU ...but we need to get the data there, as well! 1991 1995 2000 2005 2010 2015 100 102 104 106 CPU speed (MFLOPS) STREAM Benchmark data with linear regression www.cs.virginia.edu/stream/by_date/Balance.html Sebastian Wild Quicksorts of the 21st century 2021-06-07 21 / 17
  225. What happened? – Details What do comparisons, write accesses, Bytecodes

    and branch misses have in common? they happen inside the CPU ...but we need to get the data there, as well! 1991 1995 2000 2005 2010 2015 100 102 104 106 CPU speed (MFLOPS) STREAM Benchmark data with linear regression www.cs.virginia.edu/stream/by_date/Balance.html Average annual growth rate: CPU speed: 46% Sebastian Wild Quicksorts of the 21st century 2021-06-07 21 / 17
  226. What happened? – Details What do comparisons, write accesses, Bytecodes

    and branch misses have in common? they happen inside the CPU ...but we need to get the data there, as well! 1991 1995 2000 2005 2010 2015 100 102 104 106 CPU speed (MFLOPS) memory bandwidth (MWords/s) STREAM Benchmark data with linear regression www.cs.virginia.edu/stream/by_date/Balance.html Average annual growth rate: CPU speed: 46% Sebastian Wild Quicksorts of the 21st century 2021-06-07 21 / 17
  227. What happened? – Details What do comparisons, write accesses, Bytecodes

    and branch misses have in common? they happen inside the CPU ...but we need to get the data there, as well! 1991 1995 2000 2005 2010 2015 100 102 104 106 CPU speed (MFLOPS) memory bandwidth (MWords/s) STREAM Benchmark data with linear regression www.cs.virginia.edu/stream/by_date/Balance.html Average annual growth rate: CPU speed: 46% Memory Bandwidth: 37% Sebastian Wild Quicksorts of the 21st century 2021-06-07 21 / 17
  228. What happened? – Details What do comparisons, write accesses, Bytecodes

    and branch misses have in common? they happen inside the CPU ...but we need to get the data there, as well! 1991 1995 2000 2005 2010 2015 100 102 104 106 CPU speed (MFLOPS) memory bandwidth (MWords/s) STREAM Benchmark data with linear regression www.cs.virginia.edu/stream/by_date/Balance.html Average annual growth rate: CPU speed: 46% Memory Bandwidth: 37% Sebastian Wild Quicksorts of the 21st century 2021-06-07 21 / 17
  229. What happened? – Details What do comparisons, write accesses, Bytecodes

    and branch misses have in common? they happen inside the CPU ...but we need to get the data there, as well! 1991 1995 2000 2005 2010 2015 100 102 104 106 growing gap CPU speed (MFLOPS) memory bandwidth (MWords/s) STREAM Benchmark data with linear regression www.cs.virginia.edu/stream/by_date/Balance.html Average annual growth rate: CPU speed: 46% Memory Bandwidth: 37% Sebastian Wild Quicksorts of the 21st century 2021-06-07 21 / 17
  230. What happened? – Details What do comparisons, write accesses, Bytecodes

    and branch misses have in common? they happen inside the CPU ...but we need to get the data there, as well! 1991 1995 2000 2005 2010 2015 100 102 104 106 growing gap CPU speed (MFLOPS) memory bandwidth (MWords/s) “Imbalance”: CPU speed bandwidth STREAM Benchmark data with linear regression www.cs.virginia.edu/stream/by_date/Balance.html Average annual growth rate: CPU speed: 46% Memory Bandwidth: 37% Sebastian Wild Quicksorts of the 21st century 2021-06-07 21 / 17
  231. What happened? – Details What do comparisons, write accesses, Bytecodes

    and branch misses have in common? they happen inside the CPU ...but we need to get the data there, as well! 1991 1995 2000 2005 2010 2015 100 101 102 CPU speed (MFLOPS) memory bandwidth (MWords/s) “Imbalance”: CPU speed bandwidth STREAM Benchmark data with linear regression www.cs.virginia.edu/stream/by_date/Balance.html Average annual growth rate: CPU speed: 46% Memory Bandwidth: 37% Imbalance: 5% Sebastian Wild Quicksorts of the 21st century 2021-06-07 21 / 17
  232. What happened? – Details What do comparisons, write accesses, Bytecodes

    and branch misses have in common? they happen inside the CPU ...but we need to get the data there, as well! 1991 1995 2000 2005 2010 2015 100 101 102 CPU speed (MFLOPS) memory bandwidth (MWords/s) “Imbalance”: CPU speed bandwidth STREAM Benchmark data with linear regression www.cs.virginia.edu/stream/by_date/Balance.html Average annual growth rate: CPU speed: 46% Memory Bandwidth: 37% Imbalance: 5% Sebastian Wild Quicksorts of the 21st century 2021-06-07 21 / 17
  233. What happened? – Details What do comparisons, write accesses, Bytecodes

    and branch misses have in common? they happen inside the CPU ...but we need to get the data there, as well! 1991 1995 2000 2005 2010 2015 100 101 102 CPU speed (MFLOPS) memory bandwidth (MWords/s) “Imbalance”: CPU speed bandwidth STREAM Benchmark data with linear regression www.cs.virginia.edu/stream/by_date/Balance.html Average annual growth rate: CPU speed: 46% Memory Bandwidth: 37% Imbalance: 5% Classic Quicksort (Bentley & McIlroy) 1993 1 mem transfer ≈ 7 instructions focus on CPU costs Sebastian Wild Quicksorts of the 21st century 2021-06-07 21 / 17
  234. What happened? – Details What do comparisons, write accesses, Bytecodes

    and branch misses have in common? they happen inside the CPU ...but we need to get the data there, as well! 1991 1995 2000 2005 2010 2015 100 101 102 CPU speed (MFLOPS) memory bandwidth (MWords/s) “Imbalance”: CPU speed bandwidth STREAM Benchmark data with linear regression www.cs.virginia.edu/stream/by_date/Balance.html Average annual growth rate: CPU speed: 46% Memory Bandwidth: 37% Imbalance: 5% Classic Quicksort (Bentley & McIlroy) 1993 1 mem transfer ≈ 7 instructions focus on CPU costs Sebastian Wild Quicksorts of the 21st century 2021-06-07 21 / 17
  235. What happened? – Details What do comparisons, write accesses, Bytecodes

    and branch misses have in common? they happen inside the CPU ...but we need to get the data there, as well! 1991 1995 2000 2005 2010 2015 100 101 102 CPU speed (MFLOPS) memory bandwidth (MWords/s) “Imbalance”: CPU speed bandwidth STREAM Benchmark data with linear regression www.cs.virginia.edu/stream/by_date/Balance.html Average annual growth rate: CPU speed: 46% Memory Bandwidth: 37% Imbalance: 5% Classic Quicksort (Bentley & McIlroy) 1993 1 mem transfer ≈ 7 instructions focus on CPU costs Sebastian Wild Quicksorts of the 21st century 2021-06-07 21 / 17
  236. What happened? – Details What do comparisons, write accesses, Bytecodes

    and branch misses have in common? they happen inside the CPU ...but we need to get the data there, as well! 1991 1995 2000 2005 2010 2015 100 101 102 CPU speed (MFLOPS) memory bandwidth (MWords/s) “Imbalance”: CPU speed bandwidth STREAM Benchmark data with linear regression www.cs.virginia.edu/stream/by_date/Balance.html Average annual growth rate: CPU speed: 46% Memory Bandwidth: 37% Imbalance: 5% Classic Quicksort (Bentley & McIlroy) 1993 1 mem transfer ≈ 7 instructions focus on CPU costs YBB Quicksort, 2009 – today 1 transfer ≈ 30 instructions focus on CPU and bandwidth Sebastian Wild Quicksorts of the 21st century 2021-06-07 21 / 17
  237. What happened? – Details What do comparisons, write accesses, Bytecodes

    and branch misses have in common? they happen inside the CPU ...but we need to get the data there, as well! 1991 1995 2000 2005 2010 2015 100 101 102 CPU speed (MFLOPS) memory bandwidth (MWords/s) “Imbalance”: CPU speed bandwidth STREAM Benchmark data with linear regression www.cs.virginia.edu/stream/by_date/Balance.html Average annual growth rate: CPU speed: 46% Memory Bandwidth: 37% Imbalance: 5% Classic Quicksort (Bentley & McIlroy) 1993 1 mem transfer ≈ 7 instructions focus on CPU costs YBB Quicksort, 2009 – today 1 transfer ≈ 30 instructions focus on CPU and bandwidth Sebastian Wild Quicksorts of the 21st century 2021-06-07 21 / 17
  238. What happened? – Details What do comparisons, write accesses, Bytecodes

    and branch misses have in common? they happen inside the CPU ...but we need to get the data there, as well! 1991 1995 2000 2005 2010 2015 100 101 102 CPU speed (MFLOPS) memory bandwidth (MWords/s) “Imbalance”: CPU speed bandwidth STREAM Benchmark data with linear regression www.cs.virginia.edu/stream/by_date/Balance.html Average annual growth rate: CPU speed: 46% Memory Bandwidth: 37% Imbalance: 5% Classic Quicksort (Bentley & McIlroy) 1993 1 mem transfer ≈ 7 instructions focus on CPU costs YBB Quicksort, 2009 – today 1 transfer ≈ 30 instructions focus on CPU and bandwidth Sebastian Wild Quicksorts of the 21st century 2021-06-07 21 / 17
  239. Icons made by Freepik, Gregor Cresnar, Those Icons, Smashicons, Good

    Ware, Pause08, and Madebyoliver from www.flaticon.com. Vector graphics from Pressfoto, brgfx, macrovector and Jannoon028 on freepik.com Top Trumps Card from winningmoves.co.uk. 3-way railway switch by Asiano processor by Konstantin Lanzet RAM module by Utente:Sassospicco Other photos from www.pixabay.com. Sebastian Wild Quicksorts of the 21st century 2021-06-07 22 / 17