Elahe Ghasemi, Vincent Jugé, Ghazal Khalighinejad, Helia Yazdanyar
{"title":"Galloping in Fast-Growth Natural Merge Sorts","authors":"Elahe Ghasemi, Vincent Jugé, Ghazal Khalighinejad, Helia Yazdanyar","doi":"10.1007/s00453-024-01285-6","DOIUrl":null,"url":null,"abstract":"<div><p>We study the impact of merging routines in merge-based sorting algorithms. More precisely, we focus on the <i>galloping</i> routine that TimSort uses to merge monotonic sub-arrays, hereafter called <i>runs</i>, and on the impact on the number of element comparisons performed if one uses this routine instead of a naïve merging routine. This routine was introduced in order to make TimSort more efficient on arrays with few distinct values. Alas, we prove that, although it makes TimSort sort array with two values in linear time, it does not prevent TimSort from requiring up to <span>\\(\\Theta (n \\log (n))\\)</span> element comparisons to sort arrays of length <i>n</i> with three distinct values. However, we also prove that slightly modifying TimSort ’s galloping routine results in requiring only <span>\\(\\mathcal {O}(n + n \\log (\\sigma ))\\)</span> element comparisons in the worst case, when sorting arrays of length <i>n</i> with <span>\\(\\sigma \\)</span> distinct values. We do so by focusing on the notion of <i>dual runs</i>, which was introduced in the 1990s, and on the associated <i>dual run-length entropy</i>. This notion is both related to the number of distinct values and to the number of runs in an array, which came with its own <i>run-length entropy</i> that was used to explain TimSort ’s otherwise “supernatural” efficiency. We also introduce new notions of <i>fast-</i> and <i>middle-growth</i> for natural merge sorts (i.e., algorithms based on merging runs), which are found in several sorting algorithms similar to TimSort. We prove that algorithms with the fast- or middle-growth property, provided that they use our variant of TimSort ’s galloping routine for merging runs, are as efficient as possible at sorting arrays with low run-induced or dual-run-induced complexities.\n</p></div>","PeriodicalId":50824,"journal":{"name":"Algorithmica","volume":"87 2","pages":"242 - 291"},"PeriodicalIF":0.9000,"publicationDate":"2024-12-09","publicationTypes":"Journal Article","fieldsOfStudy":null,"isOpenAccess":false,"openAccessPdf":"","citationCount":"0","resultStr":null,"platform":"Semanticscholar","paperid":null,"PeriodicalName":"Algorithmica","FirstCategoryId":"94","ListUrlMain":"https://link.springer.com/article/10.1007/s00453-024-01285-6","RegionNum":4,"RegionCategory":"计算机科学","ArticlePicture":[],"TitleCN":null,"AbstractTextCN":null,"PMCID":null,"EPubDate":"","PubModel":"","JCR":"Q4","JCRName":"COMPUTER SCIENCE, SOFTWARE ENGINEERING","Score":null,"Total":0}
引用次数: 0
Abstract
We study the impact of merging routines in merge-based sorting algorithms. More precisely, we focus on the galloping routine that TimSort uses to merge monotonic sub-arrays, hereafter called runs, and on the impact on the number of element comparisons performed if one uses this routine instead of a naïve merging routine. This routine was introduced in order to make TimSort more efficient on arrays with few distinct values. Alas, we prove that, although it makes TimSort sort array with two values in linear time, it does not prevent TimSort from requiring up to \(\Theta (n \log (n))\) element comparisons to sort arrays of length n with three distinct values. However, we also prove that slightly modifying TimSort ’s galloping routine results in requiring only \(\mathcal {O}(n + n \log (\sigma ))\) element comparisons in the worst case, when sorting arrays of length n with \(\sigma \) distinct values. We do so by focusing on the notion of dual runs, which was introduced in the 1990s, and on the associated dual run-length entropy. This notion is both related to the number of distinct values and to the number of runs in an array, which came with its own run-length entropy that was used to explain TimSort ’s otherwise “supernatural” efficiency. We also introduce new notions of fast- and middle-growth for natural merge sorts (i.e., algorithms based on merging runs), which are found in several sorting algorithms similar to TimSort. We prove that algorithms with the fast- or middle-growth property, provided that they use our variant of TimSort ’s galloping routine for merging runs, are as efficient as possible at sorting arrays with low run-induced or dual-run-induced complexities.
期刊介绍:
Algorithmica is an international journal which publishes theoretical papers on algorithms that address problems arising in practical areas, and experimental papers of general appeal for practical importance or techniques. The development of algorithms is an integral part of computer science. The increasing complexity and scope of computer applications makes the design of efficient algorithms essential.
Algorithmica covers algorithms in applied areas such as: VLSI, distributed computing, parallel processing, automated design, robotics, graphics, data base design, software tools, as well as algorithms in fundamental areas such as sorting, searching, data structures, computational geometry, and linear programming.
In addition, the journal features two special sections: Application Experience, presenting findings obtained from applications of theoretical results to practical situations, and Problems, offering short papers presenting problems on selected topics of computer science.