why is dual pivot quicksort fast
play

Why is Dual-Pivot Quicksort Fast? Sebastian Wild wild@cs.uni-kl.de - PowerPoint PPT Presentation

Why is Dual-Pivot Quicksort Fast? Sebastian Wild wild@cs.uni-kl.de 29 September 2015 Theorietage 2015 Speyer Sebastian Wild Dual-Pivot Quicksort 2015-03-24 1 / 11 Sorting History Invention of Quicksort Dual-Pivot Quicksort in Java Age of


  1. Why is Dual-Pivot Quicksort Fast? Sebastian Wild wild@cs.uni-kl.de 29 September 2015 Theorietage 2015 Speyer Sebastian Wild Dual-Pivot Quicksort 2015-03-24 1 / 11

  2. Sorting History Invention of Quicksort Dual-Pivot Quicksort in Java Age of classic Quicksort Ancient World Dual-Pivot Era 2009 1961 1969 1975 ’78 1993 1997 today ’62 ’77 ’11 Sebastian Wild Dual-Pivot Quicksort 2015-03-24 2 / 11

  3. Sorting History 1961,62 Hoare: publication, first analysis 1969 Singleton: median-of-three & Insertionsort on small subarrays 1975-78 Sedgewick: analysis of many optimizations 1993 Bentley, McIlroy: duplicate elements & “ninther” 1997 Musser: O ( n log n ) worst case by truncating recursion � Basic algorithm settled since 1961; latest tweaks from 1990’s. Since then: Almost identical in all programming libraries! Invention of Quicksort Dual-Pivot Quicksort in Java Age of classic Quicksort Ancient World Dual-Pivot Era 2009 1961 1969 1975 ’78 1993 1997 today ’62 ’77 ’11 Sebastian Wild Dual-Pivot Quicksort 2015-03-24 2 / 11

  4. Sorting History 1961,62 Hoare: publication, first analysis 1969 Singleton: median-of-three & Insertionsort on small subarrays 1975-78 Sedgewick: analysis of many optimizations 1993 Bentley, McIlroy: duplicate elements & “ninther” 1997 Musser: O ( n log n ) worst case by truncating recursion � Basic algorithm settled since 1961; latest tweaks from 1990’s. Since then: Almost identical in all programming libraries! Invention of Quicksort Dual-Pivot Quicksort in Java Age of classic Quicksort Ancient World Dual-Pivot Era 2009 1961 1969 1975 ’78 1993 1997 today ’62 ’77 ’11 Sebastian Wild Dual-Pivot Quicksort 2015-03-24 2 / 11

  5. Sorting History 2008 – 2009 Vladimir Yaroslavskiy (developer at Sun) experiments with Quicksort with two pivots 11 Sep 2009 announcement on Java core library mailing list 29 Oct 2009 inclusion in development version of OpenJDK 2009 – 2011 optimizations by Joshua Bloch, Jon Bentley and others 28 July 2011 public release of Java 7 with Yaroslavskiy’s Quicksort Invention of Quicksort Dual-Pivot Quicksort in Java Age of classic Quicksort Ancient World Dual-Pivot Era 2009 1961 1969 1975 ’78 1993 1997 today ’62 ’77 ’11 Sebastian Wild Dual-Pivot Quicksort 2015-03-24 2 / 11

  6. Dual-Pivot Quicksort Algorithm (Conceptual View) Choose two pivots P � Q 1 For each element x , determine its class 2 small for x < P medium for P < x < Q large for Q < x by comparing x to pivots P and Q Arrange elements according to classes: 3 Q P Sort subarrays recursively. 4 3 efficiently on arrays? How to implement Sebastian Wild Dual-Pivot Quicksort 2015-03-24 3 / 11

  7. Dual-Pivot Quicksort Algorithm (Conceptual View) Choose two pivots P � Q 1 For each element x , determine its class 2 small for x < P medium for P < x < Q large for Q < x by comparing x to pivots P and Q Arrange elements according to classes: 3 Q P Sort subarrays recursively. 4 3 efficiently on arrays? How to implement Sebastian Wild Dual-Pivot Quicksort 2015-03-24 3 / 11

  8. Dual-Pivot Quicksort Algorithm (Conceptual View) Choose two pivots P � Q 1 For each element x , determine its class 2 small for x < P medium for P < x < Q large for Q < x by comparing x to pivots P and Q Arrange elements according to classes: 3 Q P Sort subarrays recursively. 4 3 efficiently on arrays? How to implement Sebastian Wild Dual-Pivot Quicksort 2015-03-24 3 / 11

  9. Dual-Pivot Quicksort Algorithm (Conceptual View) Choose two pivots P � Q 1 For each element x , determine its class 2 small for x < P medium for P < x < Q large for Q < x by comparing x to pivots P and Q Arrange elements according to classes: 3 Q P Sort subarrays recursively. 4 3 efficiently on arrays? How to implement Sebastian Wild Dual-Pivot Quicksort 2015-03-24 3 / 11

  10. Yaroslavskiy’s Algorithm > Q ? < P ? ✗ ✗ ✓ ✓ swap ℓ < Q ? skip < P ? ✗ ✗ ✓ ✓ skip swap g swap ℓ swap k ℓ k g Q P 3 5 1 7 4 2 8 6 Q Invariant: P Sebastian Wild Dual-Pivot Quicksort 2015-03-24 4 / 11

  11. Yaroslavskiy’s Algorithm > Q ? < P ? ✗ ✗ ✓ ✓ swap ℓ < Q ? skip < P ? ✗ ✗ ✓ ✓ skip swap g swap ℓ swap k ℓ k g Q P 3 5 1 7 4 2 8 6 Q Invariant: P Sebastian Wild Dual-Pivot Quicksort 2015-03-24 4 / 11

  12. Yaroslavskiy’s Algorithm > Q ? < P ? ✗ ✗ ✓ ✓ swap ℓ < Q ? skip < P ? ✗ ✗ ✓ ✓ skip swap g swap ℓ swap k ℓ k g Q P 3 5 1 7 4 2 8 6 Q Invariant: P Sebastian Wild Dual-Pivot Quicksort 2015-03-24 4 / 11

  13. Yaroslavskiy’s Algorithm > Q ? < P ? ✗ ✗ ✓ ✓ swap ℓ < Q ? skip < P ? ✗ ✗ ✓ ✓ skip swap g swap ℓ swap k ℓ k g Q P 3 5 1 7 4 2 8 6 Q Invariant: P Sebastian Wild Dual-Pivot Quicksort 2015-03-24 4 / 11

  14. Yaroslavskiy’s Algorithm > Q ? < P ? ✗ ✗ ✓ ✓ swap ℓ < Q ? skip < P ? ✗ ✗ ✓ ✓ skip swap g swap ℓ swap k ℓ k g Q P 3 5 1 7 4 2 8 6 Q Invariant: P P ≤◦≤ Q Sebastian Wild Dual-Pivot Quicksort 2015-03-24 4 / 11

  15. Yaroslavskiy’s Algorithm > Q ? < P ? ✗ ✗ ✓ ✓ swap ℓ < Q ? skip < P ? ✗ ✗ ✓ ✓ skip swap g swap ℓ swap k ℓ k g Q P 3 5 1 7 4 2 8 6 Q Invariant: P P ≤◦≤ Q Sebastian Wild Dual-Pivot Quicksort 2015-03-24 4 / 11

  16. Yaroslavskiy’s Algorithm > Q ? < P ? ✗ ✗ ✓ ✓ swap ℓ < Q ? skip < P ? ✗ ✗ ✓ ✓ skip swap g swap ℓ swap k ℓ k g Q P 3 1 5 7 4 2 8 6 Q Invariant: P P ≤◦≤ Q Sebastian Wild Dual-Pivot Quicksort 2015-03-24 4 / 11

  17. Yaroslavskiy’s Algorithm > Q ? < P ? ✗ ✗ ✓ ✓ swap ℓ < Q ? skip < P ? ✗ ✗ ✓ ✓ skip swap g swap ℓ swap k ℓ k g Q P 3 1 5 7 4 2 8 6 Q Invariant: P < P P ≤◦≤ Q Sebastian Wild Dual-Pivot Quicksort 2015-03-24 4 / 11

  18. Yaroslavskiy’s Algorithm > Q ? < P ? ✗ ✗ ✓ ✓ swap ℓ < Q ? skip < P ? ✗ ✗ ✓ ✓ skip swap g swap ℓ swap k ℓ k g Q P 3 1 5 7 4 2 8 6 Q Invariant: P < P P ≤◦≤ Q Sebastian Wild Dual-Pivot Quicksort 2015-03-24 4 / 11

  19. Yaroslavskiy’s Algorithm > Q ? < P ? ✗ ✗ ✓ ✓ swap ℓ < Q ? skip < P ? ✗ ✗ ✓ ✓ skip swap g swap ℓ swap k ℓ k g Q P 3 1 5 7 4 2 8 6 Q Invariant: P < P P ≤◦≤ Q Sebastian Wild Dual-Pivot Quicksort 2015-03-24 4 / 11

  20. Yaroslavskiy’s Algorithm > Q ? < P ? ✗ ✗ ✓ ✓ swap ℓ < Q ? skip < P ? ✗ ✗ ✓ ✓ skip swap g swap ℓ swap k ℓ k g Q P 3 1 5 7 4 2 8 6 Q Invariant: P < P P ≤◦≤ Q Sebastian Wild Dual-Pivot Quicksort 2015-03-24 4 / 11

  21. Yaroslavskiy’s Algorithm > Q ? < P ? ✗ ✗ ✓ ✓ swap ℓ < Q ? skip < P ? ✗ ✗ ✓ ✓ skip swap g swap ℓ swap k ℓ k g Q P 3 1 5 7 4 2 8 6 Q Invariant: P < P P ≤◦≤ Q Sebastian Wild Dual-Pivot Quicksort 2015-03-24 4 / 11

  22. Yaroslavskiy’s Algorithm > Q ? < P ? ✗ ✗ ✓ ✓ swap ℓ < Q ? skip < P ? ✗ ✗ ✓ ✓ skip swap g swap ℓ swap k ℓ k g Q P 3 1 5 7 4 2 8 6 ≥ Q Q Invariant: P < P P ≤◦≤ Q Sebastian Wild Dual-Pivot Quicksort 2015-03-24 4 / 11

  23. Yaroslavskiy’s Algorithm > Q ? < P ? ✗ ✗ ✓ ✓ swap ℓ < Q ? skip < P ? ✗ ✗ ✓ ✓ skip swap g swap ℓ swap k ℓ k g Q P 3 1 5 7 4 2 8 6 ≥ Q Q Invariant: P < P P ≤◦≤ Q Sebastian Wild Dual-Pivot Quicksort 2015-03-24 4 / 11

  24. Yaroslavskiy’s Algorithm > Q ? < P ? ✗ ✗ ✓ ✓ swap ℓ < Q ? skip < P ? ✗ ✗ ✓ ✓ skip swap g swap ℓ swap k ℓ k g Q P 3 1 5 7 4 2 8 6 ≥ Q Q Invariant: P < P P ≤◦≤ Q Sebastian Wild Dual-Pivot Quicksort 2015-03-24 4 / 11

  25. Yaroslavskiy’s Algorithm > Q ? < P ? ✗ ✗ ✓ ✓ swap ℓ < Q ? skip < P ? ✗ ✗ ✓ ✓ skip swap g swap ℓ swap k ℓ k g Q P 3 1 2 5 4 7 8 6 ≥ Q Q Invariant: P < P P ≤◦≤ Q Sebastian Wild Dual-Pivot Quicksort 2015-03-24 4 / 11

  26. Yaroslavskiy’s Algorithm > Q ? < P ? ✗ ✗ ✓ ✓ swap ℓ < Q ? skip < P ? ✗ ✗ ✓ ✓ skip swap g swap ℓ swap k g ℓ k Q P 3 1 2 5 4 7 8 6 ≥ Q Q Invariant: P < P P ≤◦≤ Q Sebastian Wild Dual-Pivot Quicksort 2015-03-24 4 / 11

  27. Yaroslavskiy’s Algorithm > Q ? < P ? ✗ ✗ ✓ ✓ swap ℓ < Q ? skip < P ? ✗ ✗ ✓ ✓ skip swap g swap ℓ swap k g ℓ k Q P 3 1 2 5 4 7 8 6 ≥ Q Q Invariant: P < P P ≤◦≤ Q Sebastian Wild Dual-Pivot Quicksort 2015-03-24 4 / 11

  28. Yaroslavskiy’s Algorithm > Q ? < P ? ✗ ✗ ✓ ✓ swap ℓ < Q ? skip < P ? ✗ ✗ ✓ ✓ skip swap g swap ℓ swap k g ℓ k Q P 3 1 2 5 4 7 8 6 ≥ Q Q Invariant: P < P P ≤◦≤ Q Sebastian Wild Dual-Pivot Quicksort 2015-03-24 4 / 11

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend