Mergesort can make use of existing order in the input by picking up existing runs, i.e., sorted segments. Since the lengths of these runs can be arbitrary, simply merging them as they arrive can be wasteful—merging can degenerate to inserting a single elements into long run.
In this talk, I show that we can find an optimal merging order (up to lower order terms of costs) with negligible overhead and thereby get the same worst-case guarantee as for standard mergesort (up to lower order terms), while exploiting existing runs if present. I present two new mergesort variants, peeksort and powersort, that are simple, stable, optimally adaptive and fast in practice (never slower than standard mergesort and Timsort, but significantly faster on certain inputs).
This talk was given at ESA 2018 and is based on joint work with Ian Munro. ItThe paper and further information can be found on my website:
https://www.wild-inter.net/publications/munro-wild-2018
Succint Data Structures for Range Minimum ProblemsSebastian Wild
This was an invited talk I gave at Purdue University. It introduces some concepts and techniques of succinct data structures along the example of the range-minimum query problem, and presents my new, average-case space optimal solution.
Entropy Trees & Range-Minimum Queries in Optimal Average-Case SpaceSebastian Wild
I gave this talk at the Dagstuhl Seminar 19051
on Data Structures for the Cloud and External Memory Data
(https://www.dagstuhl.de/en/program/calendar/semhp/?semnr=19051)
Sesquickselect: One and a half pivot for cache efficient selectionSebastian Wild
These are the slides for my ANALCO about sesquickselect, a novel quickselect variant. The paper and further details here: https://www.wild-inter.net/publications/martinez-nebel-wild-2019
Average cost of QuickXsort with pivot samplingSebastian Wild
The document discusses QuickXsort, a variant of Quicksort that uses a sorting algorithm X to sort one subproblem during recursion. QuickXsort is described as using Mergesort for X to achieve near-optimal comparison counts while sorting in-place. Merging in Mergesort is explained as possible through swapping elements between runs and a buffer to merge runs together without using extra space.
The document describes the process of quicksort and building a binary search tree on the same data. It shows quicksort sorting an array from 7 4 2 9 1 3 8 5 6 to its sorted order, and building a corresponding binary search tree from the sorted array. It notes that the recursion tree of quicksort is equivalent to the built binary search tree, and that the number of comparisons in quicksort equals building and searching the tree. It questions how median-of-three quicksort and other variants relate to building fringe-balanced search trees.
Succint Data Structures for Range Minimum ProblemsSebastian Wild
This was an invited talk I gave at Purdue University. It introduces some concepts and techniques of succinct data structures along the example of the range-minimum query problem, and presents my new, average-case space optimal solution.
Entropy Trees & Range-Minimum Queries in Optimal Average-Case SpaceSebastian Wild
I gave this talk at the Dagstuhl Seminar 19051
on Data Structures for the Cloud and External Memory Data
(https://www.dagstuhl.de/en/program/calendar/semhp/?semnr=19051)
Sesquickselect: One and a half pivot for cache efficient selectionSebastian Wild
These are the slides for my ANALCO about sesquickselect, a novel quickselect variant. The paper and further details here: https://www.wild-inter.net/publications/martinez-nebel-wild-2019
Average cost of QuickXsort with pivot samplingSebastian Wild
The document discusses QuickXsort, a variant of Quicksort that uses a sorting algorithm X to sort one subproblem during recursion. QuickXsort is described as using Mergesort for X to achieve near-optimal comparison counts while sorting in-place. Merging in Mergesort is explained as possible through swapping elements between runs and a buffer to merge runs together without using extra space.
The document describes the process of quicksort and building a binary search tree on the same data. It shows quicksort sorting an array from 7 4 2 9 1 3 8 5 6 to its sorted order, and building a corresponding binary search tree from the sorted array. It notes that the recursion tree of quicksort is equivalent to the built binary search tree, and that the number of comparisons in quicksort equals building and searching the tree. It questions how median-of-three quicksort and other variants relate to building fringe-balanced search trees.
The document describes dual-pivot quicksort, which uses two pivot elements rather than one. It summarizes previous research that found dual-pivot quicksort often improves upon classic quicksort by reducing the number of element comparisons and cache misses, though it increases the number of swaps. The document then focuses on Yaroslavskiy's dual-pivot partitioning algorithm, which efficiently arranges elements into three groups - less than the first pivot, between the pivots, and greater than the second pivot - through in-place swapping.
These are the slides of my talk at the Meeting on Analytic Algorithmics and Combinatorics 2015 (ANALCO15) on branch mispredictions in classic Quicksort and Yaroslavskiy's dual-pivot Quicksort used in Java 7.
The talk is based on joint work with Conrado Martínez and Markus E. Nebel.
Find more information and the corresponding paper on my website: http://wwwagak.cs.uni-kl.de/sebastian-wild.html
Quickselect Under Yaroslavskiy's Dual Pivoting AlgorithmSebastian Wild
I gave this talk at the 24th International Meeting on Probabilistic, Combinatorial and Asymptotic Methods for the Analysis of Algorithms (AofA 2013) on Menorca (Spain).
A paper covering the analyses of this talk (and some more!) has been submitted.
Also, in the talk, I refer to the previous speaker at the conference, my advisor Markus Nebel - corresponding results can be found in an earlier talk of mine:
slideshare.net/sebawild/average-case-analysis-of-java-7s-dual-pivot-quicksort
Check my website for preprints of papers and my other talks:
wwwagak.cs.uni-kl.de/sebastian-wild.html
Engineering Java 7's Dual Pivot Quicksort Using MaLiJAnSebastian Wild
I gave this talk at the 2013 Meeting On Algorithm Engineering and Experiments (ALENEX) meeting.
Find my other talks and the corresponding papers on my web page:
http://wwwagak.cs.uni-kl.de/sebastian-wild.html
Average Case Analysis of Java 7’s Dual Pivot QuicksortSebastian Wild
I gave this talk at the European Symposium on Algorithms 2012 in Ljubljana (Slowenia).
The corresponding paper won the best paper award.
Find my other talks and all corresponding papers on my web page:
http://wwwagak.cs.uni-kl.de/sebastian-wild.html
This presentation offers a general idea of the structure of seed, seed production, management of seeds and its allied technologies. It also offers the concept of gene erosion and the practices used to control it. Nursery and gardening have been widely explored along with their importance in the related domain.
Candidate young stellar objects in the S-cluster: Kinematic analysis of a sub...Sérgio Sacani
Context. The observation of several L-band emission sources in the S cluster has led to a rich discussion of their nature. However, a definitive answer to the classification of the dusty objects requires an explanation for the detection of compact Doppler-shifted Brγ emission. The ionized hydrogen in combination with the observation of mid-infrared L-band continuum emission suggests that most of these sources are embedded in a dusty envelope. These embedded sources are part of the S-cluster, and their relationship to the S-stars is still under debate. To date, the question of the origin of these two populations has been vague, although all explanations favor migration processes for the individual cluster members. Aims. This work revisits the S-cluster and its dusty members orbiting the supermassive black hole SgrA* on bound Keplerian orbits from a kinematic perspective. The aim is to explore the Keplerian parameters for patterns that might imply a nonrandom distribution of the sample. Additionally, various analytical aspects are considered to address the nature of the dusty sources. Methods. Based on the photometric analysis, we estimated the individual H−K and K−L colors for the source sample and compared the results to known cluster members. The classification revealed a noticeable contrast between the S-stars and the dusty sources. To fit the flux-density distribution, we utilized the radiative transfer code HYPERION and implemented a young stellar object Class I model. We obtained the position angle from the Keplerian fit results; additionally, we analyzed the distribution of the inclinations and the longitudes of the ascending node. Results. The colors of the dusty sources suggest a stellar nature consistent with the spectral energy distribution in the near and midinfrared domains. Furthermore, the evaporation timescales of dusty and gaseous clumps in the vicinity of SgrA* are much shorter ( 2yr) than the epochs covered by the observations (≈15yr). In addition to the strong evidence for the stellar classification of the D-sources, we also find a clear disk-like pattern following the arrangements of S-stars proposed in the literature. Furthermore, we find a global intrinsic inclination for all dusty sources of 60 ± 20◦, implying a common formation process. Conclusions. The pattern of the dusty sources manifested in the distribution of the position angles, inclinations, and longitudes of the ascending node strongly suggests two different scenarios: the main-sequence stars and the dusty stellar S-cluster sources share a common formation history or migrated with a similar formation channel in the vicinity of SgrA*. Alternatively, the gravitational influence of SgrA* in combination with a massive perturber, such as a putative intermediate mass black hole in the IRS 13 cluster, forces the dusty objects and S-stars to follow a particular orbital arrangement. Key words. stars: black holes– stars: formation– Galaxy: center– galaxies: star formation
Compositions of iron-meteorite parent bodies constrainthe structure of the pr...Sérgio Sacani
Magmatic iron-meteorite parent bodies are the earliest planetesimals in the Solar System,and they preserve information about conditions and planet-forming processes in thesolar nebula. In this study, we include comprehensive elemental compositions andfractional-crystallization modeling for iron meteorites from the cores of five differenti-ated asteroids from the inner Solar System. Together with previous results of metalliccores from the outer Solar System, we conclude that asteroidal cores from the outerSolar System have smaller sizes, elevated siderophile-element abundances, and simplercrystallization processes than those from the inner Solar System. These differences arerelated to the formation locations of the parent asteroids because the solar protoplane-tary disk varied in redox conditions, elemental distributions, and dynamics at differentheliocentric distances. Using highly siderophile-element data from iron meteorites, wereconstruct the distribution of calcium-aluminum-rich inclusions (CAIs) across theprotoplanetary disk within the first million years of Solar-System history. CAIs, the firstsolids to condense in the Solar System, formed close to the Sun. They were, however,concentrated within the outer disk and depleted within the inner disk. Future modelsof the structure and evolution of the protoplanetary disk should account for this dis-tribution pattern of CAIs.
Embracing Deep Variability For Reproducibility and Replicability
Abstract: Reproducibility (aka determinism in some cases) constitutes a fundamental aspect in various fields of computer science, such as floating-point computations in numerical analysis and simulation, concurrency models in parallelism, reproducible builds for third parties integration and packaging, and containerization for execution environments. These concepts, while pervasive across diverse concerns, often exhibit intricate inter-dependencies, making it challenging to achieve a comprehensive understanding. In this short and vision paper we delve into the application of software engineering techniques, specifically variability management, to systematically identify and explicit points of variability that may give rise to reproducibility issues (eg language, libraries, compiler, virtual machine, OS, environment variables, etc). The primary objectives are: i) gaining insights into the variability layers and their possible interactions, ii) capturing and documenting configurations for the sake of reproducibility, and iii) exploring diverse configurations to replicate, and hence validate and ensure the robustness of results. By adopting these methodologies, we aim to address the complexities associated with reproducibility and replicability in modern software systems and environments, facilitating a more comprehensive and nuanced perspective on these critical aspects.
https://hal.science/hal-04582287
TOPIC OF DISCUSSION: CENTRIFUGATION SLIDESHARE.pptxshubhijain836
Centrifugation is a powerful technique used in laboratories to separate components of a heterogeneous mixture based on their density. This process utilizes centrifugal force to rapidly spin samples, causing denser particles to migrate outward more quickly than lighter ones. As a result, distinct layers form within the sample tube, allowing for easy isolation and purification of target substances.
Mechanisms and Applications of Antiviral Neutralizing Antibodies - Creative B...Creative-Biolabs
Neutralizing antibodies, pivotal in immune defense, specifically bind and inhibit viral pathogens, thereby playing a crucial role in protecting against and mitigating infectious diseases. In this slide, we will introduce what antibodies and neutralizing antibodies are, the production and regulation of neutralizing antibodies, their mechanisms of action, classification and applications, as well as the challenges they face.
BIRDS DIVERSITY OF SOOTEA BISWANATH ASSAM.ppt.pptxgoluk9330
Ahota Beel, nestled in Sootea Biswanath Assam , is celebrated for its extraordinary diversity of bird species. This wetland sanctuary supports a myriad of avian residents and migrants alike. Visitors can admire the elegant flights of migratory species such as the Northern Pintail and Eurasian Wigeon, alongside resident birds including the Asian Openbill and Pheasant-tailed Jacana. With its tranquil scenery and varied habitats, Ahota Beel offers a perfect haven for birdwatchers to appreciate and study the vibrant birdlife that thrives in this natural refuge.
The document describes dual-pivot quicksort, which uses two pivot elements rather than one. It summarizes previous research that found dual-pivot quicksort often improves upon classic quicksort by reducing the number of element comparisons and cache misses, though it increases the number of swaps. The document then focuses on Yaroslavskiy's dual-pivot partitioning algorithm, which efficiently arranges elements into three groups - less than the first pivot, between the pivots, and greater than the second pivot - through in-place swapping.
These are the slides of my talk at the Meeting on Analytic Algorithmics and Combinatorics 2015 (ANALCO15) on branch mispredictions in classic Quicksort and Yaroslavskiy's dual-pivot Quicksort used in Java 7.
The talk is based on joint work with Conrado Martínez and Markus E. Nebel.
Find more information and the corresponding paper on my website: http://wwwagak.cs.uni-kl.de/sebastian-wild.html
Quickselect Under Yaroslavskiy's Dual Pivoting AlgorithmSebastian Wild
I gave this talk at the 24th International Meeting on Probabilistic, Combinatorial and Asymptotic Methods for the Analysis of Algorithms (AofA 2013) on Menorca (Spain).
A paper covering the analyses of this talk (and some more!) has been submitted.
Also, in the talk, I refer to the previous speaker at the conference, my advisor Markus Nebel - corresponding results can be found in an earlier talk of mine:
slideshare.net/sebawild/average-case-analysis-of-java-7s-dual-pivot-quicksort
Check my website for preprints of papers and my other talks:
wwwagak.cs.uni-kl.de/sebastian-wild.html
Engineering Java 7's Dual Pivot Quicksort Using MaLiJAnSebastian Wild
I gave this talk at the 2013 Meeting On Algorithm Engineering and Experiments (ALENEX) meeting.
Find my other talks and the corresponding papers on my web page:
http://wwwagak.cs.uni-kl.de/sebastian-wild.html
Average Case Analysis of Java 7’s Dual Pivot QuicksortSebastian Wild
I gave this talk at the European Symposium on Algorithms 2012 in Ljubljana (Slowenia).
The corresponding paper won the best paper award.
Find my other talks and all corresponding papers on my web page:
http://wwwagak.cs.uni-kl.de/sebastian-wild.html
This presentation offers a general idea of the structure of seed, seed production, management of seeds and its allied technologies. It also offers the concept of gene erosion and the practices used to control it. Nursery and gardening have been widely explored along with their importance in the related domain.
Candidate young stellar objects in the S-cluster: Kinematic analysis of a sub...Sérgio Sacani
Context. The observation of several L-band emission sources in the S cluster has led to a rich discussion of their nature. However, a definitive answer to the classification of the dusty objects requires an explanation for the detection of compact Doppler-shifted Brγ emission. The ionized hydrogen in combination with the observation of mid-infrared L-band continuum emission suggests that most of these sources are embedded in a dusty envelope. These embedded sources are part of the S-cluster, and their relationship to the S-stars is still under debate. To date, the question of the origin of these two populations has been vague, although all explanations favor migration processes for the individual cluster members. Aims. This work revisits the S-cluster and its dusty members orbiting the supermassive black hole SgrA* on bound Keplerian orbits from a kinematic perspective. The aim is to explore the Keplerian parameters for patterns that might imply a nonrandom distribution of the sample. Additionally, various analytical aspects are considered to address the nature of the dusty sources. Methods. Based on the photometric analysis, we estimated the individual H−K and K−L colors for the source sample and compared the results to known cluster members. The classification revealed a noticeable contrast between the S-stars and the dusty sources. To fit the flux-density distribution, we utilized the radiative transfer code HYPERION and implemented a young stellar object Class I model. We obtained the position angle from the Keplerian fit results; additionally, we analyzed the distribution of the inclinations and the longitudes of the ascending node. Results. The colors of the dusty sources suggest a stellar nature consistent with the spectral energy distribution in the near and midinfrared domains. Furthermore, the evaporation timescales of dusty and gaseous clumps in the vicinity of SgrA* are much shorter ( 2yr) than the epochs covered by the observations (≈15yr). In addition to the strong evidence for the stellar classification of the D-sources, we also find a clear disk-like pattern following the arrangements of S-stars proposed in the literature. Furthermore, we find a global intrinsic inclination for all dusty sources of 60 ± 20◦, implying a common formation process. Conclusions. The pattern of the dusty sources manifested in the distribution of the position angles, inclinations, and longitudes of the ascending node strongly suggests two different scenarios: the main-sequence stars and the dusty stellar S-cluster sources share a common formation history or migrated with a similar formation channel in the vicinity of SgrA*. Alternatively, the gravitational influence of SgrA* in combination with a massive perturber, such as a putative intermediate mass black hole in the IRS 13 cluster, forces the dusty objects and S-stars to follow a particular orbital arrangement. Key words. stars: black holes– stars: formation– Galaxy: center– galaxies: star formation
Compositions of iron-meteorite parent bodies constrainthe structure of the pr...Sérgio Sacani
Magmatic iron-meteorite parent bodies are the earliest planetesimals in the Solar System,and they preserve information about conditions and planet-forming processes in thesolar nebula. In this study, we include comprehensive elemental compositions andfractional-crystallization modeling for iron meteorites from the cores of five differenti-ated asteroids from the inner Solar System. Together with previous results of metalliccores from the outer Solar System, we conclude that asteroidal cores from the outerSolar System have smaller sizes, elevated siderophile-element abundances, and simplercrystallization processes than those from the inner Solar System. These differences arerelated to the formation locations of the parent asteroids because the solar protoplane-tary disk varied in redox conditions, elemental distributions, and dynamics at differentheliocentric distances. Using highly siderophile-element data from iron meteorites, wereconstruct the distribution of calcium-aluminum-rich inclusions (CAIs) across theprotoplanetary disk within the first million years of Solar-System history. CAIs, the firstsolids to condense in the Solar System, formed close to the Sun. They were, however,concentrated within the outer disk and depleted within the inner disk. Future modelsof the structure and evolution of the protoplanetary disk should account for this dis-tribution pattern of CAIs.
Embracing Deep Variability For Reproducibility and Replicability
Abstract: Reproducibility (aka determinism in some cases) constitutes a fundamental aspect in various fields of computer science, such as floating-point computations in numerical analysis and simulation, concurrency models in parallelism, reproducible builds for third parties integration and packaging, and containerization for execution environments. These concepts, while pervasive across diverse concerns, often exhibit intricate inter-dependencies, making it challenging to achieve a comprehensive understanding. In this short and vision paper we delve into the application of software engineering techniques, specifically variability management, to systematically identify and explicit points of variability that may give rise to reproducibility issues (eg language, libraries, compiler, virtual machine, OS, environment variables, etc). The primary objectives are: i) gaining insights into the variability layers and their possible interactions, ii) capturing and documenting configurations for the sake of reproducibility, and iii) exploring diverse configurations to replicate, and hence validate and ensure the robustness of results. By adopting these methodologies, we aim to address the complexities associated with reproducibility and replicability in modern software systems and environments, facilitating a more comprehensive and nuanced perspective on these critical aspects.
https://hal.science/hal-04582287
TOPIC OF DISCUSSION: CENTRIFUGATION SLIDESHARE.pptxshubhijain836
Centrifugation is a powerful technique used in laboratories to separate components of a heterogeneous mixture based on their density. This process utilizes centrifugal force to rapidly spin samples, causing denser particles to migrate outward more quickly than lighter ones. As a result, distinct layers form within the sample tube, allowing for easy isolation and purification of target substances.
Mechanisms and Applications of Antiviral Neutralizing Antibodies - Creative B...Creative-Biolabs
Neutralizing antibodies, pivotal in immune defense, specifically bind and inhibit viral pathogens, thereby playing a crucial role in protecting against and mitigating infectious diseases. In this slide, we will introduce what antibodies and neutralizing antibodies are, the production and regulation of neutralizing antibodies, their mechanisms of action, classification and applications, as well as the challenges they face.
BIRDS DIVERSITY OF SOOTEA BISWANATH ASSAM.ppt.pptxgoluk9330
Ahota Beel, nestled in Sootea Biswanath Assam , is celebrated for its extraordinary diversity of bird species. This wetland sanctuary supports a myriad of avian residents and migrants alike. Visitors can admire the elegant flights of migratory species such as the Northern Pintail and Eurasian Wigeon, alongside resident birds including the Asian Openbill and Pheasant-tailed Jacana. With its tranquil scenery and varied habitats, Ahota Beel offers a perfect haven for birdwatchers to appreciate and study the vibrant birdlife that thrives in this natural refuge.
SDSS1335+0728: The awakening of a ∼ 106M⊙ black hole⋆Sérgio Sacani
Context. The early-type galaxy SDSS J133519.91+072807.4 (hereafter SDSS1335+0728), which had exhibited no prior optical variations during the preceding two decades, began showing significant nuclear variability in the Zwicky Transient Facility (ZTF) alert stream from December 2019 (as ZTF19acnskyy). This variability behaviour, coupled with the host-galaxy properties, suggests that SDSS1335+0728 hosts a ∼ 106M⊙ black hole (BH) that is currently in the process of ‘turning on’. Aims. We present a multi-wavelength photometric analysis and spectroscopic follow-up performed with the aim of better understanding the origin of the nuclear variations detected in SDSS1335+0728. Methods. We used archival photometry (from WISE, 2MASS, SDSS, GALEX, eROSITA) and spectroscopic data (from SDSS and LAMOST) to study the state of SDSS1335+0728 prior to December 2019, and new observations from Swift, SOAR/Goodman, VLT/X-shooter, and Keck/LRIS taken after its turn-on to characterise its current state. We analysed the variability of SDSS1335+0728 in the X-ray/UV/optical/mid-infrared range, modelled its spectral energy distribution prior to and after December 2019, and studied the evolution of its UV/optical spectra. Results. From our multi-wavelength photometric analysis, we find that: (a) since 2021, the UV flux (from Swift/UVOT observations) is four times brighter than the flux reported by GALEX in 2004; (b) since June 2022, the mid-infrared flux has risen more than two times, and the W1−W2 WISE colour has become redder; and (c) since February 2024, the source has begun showing X-ray emission. From our spectroscopic follow-up, we see that (i) the narrow emission line ratios are now consistent with a more energetic ionising continuum; (ii) broad emission lines are not detected; and (iii) the [OIII] line increased its flux ∼ 3.6 years after the first ZTF alert, which implies a relatively compact narrow-line-emitting region. Conclusions. We conclude that the variations observed in SDSS1335+0728 could be either explained by a ∼ 106M⊙ AGN that is just turning on or by an exotic tidal disruption event (TDE). If the former is true, SDSS1335+0728 is one of the strongest cases of an AGNobserved in the process of activating. If the latter were found to be the case, it would correspond to the longest and faintest TDE ever observed (or another class of still unknown nuclear transient). Future observations of SDSS1335+0728 are crucial to further understand its behaviour. Key words. galaxies: active– accretion, accretion discs– galaxies: individual: SDSS J133519.91+072807.4
Signatures of wave erosion in Titan’s coastsSérgio Sacani
The shorelines of Titan’s hydrocarbon seas trace flooded erosional landforms such as river valleys; however, it isunclear whether coastal erosion has subsequently altered these shorelines. Spacecraft observations and theo-retical models suggest that wind may cause waves to form on Titan’s seas, potentially driving coastal erosion,but the observational evidence of waves is indirect, and the processes affecting shoreline evolution on Titanremain unknown. No widely accepted framework exists for using shoreline morphology to quantitatively dis-cern coastal erosion mechanisms, even on Earth, where the dominant mechanisms are known. We combinelandscape evolution models with measurements of shoreline shape on Earth to characterize how differentcoastal erosion mechanisms affect shoreline morphology. Applying this framework to Titan, we find that theshorelines of Titan’s seas are most consistent with flooded landscapes that subsequently have been eroded bywaves, rather than a uniform erosional process or no coastal erosion, particularly if wave growth saturates atfetch lengths of tens of kilometers.
Nearly-optimal mergesort: Fast, practical sorting methods that optimally adapt to existing runs
1. Nearly-Optimal Mergesorts
Fast, Practical Sorting Methods That Optimally Adapt to Existing Runs
Sebastian Wild
wild@uwaterloo.ca
joint work with Ian Munro
ESA 2018
26th Annual European Symposium on Algorithms
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 0 / 15
2. Outline
1 Adaptive Sorting – Status Quo1 Adaptive Sorting – Status Quo
2 Natural Mergesort2 Natural Mergesort
3 Peeksort3 Peeksort
4 Powersort4 Powersort
5 Experiments5 Experiments
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 0 / 15
3. Adaptive Sorting
Adaptive algorithm: exploit “structure” of input
adaptive sorting: exploit “presortedness”
few inversions
few runs
few outliers
...many more
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 1 / 15
4. Adaptive Sorting
Adaptive algorithm: exploit “structure” of input
adaptive sorting: exploit “presortedness”
few inversions
few runs
few outliers
...many more
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 1 / 15
5. Adaptive Sorting
Adaptive algorithm: exploit “structure” of input
adaptive sorting: exploit “presortedness”
few inversions
few runs
few outliers
...many more
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 1 / 15
6. Adaptive Sorting
Adaptive algorithm: exploit “structure” of input
adaptive sorting: exploit “presortedness”
few inversions
few runs
few outliers
...many more
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 1 / 15
7. Adaptive Sorting
Adaptive algorithm: exploit “structure” of input
adaptive sorting: exploit “presortedness”
few inversions
few runs
few outliers
...many more
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 1 / 15
8. Adaptive Sorting
Adaptive algorithm: exploit “structure” of input
adaptive sorting: exploit “presortedness”
few inversions
few runs
few outliers
...many more
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 1 / 15
9. Adaptive Sorting
Adaptive algorithm: exploit “structure” of input
adaptive sorting: exploit “presortedness”
few inversions
few runs
few outliers
...many more
optimal algorithms known for many
measures of presortedness
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 1 / 15
10. Adaptive Sorting
Adaptive algorithm: exploit “structure” of input
adaptive sorting: exploit “presortedness”
few inversions
few runs
few outliers
...many more
optim
up to constant factors!
al algorithms known for many
measures of presortedness
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 1 / 15
11. Adaptive Sorting
Adaptive algorithm: exploit “structure” of input
adaptive sorting: exploit “presortedness”
few inversions
few runs
few outliers
...many more
optim
up to constant factors!
al algorithms known for many
measures of presortedness
Want:
Optimal up to lower order terms
practical methods
low overhead for
detecting presortedness
competitive on inputs
without presortedness
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 1 / 15
12. Adaptive Sorting
Adaptive algorithm: exploit “structure” of input
adaptive sorting: exploit “presortedness”
few inversions
few runs
few outliers
...many more
optim
up to constant factors!
al algorithms known for many
measures of presortedness
Want:
Optimal up to lower order terms
practical methods
low overhead for
detecting presortedness
competitive on inputs
without presortedness
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 1 / 15
13. Adaptive Sorting
Adaptive algorithm: exploit “structure” of input
adaptive sorting: exploit “presortedness”
few inversions
few runs
few outliers
...many more
optim
up to constant factors!
al algorithms known for many
measures of presortedness
Want:
Optimal up to lower order terms
practical methods
low overhead for
detecting presortedness
competitive on inputs
without presortedness
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 1 / 15
14. Adaptive Sorting
Adaptive algorithm: exploit “structure” of input
adaptive sorting: exploit “presortedness”
few inversions
few runs
few outliers
...many more
optim
up to constant factors!
al algorithms known for many
measures of presortedness
Want:
Optimal up to lower order terms
practical methods
low overhead for
detecting presortedness
competitive on inputs
without presortedness
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 1 / 15
15. Adaptive Sorting
Adaptive algorithm: exploit “structure” of input
adaptive sorting: exploit “presortedness”
few inversions
few runs
few outliers
...many more
optim
up to constant factors!
al algorithms known for many
measures of presortedness
Want:
Optimal up to lower order terms
practical methods
low overhead for
detecting presortedness
competitive on inputs
without presortedness
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 1 / 15
16. State of the art
1 “fat-pivot” quicksort
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 2 / 15
17. State of the art
1 “fat-pivot” quicksort
split < P, = P, > P
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 2 / 15
18. State of the art
1 “fat-pivot” quicksort
split < P, = P, > P
average adapts to duplicate elements
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 2 / 15
19. State of the art
1 “fat-pivot” quicksort
split < P, = P, > P
average adapts to duplicate elements
optimal up to small constant factor
1.386 (plain quicksort)
1.188 with median-of-3
1.088 with ninther
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 2 / 15
20. State of the art
1 “fat-pivot” quicksort
split < P, = P, > P
average adapts to duplicate elements
optimal up to small constant factor
1.386 (plain quicksort)
1.188 with median-of-3
1.088 with ninther
low overhead implementation
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 2 / 15
21. State of the art
1 “fat-pivot” quicksort
split < P, = P, > P
average adapts to duplicate elements
optimal up to small constant factor
1.386 (plain quicksort)
1.188 with median-of-3
1.088 with ninther
low overhead implementation
...
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 2 / 15
22. State of the art
1 “fat-pivot” quicksort
split < P, = P, > P
average adapts to duplicate elements
optimal up to small constant factor
1.386 (plain quicksort)
1.188 with median-of-3
1.088 with ninther
low overhead implementation
...
2 Timsort
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 2 / 15
23. State of the art
1 “fat-pivot” quicksort
split < P, = P, > P
average adapts to duplicate elements
optimal up to small constant factor
1.386 (plain quicksort)
1.188 with median-of-3
1.088 with ninther
low overhead implementation
...
2 Timsort
adaptive mergesort variant
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 2 / 15
24. State of the art
1 “fat-pivot” quicksort
split < P, = P, > P
average adapts to duplicate elements
optimal up to small constant factor
1.386 (plain quicksort)
1.188 with median-of-3
1.088 with ninther
low overhead implementation
...
2 Timsort
adaptive mergesort variant
...
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 2 / 15
25. State of the art
1 “fat-pivot” quicksort
split < P, = P, > P
average adapts to duplicate elements
optimal up to small constant factor
1.386 (plain quicksort)
1.188 with median-of-3
1.088 with ninther
low overhead implementation
...
2 Timsort
adaptive mergesort variant
...
adapts to existing runs
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 2 / 15
26. State of the art
1 “fat-pivot” quicksort
split < P, = P, > P
average adapts to duplicate elements
optimal up to small constant factor
1.386 (plain quicksort)
1.188 with median-of-3
1.088 with ninther
low overhead implementation
...
2 Timsort
adaptive mergesort variant
...
adapts to existing runs
but not optimally!
factor ≥ 1.5 worse (Buss & Knop 2018)
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 2 / 15
27. State of the art
1 “fat-pivot” quicksort
split < P, = P, > P
average adapts to duplicate elements
optimal up to small constant factor
1.386 (plain quicksort)
1.188 with median-of-3
1.088 with ninther
low overhead implementation
...
2 Timsort
adaptive mergesort variant
...
adapts to existing runs
but not optimally!
factor ≥ 1.5 worse (Buss & Knop 2018)
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 2 / 15
28. State of the art
1 “fat-pivot” quicksort
split < P, = P, > P
average adapts to duplicate elements
optimal up to small constant factor
1.386 (plain quicksort)
1.188 with median-of-3
1.088 with ninther
low overhead implementation
...
2 Timsort
adaptive mergesort variant
...
adapts to existing runs
but not optimally!
factor ≥ 1.5 worse (Buss & Knop 2018)
Timsort still broken!
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 2 / 15
29. State of the art
1 “fat-pivot” quicksort
split < P, = P, > P
average adapts to duplicate elements
optimal up to small constant factor
1.386 (plain quicksort)
1.188 with median-of-3
1.088 with ninther
low overhead implementation
...
2 Timsort
adaptive mergesort variant
...
adapts to existing runs
but not optimally!
factor ≥ 1.5 worse (Buss & Knop 2018)
Timsort still broken!
„it is still possible to cause the Java imple-
mentation to fail: [...] causing an error
at runtime in Java’s sorting method.”
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 2 / 15
30. State of the art
1 “fat-pivot” quicksort
split < P, = P, > P
average adapts to duplicate elements
optimal up to small constant factor
1.386 (plain quicksort)
1.188 with median-of-3
1.088 with ninther
low overhead implementation
...
2 Timsort
adaptive mergesort variant
...
adapts to existing runs
but not optimally!
factor ≥ 1.5 worse (Buss & Knop 2018)
Timsort still broken!
„it is still possible to cause the Java imple-
mentation to fail: [...] causing an error
at runtime in Java’s sorting method.”
Observation:
Timsort’s merge rules are quite intricate.
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 2 / 15
31. State of the art
1 “fat-pivot” quicksort
split < P, = P, > P
average adapts to duplicate elements
optimal up to small constant factor
1.386 (plain quicksort)
1.188 with median-of-3
1.088 with ninther
low overhead implementation
...
2 Timsort
adaptive mergesort variant
...
adapts to existing runs
but not optimally!
factor ≥ 1.5 worse (Buss & Knop 2018)
Timsort still broken!
„it is still possible to cause the Java imple-
mentation to fail: [...] causing an error
at runtime in Java’s sorting method.”
Observation:
Timsort’s merge rules are quite intricate.
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 2 / 15
32. State of the art
1 “fat-pivot” quicksort
split < P, = P, > P
average adapts to duplicate elements
optimal up to small constant factor
1.386 (plain quicksort)
1.188 with median-of-3
1.088 with ninther
low overhead implementation
...
2 Timsort
adaptive mergesort variant
...
adapts to existing runs
but not optimally!
factor ≥ 1.5 worse (Buss & Knop 2018)
Timsort still broken!
„it is still possible to cause the Java imple-
mentation to fail: [...] causing an error
at runtime in Java’s sorting method.”
Observation:
Timsort’s merge rules are quite intricate.
? Why these rules?
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 2 / 15
33. State of the art
1 “fat-pivot” quicksort
split < P, = P, > P
average adapts to duplicate elements
optimal up to small constant factor
1.386 (plain quicksort)
1.188 with median-of-3
1.088 with ninther
low overhead implementation
...
2 Timsort
adaptive mergesort variant
...
adapts to existing runs
but not optimally!
factor ≥ 1.5 worse (Buss & Knop 2018)
Timsort still broken!
„it is still possible to cause the Java imple-
mentation to fail: [...] causing an error
at runtime in Java’s sorting method.”
Observation:
Timsort’s merge rules are quite intricate.
? Why these rules?
? Why are they so sensitive to sma
cf. Java version!
ll changes?
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 2 / 15
34. State of the art
1 “fat-pivot” quicksort
split < P, = P, > P
average adapts to duplicate elements
optimal up to small constant factor
1.386 (plain quicksort)
1.188 with median-of-3
1.088 with ninther
low overhead implementation
...
2 Timsort
adaptive mergesort variant
...
adapts to existing runs
but not optimally!
factor ≥ 1.5 worse (Buss & Knop 2018)
Timsort still broken!
„it is still possible to cause the Java imple-
mentation to fail: [...] causing an error
at runtime in Java’s sorting method.”
Observation:
Timsort’s merge rules are quite intricate.
? Why these rules?
? Why are they so sensitive to sma
cf. Java version!
ll changes?
... and can’t we find simpler rules?
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 2 / 15
37. Run-Length Entropy
Our measure of unsortedness: runs
maximal contiguous sorted range
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 3 / 15
38. Run-Length Entropy
Our measure of unsortedness: runs
maximal contiguous sorted range
simple version: lg(#runs)
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 3 / 15
39. Run-Length Entropy
Our measure of unsortedness: runs
maximal contiguous sorted range
simple version: lg(#runs)
fine-grained version: entropy of run lengths
runs lengths L1, . . . , Lr H
L1
n
, . . . ,
Lr
n
=
r
i=1
Li
n
lg
n
Li
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 3 / 15
40. Run-Length Entropy
Our measure of unsortedness: runs
maximal contiguous sorted range
simple version: lg(#runs)
fine-grained version: entropy of run lengths
runs lengths L1, . . . , Lr H
L1
n
, . . . ,
Lr
n
=
r
i=1
Li
n
lg
n
Li
Comparison Lower Bound
n! permutations in total
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 3 / 15
41. Run-Length Entropy
Our measure of unsortedness: runs
maximal contiguous sorted range
simple version: lg(#runs)
fine-grained version: entropy of run lengths
runs lengths L1, . . . , Lr H
L1
n
, . . . ,
Lr
n
=
r
i=1
Li
n
lg
n
Li
Comparison Lower Bound
n! permutations in total
but sorted within runs
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 3 / 15
42. Run-Length Entropy
Our measure of unsortedness: runs
maximal contiguous sorted range
simple version: lg(#runs)
fine-grained version: entropy of run lengths
runs lengths L1, . . . , Lr H
L1
n
, . . . ,
Lr
n
=
r
i=1
Li
n
lg
n
Li
Comparison Lower Bound
n! permutations in total
but sorted within runs
n!
L1! · · · Lr!
possible inputs
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 3 / 15
43. Run-Length Entropy
Our measure of unsortedness: runs
maximal contiguous sorted range
simple version: lg(#runs)
fine-grained version: entropy of run lengths
runs lengths L1, . . . , Lr H
L1
n
, . . . ,
Lr
n
=
r
i=1
Li
n
lg
n
Li
Comparison Lower Bound
n! permutations in total
but sorted within runs
n!
L1! · · · Lr!
possible inputs
Need lg
n!
L1! · · · Lr!
= H L1
n , . . . , Lr
n · n − O(n) comparisons
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 3 / 15
44. Run-Length Entropy
Our measure of unsortedness: runs
maximal contiguous sorted range
simple version: lg(#runs)
fine-grained version: entropy of run lengths
runs lengths L1, . . . , Lr H
L1
n
, . . . ,
Lr
n
=
r
i=1
Li
n
lg
n
Li
Comparison Lower Bound
n! permutations in total
but sorted within runs
n!
L1! · · · Lr!
possible inputs
Need lg
n!
L1! · · · Lr!
= H L
only H in the following
1
n , . . . , Lr
n · n − O(n) comparisons
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 3 / 15
45. Outline
1 Adaptive Sorting – Status Quo1 Adaptive Sorting – Status Quo
2 Natural Mergesort2 Natural Mergesort
3 Peeksort3 Peeksort
4 Powersort4 Powersort
5 Experiments5 Experiments
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 3 / 15
48. Natural Mergesort
“natural” mergesort = run-adaptive mergesort
(Knuth 1973)
Conceptually two steps:
1 Find runs in input.
2 Merge them
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 4 / 15
49. Natural Mergesort
“natural” mergesort = run-adaptive mergesort
(Knuth 1973)
Concep
interleaved in code
tually two steps:
1 Find runs in input.
2 Merge them
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 4 / 15
50. Natural Mergesort
“natural” mergesort = run-adaptive mergesort
(Knuth 1973)
Concep
interleaved in code
tually two steps:
1 Find runs in input.
2 Merge them
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 4 / 15
51. Natural Mergesort
“natural” mergesort = run-adaptive mergesort
(Knuth 1973)
Concep
interleaved in code
tually two steps:
1 Find runs in input.
2 Merge them
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 4 / 15
52. Natural Mergesort
“natural” mergesort = run-adaptive mergesort
(Knuth 1973)
Concep
interleaved in code
tually two steps:
1 Find runs in input.
2 Merge them in some order.
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 4 / 15
53. Natural Mergesort
“natural” mergesort = run-adaptive mergesort
(Knuth 1973)
Concep
interleaved in code
tually two steps:
1 Find runs in input.
2 Merge them in some order
(Knuth: simple bottom-up)
.
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 4 / 15
54. Natural Mergesort
“natural” mergesort = run-adaptive mergesort
Conceptually two steps:
1 Find runs in input.
2 Merge them in some order.
Here:
only binary merges
2 becomes:
merge 2 runs,
repeat until single run
only stable sorts
merge 2 adjacent runs
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 4 / 15
55. Natural Mergesort
“natural” mergesort = run-adaptive mergesort
Conceptually two steps:
1 Find runs in input.
2 Merge them in some order.
Here:
only binary merges
2 becomes:
merge 2 runs,
repeat until single run
only stable sorts
merge 2 adjacent runs
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 4 / 15
56. Natural Mergesort
“natural” mergesort = run-adaptive mergesort
Conceptually two steps:
1 Find runs in input.
2 Merge them in some order.
Here:
only binary merges
2 becomes:
merge 2 runs,
repeat until single run
only stable sorts
merge 2 adjacent runs
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 4 / 15
57. Natural Mergesort
“natural” mergesort = run-adaptive mergesort
Conceptually two steps:
1 Find runs in input.
2 Merge them in some order.
Here:
only binary merges
2 becomes:
merge 2 runs,
repeat until single run
only stable sorts
merge 2 adjacent runs
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 4 / 15
58. Natural Mergesort
“natural” mergesort = run-adaptive mergesort
Conceptually two steps:
1 Find runs in input.
2 Merge them in some order.
Here:
only binary merges
2 becomes:
merge 2 runs,
repeat until single run
only stable sorts
merge 2 adjacent runs
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 4 / 15
59. Natural Mergesort
“natural” mergesort = run-adaptive mergesort
Conceptually two steps:
1 Find runs in input.
2 Merge them in some order.
Here:
only binary merges
2 becomes:
merge 2 runs,
repeat until single run
only stable sorts
merge 2 adjacent runs
Merge trees:
15 17 12 19 2 9 13 7 11 1 4 8 10 14 23 5 21 3 6 16 18 20 22
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 4 / 15
60. Natural Mergesort
“natural” mergesort = run-adaptive mergesort
Conceptually two steps:
1 Find runs in input.
2 Merge them in some order.
Here:
only binary merges
2 becomes:
merge 2 runs,
repeat until single run
only stable sorts
merge 2 adjacent runs
Merge trees:
15 17 12 19 2 9 13 7 11 1 4 8 10 14 23 5 21 3 6 16 18 20 22
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 4 / 15
61. Natural Mergesort
“natural” mergesort = run-adaptive mergesort
Conceptually two steps:
1 Find runs in input.
2 Merge them in some order.
Here:
only binary merges
2 becomes:
merge 2 runs,
repeat until single run
only stable sorts
merge 2 adjacent runs
Merge trees:
15 17 12 19 2 9 13 7 11 1 4 8 10 14 23 5 21 3 6 16 18 20 22
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 4 / 15
62. Natural Mergesort
“natural” mergesort = run-adaptive mergesort
Conceptually two steps:
1 Find runs in input.
2 Merge them in some order.
Here:
only binary merges
2 becomes:
merge 2 runs,
repeat until single run
only stable sorts
merge 2 adjacent runs
Merge trees:
15 17 12 19 2 9 13 7 11 1 4 8 10 14 23 5 21 3 6 16 18 20 22
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 4 / 15
63. Natural Mergesort
“natural” mergesort = run-adaptive mergesort
Conceptually two steps:
1 Find runs in input.
2 Merge them in some order.
Here:
only binary merges
2 becomes:
merge 2 runs,
repeat until single run
only stable sorts
merge 2 adjacent runs
Merge trees:
15 17 12 19 2 9 13 7 11 1 4 8 10 14 23 5 21 3 6 16 18 20 22
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 4 / 15
64. Natural Mergesort
“natural” mergesort = run-adaptive mergesort
Conceptually two steps:
1 Find runs in input.
2 Merge them in some order.
Here:
only binary merges
2 becomes:
merge 2 runs,
repeat until single run
only stable sorts
merge 2 adjacent runs
Merge trees:
15 17 12 19 2 9 13 7 11 1 4 8 10 14 23 5 21 3 6 16 18 20 22
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 4 / 15
65. Natural Mergesort
“natural” mergesort = run-adaptive mergesort
Conceptually two steps:
1 Find runs in input.
2 Merge them in some order.
Here:
only binary merges
2 becomes:
merge 2 runs,
repeat until single run
only stable sorts
merge 2 adjacent runs
Merge trees:
15 17 12 19 2 9 13 7 11 1 4 8 10 14 23 5 21 3 6 16 18 20 22
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 4 / 15
66. Natural Mergesort
“natural” mergesort = run-adaptive mergesort
Conceptually two steps:
1 Find runs in input.
2 Merge them in some order.
Here:
only binary merges
2 becomes:
merge 2 runs,
repeat until single run
only stable sorts
merge 2 adjacent runs
Merge trees:
15 17 12 19 2 9 13 7 11 1 4 8 10 14 23 5 21 3 6 16 18 20 22
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 4 / 15
67. Natural Mergesort
“natural” mergesort = run-adaptive mergesort
Conceptually two steps:
1 Find runs in input.
2 Merge them in some order.
Here:
only binary merges
2 becomes:
merge 2 runs,
repeat until single run
only stable sorts
merge 2 adjacent runs
Merge trees:
15 17 12 19 2 9 13 7 11 1 4 8 10 14 23 5 21 3 6 16 18 20 22
Merge costs
cost of merge := size of output
≈ memory transfers
#cmps
total cost = total area of
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 4 / 15
68. Natural Mergesort
“natural” mergesort = run-adaptive mergesort
Conceptually two steps:
1 Find runs in input.
2 Merge them in some order.
Here:
only binary merges
2 becomes:
merge 2 runs,
repeat until single run
only stable sorts
merge 2 adjacent runs
Merge trees:
15 17 12 19 2 9 13 7 11 1 4 8 10 14 23 5 21 3 6 16 18 20 22
Merge costs
cost of merge := size of output
≈ memory transfers
#cmps
total cost = total area of
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 4 / 15
69. Natural Mergesort
“natural” mergesort = run-adaptive mergesort
Conceptually two steps:
1 Find runs in input.
2 Merge them in some order.
Here:
only binary merges
2 becomes:
merge 2 runs,
repeat until single run
only stable sorts
merge 2 adjacent runs
Merge trees:
15 17 12 19 2 9 13 7 11 1 4 8 10 14 23 5 21 3 6 16 18 20 22
Merge costs
cost of merge := size of output
≈ memory transfers
#cmps
total cost = total area of
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 4 / 15
70. Natural Mergesort
“natural” mergesort = run-adaptive mergesort
Conceptually two steps:
1 Find runs in input.
2 Merge them in some order.
Here:
only binary merges
2 becomes:
merge 2 runs,
repeat until single run
only stable sorts
merge 2 adjacent runs
Merge trees:
15 17 12 19 2 9 13 7 11 1 4 8 10 14 23 5 21 3 6 16 18 20 22
Merge costs
cost of merge := size of output
≈ memory transfers
#cmps
total cost = total area of
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 4 / 15
71. Natural Mergesort
“natural” mergesort = run-adaptive mergesort
Conceptually two steps:
1 Find runs in input.
2 Merge them in some order.
Here:
only binary merges
2 becomes:
merge 2 runs,
repeat until single run
only stable sorts
merge 2 adjacent runs
Merge trees:
15 17 12 19 2 9 13 7 11 1 4 8 10 14 23 5 21 3 6 16 18 20 22
Merge costs
cost of merge := size of output
≈ memory transfers
#cmps
total cost = total area of
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 4 / 15
72. Mergesort meets search trees
Different merge trees yield different cost!
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 5 / 15
78. Mergesort meets search trees
Different merge trees yield different cost!
15 17 12 19 2 9 13 7 11 1 4 8 10 14 23 5 21 3 6 16 18 20 22
Merge cost = total area of
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 5 / 15
79. Mergesort meets search trees
Different merge trees yield different cost!
15 17 12 19 2 9 13 7 11 1 4 8 10 14 23 5 21 3 6 16 18 20 22
Merge cost = total area of
= total length of paths to all array entries
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 5 / 15
80. Mergesort meets search trees
Different merge trees yield different cost!
2 32 2 6 2 6
Merge cost = total area of
= total length of paths to all array entries
= weighted external path
w leaf
weight(w) · depth(w)
length
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 5 / 15
81. Mergesort meets search trees
Different merge trees yield different cost!
2 32 2 6 2 6
Merge cost = total area of
= total length of paths to all array entries
= weighted external path
w leaf
weight(w) · depth(w)
length
optimal merge tree
= optimal BST for leaf weights L1, . . . , Lr
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 5 / 15
82. Mergesort meets search trees
Different merge trees yield different cost!
2 32 2 6 2 6
Merge cost = total area of
= total length of paths to all array entries
= weighted external path
w leaf
weight(w) · depth(w)
length
optimal merge tree
= optimal BST for leaf weights L1, . . . , Lr
How to compute good merge tree?
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 5 / 15
83. Mergesort meets search trees
Different merge trees yield different cost!
2 32 2 6 2 6
Merge cost = total area of
= total length of paths to all array entries
= weighted external path
w leaf
weight(w) · depth(w)
length
optimal merge tree
= optimal BST for leaf weights L1, . . . , Lr
How to compute good merge tree?
Huffman merge
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 5 / 15
84. Mergesort meets search trees
Different merge trees yield different cost!
2 32 2 6 2 6
Merge cost = total area of
= total length of paths to all array entries
= weighted external path
w leaf
weight(w) · depth(w)
length
optimal merge tree
= optimal BST for leaf weights L1, . . . , Lr
How to compute good merge tree?
Huffman merge
merge shortest runs
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 5 / 15
85. Mergesort meets search trees
Different merge trees yield different cost!
2 32 2 6 2 6
Merge cost = total area of
= total length of paths to all array entries
= weighted external path
w leaf
weight(w) · depth(w)
length
optimal merge tree
= optimal BST for leaf weights L1, . . . , Lr
How to compute good merge tree?
Huffman merge
merge shortest runs
indep. discovered
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 5 / 15
86. Mergesort meets search trees
Different merge trees yield different cost!
2 32 2 6 2 6
Merge cost = total area of
= total length of paths to all array entries
= weighted external path
w leaf
weight(w) · depth(w)
length
optimal merge tree
= optimal BST for leaf weights L1, . . . , Lr
How to compute good merge tree?
Huffman merge
merge shortest runs
indep. discovered
• Golin & Sedgewick 1993
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 5 / 15
87. Mergesort meets search trees
Different merge trees yield different cost!
2 32 2 6 2 6
Merge cost = total area of
= total length of paths to all array entries
= weighted external path
w leaf
weight(w) · depth(w)
length
optimal merge tree
= optimal BST for leaf weights L1, . . . , Lr
How to compute good merge tree?
Huffman merge
merge shortest runs
indep. discovered
• Golin & Sedgewick 1993
• Takaoka 1998
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 5 / 15
88. Mergesort meets search trees
Different merge trees yield different cost!
2 32 2 6 2 6
Merge cost = total area of
= total length of paths to all array entries
= weighted external path
w leaf
weight(w) · depth(w)
length
optimal merge tree
= optimal BST for leaf weights L1, . . . , Lr
How to compute good merge tree?
Huffman merge
merge shortest runs
indep. discovered
• Golin & Sedgewick 1993
• Takaoka 1998
• Barbay & Navarro 2009
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 5 / 15
89. Mergesort meets search trees
Different merge trees yield different cost!
2 32 2 6 2 6
Merge cost = total area of
= total length of paths to all array entries
= weighted external path
w leaf
weight(w) · depth(w)
length
optimal merge tree
= optimal BST for leaf weights L1, . . . , Lr
How to compute good merge tree?
Huffman merge
merge shortest runs
indep. discovered
• Golin & Sedgewick 1993
• Takaoka 1998
• Barbay & Navarro 2009
• Chandramouli & Goldstein 2014
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 5 / 15
90. Mergesort meets search trees
Different merge trees yield different cost!
2 32 2 6 2 6
Merge cost = total area of
= total length of paths to all array entries
= weighted external path
w leaf
weight(w) · depth(w)
length
optimal merge tree
= optimal BST for leaf weights L1, . . . , Lr
How to compute good merge tree?
Huffman merge
merge shortest runs
indep. discovered
• Golin & Sedgewick 1993
• Takaoka 1998
• Barbay & Navarro 2009
• Chandramouli & Goldstein 2014
must sort lengths
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 5 / 15
91. Mergesort meets search trees
Different merge trees yield different cost!
2 32 2 6 2 6
Merge cost = total area of
= total length of paths to all array entries
= weighted external path
w leaf
weight(w) · depth(w)
length
optimal merge tree
= optimal BST for leaf weights L1, . . . , Lr
How to compute good merge tree?
Huffman merge
merge shortest runs
indep. discovered
• Golin & Sedgewick 1993
• Takaoka 1998
• Barbay & Navarro 2009
• Chandramouli & Goldstein 2014
must sort lengths
not stable
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 5 / 15
92. Mergesort meets search trees
Different merge trees yield different cost!
2 32 2 6 2 6
Merge cost = total area of
= total length of paths to all array entries
= weighted external path
w leaf
weight(w) · depth(w)
length
optimal merge tree
= optimal BST for leaf weights L1, . . . , Lr
How to compute good merge tree?
Huffman merge
merge shortest runs
indep. discovered
• Golin & Sedgewick 1993
• Takaoka 1998
• Barbay & Navarro 2009
• Chandramouli & Goldstein 2014
must sort lengths
not stable
Hu-Tucker merge
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 5 / 15
93. Mergesort meets search trees
Different merge trees yield different cost!
2 32 2 6 2 6
Merge cost = total area of
= total length of paths to all array entries
= weighted external path
w leaf
weight(w) · depth(w)
length
optimal merge tree
= optimal BST for leaf weights L1, . . . , Lr
How to compute good merge tree?
Huffman merge
merge shortest runs
indep. discovered
• Golin & Sedgewick 1993
• Takaoka 1998
• Barbay & Navarro 2009
• Chandramouli & Goldstein 2014
must sort lengths
not stable
Hu-Tucker merge
optimal alphabetic tree
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 5 / 15
94. Mergesort meets search trees
Different merge trees yield different cost!
2 32 2 6 2 6
Merge cost = total area of
= total length of paths to all array entries
= weighted external path
w leaf
weight(w) · depth(w)
length
optimal merge tree
= optimal BST for leaf weights L1, . . . , Lr
How to compute good merge tree?
Huffman merge
merge shortest runs
indep. discovered
• Golin & Sedgewick 1993
• Takaoka 1998
• Barbay & Navarro 2009
• Chandramouli & Goldstein 2014
must sort lengths
not stable
Hu-Tucker merge
optimal alphabetic tree
have to store lengths
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 5 / 15
95. Mergesort meets search trees
Different merge trees yield different cost!
2 32 2 6 2 6
Merge cost = total area of
= total length of paths to all array entries
= weighted external path
w leaf
weight(w) · depth(w)
length
optimal merge tree
= optimal BST for leaf weights L1, . . . , Lr
How to compute good merge tree?
Huffman merge
merge shortest runs
indep. discovered
• Golin & Sedgewick 1993
• Takaoka 1998
• Barbay & Navarro 2009
• Chandramouli & Goldstein 2014
must sort lengths
not stable
Hu-Tucker merge
optimal alphabetic tree
have to store lengths
complicated algorithm
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 5 / 15
96. Mergesort meets search trees
Different merge trees yield different cost!
2 32 2 6 2 6
Merge cost = total area of
= total length of paths to all array entries
= weighted external path
w leaf
weight(w) · depth(w)
length
optimal merge tree
= optimal BST for leaf weights L1, . . . , Lr
How to compute good merge tree?
Huffman merge
merge shortest runs
indep. discovered
• Golin & Sedgewick 1993
• Takaoka 1998
• Barbay & Navarro 2009
• Chandramouli & Goldstein 2014
must sort lengths
not stable
Hu-Tucker merge
optimal alphabetic tree
have to store lengths
complicated algorithm
nearly-optimal
...70s are calling
BST merge
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 5 / 15
97. Mergesort meets search trees
Different merge trees yield different cost!
2 32 2 6 2 6
Merge cost = total area of
= total length of paths to all array entries
= weighted external path
w leaf
weight(w) · depth(w)
length
optimal merge tree
= optimal BST for leaf weights L1, . . . , Lr
How to compute good merge tree?
Huffman merge
merge shortest runs
indep. discovered
• Golin & Sedgewick 1993
• Takaoka 1998
• Barbay & Navarro 2009
• Chandramouli & Goldstein 2014
must sort lengths
not stable
Hu-Tucker merge
optimal alphabetic tree
have to store lengths
complicated algorithm
nearly-optimal
...70s are calling
BST merge
simple (greedy) linear-time methods!
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 5 / 15
98. Mergesort meets search trees
Different merge trees yield different cost!
2 32 2 6 2 6
Merge cost = total area of
= total length of paths to all array entries
= weighted external path
w leaf
weight(w) · depth(w)
length
optimal merge tree
= optimal BST for leaf weights L1, . . . , Lr
How to compute good merge tree?
Huffman merge
merge shortest runs
indep. discovered
• Golin & Sedgewick 1993
• Takaoka 1998
• Barbay & Navarro 2009
• Chandramouli & Goldstein 2014
must sort lengths
not stable
Hu-Tucker merge
optimal alphabetic tree
have to store lengths
complicated algorithm
nearly-optimal
...70s are calling
BST merge
simple (greedy) linear-time methods!
almost optimal ( H + 2)
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 5 / 15
99. Mergesort meets search trees
Different merge trees yield different cost!
2 32 2 6 2 6
Merge cost = total area of
= total length of paths to all array entries
= weighted external path
w leaf
weight(w) · depth(w)
length
optimal merge tree
= optimal BST for leaf weights L1, . . . , Lr
How to compute good merge tree?
Huffman merge
merge shortest runs
indep. discovered
• Golin & Sedgewick 1993
• Takaoka 1998
• Barbay & Navarro 2009
• Chandramouli & Goldstein 2014
must sort lengths
not stable
Hu-Tucker merge
optimal alphabetic tree
have to store lengths
complicated algorithm
nearly-optimal
...70s are calling
BST merge
simple (greedy) linear-time methods!
almost optimal ( H + 2)
ŏ have to store lengths
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 5 / 15
100. Mergesort meets search trees
Different merge trees yield different cost!
2 32 2 6 2 6
Merge cost = total area of
= total length of paths to all array entries
= weighted external path
w leaf
weight(w) · depth(w)
length
optimal merge tree
= optimal BST for leaf weights L1, . . . , Lr
How to compute good merge tree?
Huffman merge
merge shortest runs
indep. discovered
• Golin & Sedgewick 1993
• Takaoka 1998
• Barbay & Navarro 2009
• Chandramouli & Goldstein 2014
must sort lengths
not stable
Hu-Tucker merge
optimal alphabetic tree
have to store lengths
complicated algorithm
nearly-optimal
...70s are calling
BST merge
simple (greedy) linear-time methods!
almost optimal ( H + 2)
ŏ have to store lengths
ŏ extra scan to detect runs
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 5 / 15
101. Mergesort meets search trees
Different merge trees yield different cost!
2 32 2 6 2 6
Merge cost = total area of
= total length of paths to all array entries
= weighted external path
w leaf
weight(w) · depth(w)
length
optimal merge tree
= optimal BST for leaf weights L1, . . . , Lr
How to compute good merge tree?
Huffman merge
merge shortest runs
indep. discovered
• Golin & Sedgewick 1993
• Takaoka 1998
• Barbay & Navarro 2009
• Chandramouli & Goldstein 2014
must sort lengths
not stable
Hu-Tucker merge
optimal alphabetic tree
have to store lengths
complicated algorithm
nearly-optimal
...70s are calling
BST merge
simple (greedy) linear-time methods!
almost optimal ( H + 2)
ŏ have to store lengths
ŏ extra scan to detect runs
avoidable?
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 5 / 15
102. Outline
1 Adaptive Sorting – Status Quo1 Adaptive Sorting – Status Quo
2 Natural Mergesort2 Natural Mergesort
3 Peeksort3 Peeksort
4 Powersort4 Powersort
5 Experiments5 Experiments
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 5 / 15
103. Peeksort
Method 1: weight-balancing
(Mehlhorn 1975, Bayer 1975)
choose root to balance subtree weights
recurse on subtrees
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 6 / 15
104. Peeksort
Method 1: weight-balancing
(Mehlhorn 1975, Bayer 1975)
choose root to balance subtree weights
recurse on subtrees
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 6 / 15
105. Peeksort
Method 1: weight-balancing
(Mehlhorn 1975, Bayer 1975)
choose root to balance subtree weights
recurse on subtrees
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 6 / 15
106. Peeksort
Method 1: weight-balancing
(Mehlhorn 1975, Bayer 1975)
choose root to balance subtree weights
recurse on subtrees
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 6 / 15
107. Peeksort
Method 1: weight-balancing
(Mehlhorn 1975, Bayer 1975)
choose root to balance subtree weights
recurse on subtrees
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 6 / 15
108. Peeksort
Method 1: weight-balancing
(Mehlhorn 1975, Bayer 1975)
choose root to balance subtree weights
recurse on subtrees
1⁄2
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 6 / 15
109. Peeksort
Method 1: weight-balancing
(Mehlhorn 1975, Bayer 1975)
choose root to balance subtree weights
recurse on subtrees
1⁄2
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 6 / 15
110. Peeksort
Method 1: weight-balancing
(Mehlhorn 1975, Bayer 1975)
choose root to balance subtree weights
recurse on subtrees
1⁄2
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 6 / 15
111. Peeksort
Method 1: weight-balancing
(Mehlhorn 1975, Bayer 1975)
choose root to balance subtree weights
recurse on subtrees
1⁄2
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 6 / 15
112. Peeksort
Method 1: weight-balancing
(Mehlhorn 1975, Bayer 1975)
choose root to balance subtree weights
recurse on subtrees
1⁄2
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 6 / 15
113. Peeksort
Method 1: weight-balancing
(Mehlhorn 1975, Bayer 1975)
choose root to balance subtree weights
recurse on subtrees
1⁄2
1⁄2
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 6 / 15
114. Peeksort
Method 1: weight-balancing
(Mehlhorn 1975, Bayer 1975)
choose root to balance subtree weights
recurse on subtrees
1⁄2
1⁄2
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 6 / 15
115. Peeksort
Method 1: weight-balancing
(Mehlhorn 1975, Bayer 1975)
choose root to balance subtree weights
recurse on subtrees
1⁄2
1⁄2
1⁄2
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 6 / 15
116. Peeksort
Method 1: weight-balancing
(Mehlhorn 1975, Bayer 1975)
choose root to balance subtree weights
recurse on subtrees
1⁄2
1⁄2
1⁄2
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 6 / 15
117. Peeksort
Method 1: weight-balancing
(Mehlhorn 1975, Bayer 1975)
choose root to balance subtree weights
recurse on subtrees
1⁄2
1⁄2
1⁄2
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 6 / 15
118. Peeksort
Method 1: weight-balancing
(Mehlhorn 1975, Bayer 1975)
choose root to balance subtree weights
recurse on subtrees
1⁄2
1⁄2
1⁄2
Peeksort
can simulate weight-balancing
without knowing/storing all runs!
“peek” at middle of array
to find closest run boundary
split there and recurse
can avoid redundant work:
find full run straddling midpoint
4 parameters for recursive calls:
re s
stores outermost runs
each run scanned only once
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 6 / 15
119. Peeksort
Method 1: weight-balancing
(Mehlhorn 1975, Bayer 1975)
choose root to balance subtree weights
recurse on subtrees
1⁄2
1⁄2
1⁄2
Peeksort
can simulate weight-balancing
without knowing/storing all runs!
“peek” at middle of array
to find closest run boundary
split there and recurse
can avoid redundant work:
find full run straddling midpoint
4 parameters for recursive calls:
re s
stores outermost runs
each run scanned only once
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 6 / 15
120. Peeksort
Method 1: weight-balancing
(Mehlhorn 1975, Bayer 1975)
choose root to balance subtree weights
recurse on subtrees
1⁄2
1⁄2
1⁄2
Peeksort
can simulate weight-balancing
without knowing/storing all runs!
“peek” at middle of array
to find closest run boundary
split there and recurse
can avoid redundant work:
find full run straddling midpoint
4 parameters for recursive calls:
re s
stores outermost runs
each run scanned only once
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 6 / 15
121. Peeksort
Method 1: weight-balancing
(Mehlhorn 1975, Bayer 1975)
choose root to balance subtree weights
recurse on subtrees
1⁄2
1⁄2
1⁄2
Peeksort
can simulate weight-balancing
without knowing/storing all runs!
“peek” at middle of array
to find closest run boundary
split there and recurse
can avoid redundant work:
find full run straddling midpoint
4 parameters for recursive calls:
re s
stores outermost runs
each run scanned only once
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 6 / 15
122. Peeksort
Method 1: weight-balancing
(Mehlhorn 1975, Bayer 1975)
choose root to balance subtree weights
recurse on subtrees
1⁄2
1⁄2
1⁄2
Peeksort
can simulate weight-balancing
without knowing/storing all runs!
“peek” at middle of array
to find closest run boundary
split there and recurse
can avoid redundant work:
find full run straddling midpoint
4 parameters for recursive calls:
re s
stores outermost runs
each run scanned only once
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 6 / 15
123. Peeksort
Method 1: weight-balancing
(Mehlhorn 1975, Bayer 1975)
choose root to balance subtree weights
recurse on subtrees
1⁄2
1⁄2
1⁄2
Peeksort
can simulate weight-balancing
without knowing/storing all runs!
“peek” at middle of array
to find closest run boundary
split there and recurse
can avoid redundant work:
find full run straddling midpoint
4 parameters for recursive calls:
re s
stores outermost runs
each run scanned only once
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 6 / 15
124. Peeksort
Method 1: weight-balancing
(Mehlhorn 1975, Bayer 1975)
choose root to balance subtree weights
recurse on subtrees
1⁄2
1⁄2
1⁄2
Peeksort
can simulate weight-balancing
without knowing/storing all runs!
“peek” at middle of array
to find closest run boundary
split there and recurse
can avoid redundant work:
find full run straddling midpoint
4 parameters for recursive calls:
re s
stores outermost runs
each run scanned only once
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 6 / 15
125. Peeksort
Method 1: weight-balancing
(Mehlhorn 1975, Bayer 1975)
choose root to balance subtree weights
recurse on subtrees
1⁄2
1⁄2
1⁄2
Peeksort
can simulate weight-balancing
without knowing/storing all runs!
“peek” at middle of array
to find closest run boundary
split there and recurse
can avoid redundant work:
find full run straddling midpoint
4 parameters for recursive calls:
re s
stores outermost runs
each run scanned only once
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 6 / 15
126. Peeksort
Method 1: weight-balancing
(Mehlhorn 1975, Bayer 1975)
choose root to balance subtree weights
recurse on subtrees
1⁄2
1⁄2
1⁄2
Peeksort
can simulate weight-balancing
without knowing/storing all runs!
“peek” at middle of array
to find closest run boundary
split there and recurse
can avoid redundant work:
find full run straddling midpoint
4 parameters for recursive calls:
re s
stores outermost runs
empty if = e resp. s = r
each run scanned only once
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 6 / 15
127. Peeksort
Method 1: weight-balancing
(Mehlhorn 1975, Bayer 1975)
choose root to balance subtree weights
recurse on subtrees
1⁄2
1⁄2
1⁄2
Peeksort
can simulate weight-balancing
without knowing/storing all runs!
“peek” at middle of array
to find closest run boundary
split there and recurse
can avoid redundant work:
find full run straddling midpoint
4 parameters for recursive calls:
re s
stores outermost runs
empty if = e resp. s = r
each run scanned only once
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 6 / 15
128. Analysis of peeksort
Theorem (Horibe 1977, Bayer 1975:)
Weight-balancing on leaf probabilities
α1, . . . , αr yields a BST with search cost
C H(α1, . . . , αr) + 2.
immediate corollary:
Peeksort incurs merge cost M (H + 2)n.
Peeksort needs C n + (H + 2)n cmps.
both are optimal up to O(n) terms
Peeksort exploits existing runs
optimally up to lower order terms!
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 7 / 15
129. Analysis of peeksort
Theorem (Horibe 1977, Bayer 1975:)
Weight-balancing on leaf probabilities
α1, . . . , αr yields a BST with search cost
exp. #cmps to find
random leaf chosen
with prob. αi.
C H(α1, . . . , αr) + 2.
immediate corollary:
Peeksort incurs merge cost M (H + 2)n.
Peeksort needs C n + (H + 2)n cmps.
both are optimal up to O(n) terms
Peeksort exploits existing runs
optimally up to lower order terms!
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 7 / 15
130. Analysis of peeksort
Theorem (Horibe 1977, Bayer 1975:)
Weight-balancing on leaf probabilities
α1, . . . , αr yields a BST with search cost
exp. #cmps to find
random leaf chosen
with prob. αi.
C H(α1, . . . , αr) + 2.
immediate corollary:
Peeksort incurs merge cost M (H + 2)n.
Peeksort needs C n + (H + 2)n cmps.
both are optimal up to O(n) terms
Peeksort exploits existing runs
optimally up to lower order terms!
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 7 / 15
131. Analysis of peeksort
Theorem (Horibe 1977, Bayer 1975:)
Weight-balancing on leaf probabilities
α1, . . . , αr yields a BST with search cost
exp. #cmps to find
random leaf chosen
with prob. αi.
C H(α1, . . . , αr) + 2.
immediate corollary:
Peeksort incurs merge cost M (H + 2)n.
Peeksort needs C n + (H + 2)n cmps.
both are optimal up to O(n) terms
Peeksort exploits existing runs
optimally up to lower order terms!
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 7 / 15
132. Analysis of peeksort
Theorem (Horibe 1977, Bayer 1975:)
Weight-balancing on leaf probabilities
α1, . . . , αr yields a BST with search cost
exp. #cmps to find
random leaf chosen
with prob. αi.
C H(α1, . . . , αr) + 2.
immediate corollary:
Peeksort incurs merge cost M (H + 2)n.
Peeksort needs C n
detect runs
+ (H + 2)n cmps.
both are optimal up to O(n) terms
Peeksort exploits existing runs
optimally up to lower order terms!
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 7 / 15
133. Analysis of peeksort
Theorem (Horibe 1977, Bayer 1975:)
Weight-balancing on leaf probabilities
α1, . . . , αr yields a BST with search cost
exp. #cmps to find
random leaf chosen
with prob. αi.
C H(α1, . . . , αr) + 2.
immediate corollary:
Peeksort incurs merge cost M (H + 2)n.
Peeksort needs C n
detect runs
+ (H +
merge cost
2)n cmps.
both are optimal up to O(n) terms
Peeksort exploits existing runs
optimally up to lower order terms!
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 7 / 15
134. Analysis of peeksort
Theorem (Horibe 1977, Bayer 1975:)
Weight-balancing on leaf probabilities
α1, . . . , αr yields a BST with search cost
exp. #cmps to find
random leaf chosen
with prob. αi.
C H(α1, . . . , αr) + 2.
immediate corollary:
Peeksort incurs merge cost M (H + 2)n.
Peeksort needs C n
detect runs
+ (H +
merge cost
2)n cmps.
both are optimal up to O(n) terms
Peeksort exploits existing runs
optimally up to lower order terms!
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 7 / 15
135. Analysis of peeksort
Theorem (Horibe 1977, Bayer 1975:)
Weight-balancing on leaf probabilities
α1, . . . , αr yields a BST with search cost
exp. #cmps to find
random leaf chosen
with prob. αi.
C H(α1, . . . , αr) + 2.
immediate corollary:
Peeksort incurs merge cost M (H + 2)n.
Peeksort needs C n
detect runs
+ (H +
merge cost
2)n cmps.
both are optimal up to O(n) terms
Peeksort exploits existing runs
optimally up to lower order terms!
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 7 / 15
136. Analysis of peeksort
Theorem (Horibe 1977, Bayer 1975:)
Weight-balancing on leaf probabilities
α1, . . . , αr yields a BST with search cost
exp. #cmps to find
random leaf chosen
with prob. αi.
C H(α1, . . . , αr) + 2.
immediate corollary:
Peeksort incurs merge cost M (H + 2)n.
Peeksort needs C n
detect runs
+ (H +
merge cost
2)n cmps.
both are optimal up to O(n) terms
Peeksort exploits existing runs
optimally up to lower order terms!
Are we done then?
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 7 / 15
137. Analysis of peeksort
Theorem (Horibe 1977, Bayer 1975:)
Weight-balancing on leaf probabilities
α1, . . . , αr yields a BST with search cost
exp. #cmps to find
random leaf chosen
with prob. αi.
C H(α1, . . . , αr) + 2.
immediate corollary:
Peeksort incurs merge cost M (H + 2)n.
Peeksort needs C n
detect runs
+ (H +
merge cost
2)n cmps.
both are optimal up to O(n) terms
Peeksort exploits existing runs
optimally up to lower order terms!
Are we done then?
ŏ have to store lengths
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 7 / 15
138. Analysis of peeksort
Theorem (Horibe 1977, Bayer 1975:)
Weight-balancing on leaf probabilities
α1, . . . , αr yields a BST with search cost
exp. #cmps to find
random leaf chosen
with prob. αi.
C H(α1, . . . , αr) + 2.
immediate corollary:
Peeksort incurs merge cost M (H + 2)n.
Peeksort needs C n
detect runs
+ (H +
merge cost
2)n cmps.
both are optimal up to O(n) terms
Peeksort exploits existing runs
optimally up to lower order terms!
Are we done then?
ŏ have to store lengths
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 7 / 15
139. Analysis of peeksort
Theorem (Horibe 1977, Bayer 1975:)
Weight-balancing on leaf probabilities
α1, . . . , αr yields a BST with search cost
exp. #cmps to find
random leaf chosen
with prob. αi.
C H(α1, . . . , αr) + 2.
immediate corollary:
Peeksort incurs merge cost M (H + 2)n.
Peeksort needs C n
detect runs
+ (H +
merge cost
2)n cmps.
both are optimal up to O(n) terms
Peeksort exploits existing runs
optimally up to lower order terms!
Are we done then?
ŏ have to store lengths
ŏ extra scan to detect runs
one run at a time
we load runs (peeking)
without putting memory
transfers to good use
... can’t we do better?
Timsort does better:
newly detected run usually
merged soon after
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 7 / 15
140. Analysis of peeksort
Theorem (Horibe 1977, Bayer 1975:)
Weight-balancing on leaf probabilities
α1, . . . , αr yields a BST with search cost
exp. #cmps to find
random leaf chosen
with prob. αi.
C H(α1, . . . , αr) + 2.
immediate corollary:
Peeksort incurs merge cost M (H + 2)n.
Peeksort needs C n
detect runs
+ (H +
merge cost
2)n cmps.
both are optimal up to O(n) terms
Peeksort exploits existing runs
optimally up to lower order terms!
Are we done then?
ŏ have to store lengths
ŏ extra scan to detect runs
one run at a time
we load runs (peeking)
without putting memory
transfers to good use
... can’t we do better?
Timsort does better:
newly detected run usually
merged soon after
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 7 / 15
141. Analysis of peeksort
Theorem (Horibe 1977, Bayer 1975:)
Weight-balancing on leaf probabilities
α1, . . . , αr yields a BST with search cost
exp. #cmps to find
random leaf chosen
with prob. αi.
C H(α1, . . . , αr) + 2.
immediate corollary:
Peeksort incurs merge cost M (H + 2)n.
Peeksort needs C n
detect runs
+ (H +
merge cost
2)n cmps.
both are optimal up to O(n) terms
Peeksort exploits existing runs
optimally up to lower order terms!
Are we done then?
ŏ have to store lengths
ŏ extra scan to detect runs
one run at a time
we load runs (peeking)
without putting memory
transfers to good use
... can’t we do better?
Timsort does better:
newly detected run usually
merged soon after
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 7 / 15
142. Analysis of peeksort
Theorem (Horibe 1977, Bayer 1975:)
Weight-balancing on leaf probabilities
α1, . . . , αr yields a BST with search cost
exp. #cmps to find
random leaf chosen
with prob. αi.
C H(α1, . . . , αr) + 2.
immediate corollary:
Peeksort incurs merge cost M (H + 2)n.
Peeksort needs C n
detect runs
+ (H +
merge cost
2)n cmps.
both are optimal up to O(n) terms
Peeksort exploits existing runs
optimally up to lower order terms!
Are we done then?
ŏ have to store lengths
ŏ extra scan to detect runs
one run at a time
we load runs (peeking)
without putting memory
transfers to good use
... can’t we do better?
Timsort does better:
newly detected run usually
merged soon after
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 7 / 15
143. Analysis of peeksort
Theorem (Horibe 1977, Bayer 1975:)
Weight-balancing on leaf probabilities
α1, . . . , αr yields a BST with search cost
exp. #cmps to find
random leaf chosen
with prob. αi.
C H(α1, . . . , αr) + 2.
immediate corollary:
Peeksort incurs merge cost M (H + 2)n.
Peeksort needs C n
detect runs
+ (H +
merge cost
2)n cmps.
both are optimal up to O(n) terms
Peeksort exploits existing runs
optimally up to lower order terms!
Are we done then?
ŏ have to store lengths
ŏ extra scan to detect runs
one run at a time
we load runs (peeking)
without putting memory
transfers to good use
... can’t we do better?
Timsort does better:
newly detected run usually
merged soon after
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 7 / 15
144. Outline
1 Adaptive Sorting – Status Quo1 Adaptive Sorting – Status Quo
2 Natural Mergesort2 Natural Mergesort
3 Peeksort3 Peeksort
4 Powersort4 Powersort
5 Experiments5 Experiments
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 7 / 15
145. The bisection heuristic
Timsort
proceed left to right:
detect the next run
push it onto stack of runs
merge some
devil is in the details
runs from the
stack
cannot use weight-balancing
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 8 / 15
146. The bisection heuristic
Timsort
proceed left to right:
detect the next run
push it onto stack of runs
merge some
devil is in the details
runs from the
stack
cannot use weight-balancing
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 8 / 15
147. The bisection heuristic
Timsort
proceed left to right:
detect the next run
push it onto stack of runs
merge some
devil is in the details
runs from the
stack
cannot use weight-balancing
Method 2 : bisection
(Mehlhorn 1977)
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 8 / 15
148. The bisection heuristic
Timsort
proceed left to right:
detect the next run
push it onto stack of runs
merge some
devil is in the details
runs from the
stack
cannot use weight-balancing
Method 2 : bisection
(Mehlhorn 1977)
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 8 / 15
149. The bisection heuristic
Timsort
proceed left to right:
detect the next run
push it onto stack of runs
merge some
devil is in the details
runs from the
stack
cannot use weight-balancing
Method 2 : bisection
(Mehlhorn 1977)
1⁄2
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 8 / 15
150. The bisection heuristic
Timsort
proceed left to right:
detect the next run
push it onto stack of runs
merge some
devil is in the details
runs from the
stack
cannot use weight-balancing
Method 2 : bisection
(Mehlhorn 1977)
1⁄2
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 8 / 15
151. The bisection heuristic
Timsort
proceed left to right:
detect the next run
push it onto stack of runs
merge some
devil is in the details
runs from the
stack
cannot use weight-balancing
Method 2 : bisection
(Mehlhorn 1977)
1⁄2
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 8 / 15
152. The bisection heuristic
Timsort
proceed left to right:
detect the next run
push it onto stack of runs
merge some
devil is in the details
runs from the
stack
cannot use weight-balancing
Method 2 : bisection
(Mehlhorn 1977)
1⁄2
1⁄2
1⁄4
3
⁄4
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 8 / 15
153. The bisection heuristic
Timsort
proceed left to right:
detect the next run
push it onto stack of runs
merge some
devil is in the details
runs from the
stack
cannot use weight-balancing
Method 2 : bisection
(Mehlhorn 1977)
1⁄2
1⁄2
1⁄4
3
⁄4
weight-balancing
chose this!
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 8 / 15
154. The bisection heuristic
Timsort
proceed left to right:
detect the next run
push it onto stack of runs
merge some
devil is in the details
runs from the
stack
cannot use weight-balancing
Method 2 : bisection
(Mehlhorn 1977)
1⁄2
1⁄2
1⁄4
3
⁄4
weight-balancing
chose this!
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 8 / 15
155. The bisection heuristic
Timsort
proceed left to right:
detect the next run
push it onto stack of runs
merge some
devil is in the details
runs from the
stack
cannot use weight-balancing
Method 2 : bisection
(Mehlhorn 1977)
1⁄2
1⁄2
1⁄4
3
⁄4
weight-balancing
chose this!
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 8 / 15
156. The bisection heuristic
Timsort
proceed left to right:
detect the next run
push it onto stack of runs
merge some
devil is in the details
runs from the
stack
cannot use weight-balancing
Method 2 : bisection
(Mehlhorn 1977)
1⁄2
1⁄2
1⁄4
3
⁄4
weight-balancing
chose this!
1⁄2
1⁄4
3
⁄4
1⁄8
7
⁄8
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 8 / 15
157. The bisection heuristic
Timsort
proceed left to right:
detect the next run
push it onto stack of runs
merge some
devil is in the details
runs from the
stack
cannot use weight-balancing
Method 2 : bisection
(Mehlhorn 1977)
1⁄2
1⁄2
1⁄4
3
⁄4
weight-balancing
chose this!
1⁄2
1⁄4
3
⁄4
1⁄8
7
⁄8
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 8 / 15
158. The bisection heuristic
Timsort
proceed left to right:
detect the next run
push it onto stack of runs
merge some
devil is in the details
runs from the
stack
cannot use weight-balancing
Method 2 : bisection
(Mehlhorn 1977)
1⁄2
1⁄2
1⁄4
3
⁄4
weight-balancing
chose this!
1⁄2
1⁄4
3
⁄4
1⁄8
7
⁄8
split out of range!
no node created
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 8 / 15
159. The bisection heuristic
Timsort
proceed left to right:
detect the next run
push it onto stack of runs
merge some
devil is in the details
runs from the
stack
cannot use weight-balancing
Method 2 : bisection
(Mehlhorn 1977)
1⁄2
1⁄2
1⁄4
3
⁄4
weight-balancing
chose this!
1⁄2
1⁄4
3
⁄4
1⁄8
7
⁄8
split out of range!
no node created
1⁄2
1⁄4
3
⁄4
1⁄8
7
⁄8 15⁄16
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 8 / 15
160. The bisection heuristic
Timsort
proceed left to right:
detect the next run
push it onto stack of runs
merge some
devil is in the details
runs from the
stack
cannot use weight-balancing
Method 2 : bisection
(Mehlhorn 1977)
1⁄2
1⁄2
1⁄4
3
⁄4
weight-balancing
chose this!
1⁄2
1⁄4
3
⁄4
1⁄8
7
⁄8
split out of range!
no node created
1⁄2
1⁄4
3
⁄4
1⁄8
7
⁄8 15⁄16
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 8 / 15
161. The bisection heuristic
Timsort
proceed left to right:
detect the next run
push it onto stack of runs
merge some
devil is in the details
runs from the
stack
cannot use weight-balancing
Method 2 : bisection
(Mehlhorn 1977)
1⁄2
1⁄2
1⁄4
3
⁄4
weight-balancing
chose this!
1⁄2
1⁄4
3
⁄4
1⁄8
7
⁄8
split out of range!
no node created
1⁄2
1⁄4
3
⁄4
1⁄8
7
⁄8 15⁄16
Alternative view: node powers
inner node midpoint interval
= normalized interval
[1..n] → [0, 1]
power = min s.t.
contains c · 2−
depends only on 2 runs
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 8 / 15
162. The bisection heuristic
Timsort
proceed left to right:
detect the next run
push it onto stack of runs
merge some
devil is in the details
runs from the
stack
cannot use weight-balancing
Method 2 : bisection
(Mehlhorn 1977)
1⁄2
1⁄2
1⁄4
3
⁄4
weight-balancing
chose this!
1⁄2
1⁄4
3
⁄4
1⁄8
7
⁄8
split out of range!
no node created
1⁄2
1⁄4
3
⁄4
1⁄8
7
⁄8 15⁄16
Alternative view: node powers
inner node midpoint interval
= normalized interval
[1..n] → [0, 1]
power = min s.t.
contains c · 2−
depends only on 2 runs
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 8 / 15
163. The bisection heuristic
Timsort
proceed left to right:
detect the next run
push it onto stack of runs
merge some
devil is in the details
runs from the
stack
cannot use weight-balancing
Method 2 : bisection
(Mehlhorn 1977)
1⁄2
1⁄2
1⁄4
3
⁄4
weight-balancing
chose this!
1⁄2
1⁄4
3
⁄4
1⁄8
7
⁄8
split out of range!
no node created
1⁄2
1⁄4
3
⁄4
1⁄8
7
⁄8
44
33
44
22
44
33
44
11
44
33
44
22
44
33
44
15⁄16
Alternative view: node powers
inner node midpoint interval
= normalized interval
[1..n] → [0, 1]
power = min s.t.
contains c · 2−
depends only on 2 runs
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 8 / 15
164. The bisection heuristic
Timsort
proceed left to right:
detect the next run
push it onto stack of runs
merge some
devil is in the details
runs from the
stack
cannot use weight-balancing
Method 2 : bisection
(Mehlhorn 1977)
1⁄2
1⁄2
1⁄4
3
⁄4
weight-balancing
chose this!
1⁄2
1⁄4
3
⁄4
1⁄8
7
⁄8
split out of range!
no node created
1⁄2
1⁄4
3
⁄4
1⁄8
7
⁄8
44
33
44
22
44
33
44
11
44
33
44
22
44
33
44
15⁄16
3 2 1 2 4
Alternative view: node powers
inner node midpoint interval
= normalized interval
[1..n] → [0, 1]
power = min s.t.
contains c · 2−
depends only on 2 runs
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 8 / 15
165. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs
a b c d e f
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
166. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs
a b c d e f
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
167. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs
a b c d e f
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
168. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs
a b c d e f
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
169. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs
a b c d e f
3
run1 run2
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
170. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs
a b c d e f
3
run1 run2
a – 3
run stack
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
171. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs
a b c d e f
3 2
run1 run2
a – 3
run stack
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
172. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs
a b c d e f
3 2
run1 run2
a – 3
b – 2
run stack
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
173. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs
a b c d e f
3 2
run1 run2
a – 3
b – 2
run stack
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
174. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs
a b c d e f
3 2
run1 run2
a – 3
b – 2
run stack
merge
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
175. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs
ab c d e f
2
run2
ab – 2
run stack
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
176. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs
ab c d e f
2 1
run1 run2
ab – 2
run stack
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
177. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs
ab c d e f
2 1
run1 run2
ab – 2
c – 1
run stack
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
178. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs
ab c d e f
2 1
run1 run2
ab – 2
c – 1
run stack
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
179. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs
ab c d e f
2 1
run1 run2
ab – 2
c – 1
run stack
merge
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
180. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs
abc d e f
1
run2
abc – 1
run stack
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
181. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs
abc d e f
1 2
run1 run2
abc – 1
run stack
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
182. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs
abc d e f
1 2
run1 run2
abc – 1
d – 2
run stack
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
183. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs
abc d e f
1 2 4
run1
run2
abc – 1
d – 2
run stack
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
184. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs
abc d e f
1 2 4
run1
run2
abc – 1
d – 2
e – 4
run stack
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
185. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs
abc d e f
1 2 4
abc – 1
d – 2
e – 4
run stack
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
186. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs merge-down phase
abc d e f
1 2 4
abc – 1
d – 2
e – 4
run stack
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
187. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs merge-down phase
abc d e f
1 2 4
abc – 1
d – 2
e – 4
run stack
merge
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
188. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs merge-down phase
abc d ef
1 2
abc – 1
d – 2
run stack
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
189. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs merge-down phase
abc d ef
1 2
abc – 1
d – 2
run stack
merge
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
190. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs merge-down phase
abc def
1
abc – 1
run stack
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
191. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs merge-down phase
abc def
1
abc – 1
run stack
merge
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
192. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs
abcdef
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
193. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs
abcdef
Theorem (Mehlhorn 1977:)
The bisection heuristic yields a BST with
search cost C H(α1, . . . , αr) + 2.
same merge/cmps cost as Peeksort
exploit runs optimally up to lower
order terms!
but: detects runs lazily!
no extra scan!
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
194. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs
abcdef
Theorem (Mehlhorn 1977:)
The bisection heuristic yields a BST with
search cost C H(α1, . . . , αr) + 2.
same merge/cmps cost as Peeksort
exploit runs optimally up to lower
order terms!
but: detects runs lazily!
no extra scan!
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
195. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs
abcdef
Theorem (Mehlhorn 1977:)
The bisection heuristic yields a BST with
search cost C H(α1, . . . , αr) + 2.
same merge/cmps cost as Peeksort
exploit runs optimally up to lower
order terms!
but: detects runs lazily!
no extra scan!
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
196. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs
abcdef
Theorem (Mehlhorn 1977:)
The bisection heuristic yields a BST with
search cost C H(α1, . . . , αr) + 2.
same merge/cmps cost as Peeksort
exploit runs optimally up to lower
order terms!
but: detects runs lazily!
no extra scan!
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
197. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs
abcdef
Theorem (Mehlhorn 1977:)
The bisection heuristic yields a BST with
search cost C H(α1, . . . , αr) + 2.
same merge/cmps cost as Peeksort
exploit runs optimally up to lower
order terms!
but: detects runs lazily!
no extra scan!
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
198. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs
abcdef
Theorem (Mehlhorn 1977:)
The bisection heuristic yields a BST with
search cost C H(α1, . . . , αr) + 2.
same merge/cmps cost as Peeksort
exploit runs optimally up to lower
order terms!
but: detects runs lazily!
no extra scan!
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
199. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs
abcdef
Theorem (Mehlhorn 1977:)
The bisection heuristic yields a BST with
search cost C H(α1, . . . , αr) + 2.
same merge/cmps cost as Peeksort
exploit runs optimally up to lower
order terms!
but: detects runs lazily!
no extra scan!
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
200. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs
abcdef
Theorem (Mehlhorn 1977:)
The bisection heuristic yields a BST with
search cost C H(α1, . . . , αr) + 2.
same merge/cmps cost as Peeksort
exploit runs optimally up to lower
order terms!
but: detects runs lazily!
no extra scan!
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
201. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs
abcdef
Theorem (Mehlhorn 1977:)
The bisection heuristic yields a BST with
search cost C H(α1, . . . , αr) + 2.
same merge/cmps cost as Peeksort
exploit runs optimally up to lower
order terms!
but: detects runs lazily!
no extra scan!
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
202. Powersort
Powersort
proceed left to right:
detect next run & compute power
push run onto stack of runs
while new node less powerful:
merge topmost runs
abcdef
Theorem (Mehlhorn 1977:)
The bisection heuristic yields a BST with
search cost C H(α1, . . . , αr) + 2.
same merge/cmps cost as Peeksort
exploit runs optimally up to lower
order terms!
but: detects runs lazily!
no extra scan!
More good properties:
power efficient to compute;
O(1) with bitwise tricks (clz
count leading zeros
)
never stores more than lg n runs:
powers on stack strictly monotonic
(highest on top)
stack height max power lg n + 1
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
203. Outline
1 Adaptive Sorting – Status Quo1 Adaptive Sorting – Status Quo
2 Natural Mergesort2 Natural Mergesort
3 Peeksort3 Peeksort
4 Powersort4 Powersort
5 Experiments5 Experiments
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 9 / 15
206. Experimental Evaluation
Hypotheses:
1 Negligible overhead:
Peek- and powersort are as fast as standard mergesort on inputs with high H.
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 10 / 15
207. Experimental Evaluation
Hypotheses:
1 Negligible overhead:
Peek- and powersort are as fast as standard mergesort on inputs with high H.
2 Run-adaptiveness helps:
Adaptive methods are faster on inputs with low H.
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 10 / 15
208. Experimental Evaluation
Hypotheses:
1 Negligible overhead:
Peek- and powersort are as fast as standard mergesort on inputs with high H.
2 Run-adaptiveness helps:
Adaptive methods are faster on inputs with low H.
3 Timsort’s weak point:
Timsort is much slower than peek-/powersort on certain inputs.
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 10 / 15
209. Experimental Evaluation
Hypotheses:
1 Negligible overhead:
Peek- and powersort are as fast as standard mergesort on inputs with high H.
2 Run-adaptiveness helps:
Adaptive methods are faster on inputs with low H.
3 Timsort’s weak point:
Timsort is much slower than peek-/powersort on certain inputs.
Setup:
Java implementations, reproduced in C++
mildly hand-tuned code
sorting int[]s, length around 107
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 10 / 15
210. Negligible Overhead
1 Negligible overhead:
Peek- and powersort are as good as standard mergesort on inputs with high H.
Study: random permutations, Java runtimes
time
n lg n
C++
105
106
107
108
4
5
6
7
top-down mergesort
bottom-up mergesort
peeksort
powersort
Timsort
trotsort (Timsort w/o galloping)
Arrays.sort(int[]) (quicksort, not stable)
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 11 / 15
211. Negligible Overhead
1 Negligible overhead:
Peek- and powersort are as good as standard mergesort on inputs with high H.
Study: random permutations, Java runtimes
time
n lg n
C++
105
106
107
108
4
5
6
7
top-down mergesort
bottom-up mergesort
peeksort
powersort
Timsort
trotsort (Timsort w/o galloping)
Arrays.sort(int[]) (quicksort, not stable)
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 11 / 15
212. Negligible Overhead
1 Negligible overhead:
Peek- and powersort are as good as standard mergesort on inputs with high H.
Study: random permutations, Java runtimes
time
n lg n
C++
105
106
107
108
4
5
6
7
top-down mergesort
bottom-up mergesort
peeksort
powersort
Timsort
trotsort (Timsort w/o galloping)
Arrays.sort(int[]) (quicksort, not stable)
galloping merge too slow
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 11 / 15
213. Negligible Overhead
1 Negligible overhead:
Peek- and powersort are as good as standard mergesort on inputs with high H.
Study: random permutations, Java runtimes
time
n lg n
C++
105
106
107
108
4.2
4.4
4.6
top-down mergesort
bottom-up mergesort
peeksort
powersort
Timsort
trotsort (Timsort w/o galloping)
Arrays.sort(int[]) (quicksort, not stable)
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 11 / 15
214. Negligible Overhead
1 Negligible overhead:
Peek- and powersort are as good as standard mergesort on inputs with high H.
Study: random permutations, Java runtimes
time
n lg n
C++
105
106
107
108
4.2
4.4
4.6
top-down mergesort
bottom-up mergesort
peeksort
powersort
Timsort
trotsort (Timsort w/o galloping)
Arrays.sort(int[]) (quicksort, not stable)
no significant difference to standard mergesort
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 11 / 15
215. Negligible Overhead
1 Negligible overhead:
Peek- and powersort are as good as standard mergesort on inputs with high H.
Study: random permutations, Java runtimes
time
n lg n
C++
105
106
107
108
4.2
4.4
4.6
top-down mergesort
bottom-up mergesort
peeksort
powersort
Timsort
trotsort (Timsort w/o galloping)
Arrays.sort(int[]) (quicksort, not stable)
no significant difference to standard mergesort
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 11 / 15
216. Run-adaptiveness helps
2 Run-adaptiveness helps:
Adaptive methods are faster on inputs with low H.
Study: “random runs”: rp w/ ranges of Geo(1/
√
n) sorted, Java runtimes, n = 107 C++
≈
√
n runs, avg length
√
n
10% of runs < 0.1
√
n
5% of runs > 5
√
n
moderate presortedness
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 12 / 15
217. Run-adaptiveness helps
2 Run-adaptiveness helps:
Adaptive methods are faster on inputs with low H.
Study: “random runs”: rp w/ ranges of Geo(1/
√
n) sorted, Java runtimes, n = 107 C++
≈
√
n runs, avg length
√
n
10% of runs < 0.1
√
n
5% of runs > 5
√
n
moderate presortedness
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 12 / 15
218. Run-adaptiveness helps
2 Run-adaptiveness helps:
Adaptive methods are faster on inputs with low H.
Study: “random runs”: rp w/ ranges of Geo(1/
√
n) sorted, Java runtimes, n = 107 C++
≈
√
n runs, avg length
√
n
10% of runs < 0.1
√
n
5% of runs > 5
√
n
moderate presortedness
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 12 / 15
219. Run-adaptiveness helps
2 Run-adaptiveness helps:
Adaptive methods are faster on inputs with low H.
Study: “random runs”: rp w/ ranges of Geo(1/
√
n) sorted, Java runtimes, n = 107 C++
≈
√
n runs, avg length
√
n
10% of runs < 0.1
√
n
5% of runs > 5
√
n
moderate presortedness
500
600
700
time (ms)
top-down mergesort
bottom-up mergesort
peeksort
powersort
Timsort
trotsort (no galloping)
Arrays.sort(int[])
merge cost
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 12 / 15
220. Run-adaptiveness helps
2 Run-adaptiveness helps:
Adaptive methods are faster on inputs with low H.
Study: “random runs”: rp w/ ranges of Geo(1/
√
n) sorted, Java runtimes, n = 107 C++
≈
√
n runs, avg length
√
n
10% of runs < 0.1
√
n
5% of runs > 5
√
n
moderate presortedness
500
600
700
time (ms)
top-down mergesort
bottom-up mergesort
peeksort
powersort
Timsort
trotsort (no galloping)
Arrays.sort(int[])
merge cost
beat quicksort by 20%
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 12 / 15
221. Run-adaptiveness helps
2 Run-adaptiveness helps:
Adaptive methods are faster on inputs with low H.
Study: “random runs”: rp w/ ranges of Geo(1/
√
n) sorted, Java runtimes, n = 107 C++
≈
√
n runs, avg length
√
n
10% of runs < 0.1
√
n
5% of runs > 5
√
n
moderate presortedness
500
600
700
time (ms)
top-down mergesort
bottom-up mergesort
peeksort
powersort
Timsort
trotsort (no galloping)
Arrays.sort(int[])
merge cost
beat quicksort by 20%
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 12 / 15
222. Timsort’s weak point
3 Timsort’s weak point:
Timsort is much slower than peek-/powersort on certain inputs.
Study: “Timsort-drags”: known family of bad-case sequences
Rtim(n) by Buss & Knop 2018
L1, . . . , Lr
Java runtimes, n = 224 ≈ 1.6 · 107
C++
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 13 / 15
223. Timsort’s weak point
3 Timsort’s weak point:
Timsort is much slower than peek-/powersort on certain inputs.
Study: “Timsort-drags”: known family of bad-case sequences
Rtim(n) by Buss & Knop 2018
L1, . . . , Lr
Java runtimes, n = 224 ≈ 1.6 · 107
C++
1,200
1,400
1,600
1,800
2,000
time (ms)
td-mergesort
bu-mergesort
peeksort
powersort
Timsort
trotsort
Arrays.sort(int[])
0.8
0.9
1
1.1
1.2
1.3
merge costs (normalized)
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 13 / 15
224. Timsort’s weak point
3 Timsort’s weak point:
Timsort is much slower than peek-/powersort on certain inputs.
Study: “Timsort-drags”: known family of bad-case sequences
Rtim(n) by Buss & Knop 2018
L1, . . . , Lr
Java runtimes, n = 224 ≈ 1.6 · 107
C++
1,200
1,400
1,600
1,800
2,000
time (ms)
td-mergesort
bu-mergesort
peeksort
powersort
Timsort
trotsort
Arrays.sort(int[])
0.8
0.9
1
1.1
1.2
1.3
merge costs (normalized)
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 13 / 15
225. Timsort’s weak point
3 Timsort’s weak point:
Timsort is much slower than peek-/powersort on certain inputs.
Study: “Timsort-drags”: known family of bad-case sequences
Rtim(n) by Buss & Knop 2018
L1, . . . , Lr
Java runtimes, n = 224 ≈ 1.6 · 107
C++
1,200
1,400
1,600
1,800
2,000
time (ms)
td-mergesort
bu-mergesort
peeksort
powersort
Timsort
trotsort
Arrays.sort(int[])
0.8
0.9
1
1.1
1.2
1.3
merge costs (normalized)
Timsort/trotsort has
40% higher merge cost
10% higher running time in Java
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 13 / 15
226. Timsort’s weak point
3 Timsort’s weak point:
Timsort is much slower than peek-/powersort on certain inputs.
Study: “Timsort-drags”: known family of bad-case sequences
Rtim(n) by Buss & Knop 2018
L1, . . . , Lr
Java runtimes, n = 224 ≈ 1.6 · 107
C++
1,200
1,400
1,600
1,800
2,000
time (ms)
td-mergesort
bu-mergesort
peeksort
powersort
Timsort
trotsort
Arrays.sort(int[])
0.8
0.9
1
1.1
1.2
1.3
merge costs (normalized)
Timsort/trotsort has
40% higher merge cost
10% higher running time in Java
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 13 / 15
227. Timsort’s weak point
3 Timsort’s weak point:
Timsort is much slower than peek-/powersort on certain inputs.
Study: “Timsort-drags”: known family of bad-case sequences
Rtim(n) by Buss & Knop 2018
L1, . . . , Lr
Java runtimes, n = 224 ≈ 1.6 · 107
C++
1,200
1,400
1,600
1,800
2,000
time (ms)
td-mergesort
bu-mergesort
peeksort
powersort
Timsort
trotsort
Arrays.sort(int[])
0.8
0.9
1
1.1
1.2
1.3
merge costs (normalized)
Timsort/trotsort has
40% higher merge cost
10% higher running time in Java
40% higher running time in C++
Sebastian Wild Nearly-Optimal Mergesorts 2018-08-20 13 / 15