TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Quadsort: a stable non-recursive merge sort

312 pointsby geophertzover 5 years ago

16 comments

nightcrackerover 5 years ago
C&#x27;s qsort() is notoriously bad because the comparison function isn&#x27;t inlined, meaning the majority of time is spent in call overhead.<p>Zhangxp1998 ported quadsort to C++ and compared with std::sort and found it to be slower: <a href="https:&#x2F;&#x2F;gist.github.com&#x2F;zhangxp1998&#x2F;0e2fa30656c894017d183e0dbf2d862a" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;zhangxp1998&#x2F;0e2fa30656c894017d183e0d...</a><p>Shameless self promotion, if you want a faster sorting implementation, check out pdqsort (<a href="https:&#x2F;&#x2F;github.com&#x2F;orlp&#x2F;pdqsort" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;orlp&#x2F;pdqsort</a>) which is almost twice as fast as libstdc++&#x27;s std::sort for randomly shuffled integers, and has all sorts of tricks for input distributions with patterns (like pre-sorted, reverse sorted, single out-of-order element appended, etc).
评论 #22325442 未加载
评论 #22326892 未加载
nneonneoover 5 years ago
How does this fare against Python&#x27;s famous Timsort (used by several languages and systems)? How about the dual-pivot quicksort used by Java for primitive arrays?<p>Someone has to have put together a nice benchmark for comparing many sorting algorithms. I wish that the author had done some benchmarking first, so that the proposed algorithm can properly be positioned w.r.t. state-of-the-art techniques.
评论 #22327349 未加载
评论 #22326967 未加载
评论 #22324243 未加载
jphover 5 years ago
Summary: this is a non-recursive merge sort with improvements.<p>Benchmark of quadsort() versus C qsort():<p>* ~10x faster on forward-order items<p>* ~2x faster on reverse-order items<p>* ~equivalent on random-order items<p>Improvements:<p>* Ordering: when blocks of items are in order, or in reverse-order, then do special case handling, which gives quadsort O(n + log n) instead of qsort O(n * log n).<p>* Boundaries: compare data rather than traditional merge sort wasteful boundary checks.
评论 #22324157 未加载
评论 #22324276 未加载
xuchengover 5 years ago
Quick sort is not the fastest sorting algorithm. It would be nice if there is a benchmark comparing with other state of the art algorithms like Timsort.
评论 #22325857 未加载
proc0over 5 years ago
Interesting, but I&#x27;m surprised if this is the first time we have sorting algorithm that is swapping more than two elements at a time. I would have guessed every possible iteration of sorting algorithms has been already explored, proven and tested.
评论 #22323430 未加载
评论 #22323287 未加载
评论 #22323412 未加载
评论 #22324386 未加载
评论 #22324415 未加载
kccqzyover 5 years ago
This reminds me of a programming exercise I was asked to write when I first learned programming: write a sorting program generator that given N, generates a program that sorts an array of N elements optimally: the generated code has N! branches, one for each possible permutation. With some CSE help from the compiler, it can be really quite fast at the expense of code size.<p>The author&#x27;s explanation isn&#x27;t entirely clear, but it seems similar to the above construction with a fixed N and then a merge sort afterwards.
评论 #22324988 未加载
评论 #22323755 未加载
xeeeeeeeeeeenuover 5 years ago
In his benchmark, the author is assuming that qsort() is implemented using quicksort, but that&#x27;s not necessarily true. For example, glibc is using mergesort (although it falls back to quicksort if the system is short on memory).
评论 #22324517 未加载
zhangxp1998over 5 years ago
qsort has to invoke your comparison function repeatedly, which incurs a lot of overhead. Try C++&#x27;s std::sort
评论 #22323500 未加载
评论 #22324865 未加载
Naacover 5 years ago
Since we&#x27;re already using O(N) space, it would be interesting to see how this compares to Radix sort[0], which is O(N) space but O(N) time ( due to just hashing everything ).<p>Like others have said, it would be cool to see quadsort stacked up to other current state-of-the-art sorting algorithms.<p>[0] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Radix_sort" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Radix_sort</a>
评论 #22325272 未加载
QuinnWiltonover 5 years ago
I&#x27;ve never seen a sorting algorithm that uses a non-binary comparison function to order values. Is that a novel technique?<p>It seems really obvious in hindsight, so I&#x27;m sure there&#x27;s just prior art I don&#x27;t know about.
评论 #22323279 未加载
评论 #22324181 未加载
评论 #22323271 未加载
loegover 5 years ago
Seems it&#x27;s mergesort but with a slightly more complicated comparison primitive.
评论 #22323262 未加载
chkasover 5 years ago
This Quicksort is almost twice as fast<p><a href="https:&#x2F;&#x2F;rextester.com&#x2F;XHCGA23293" rel="nofollow">https:&#x2F;&#x2F;rextester.com&#x2F;XHCGA23293</a>
评论 #22324980 未加载
thdespouover 5 years ago
I&#x27;m a bit sceptical because I don&#x27;t see any mathematical proof. Only benchmarks which do not prove a lot. It may be faster only by a constant factor on a particular machine but for sufficiently large n it would be as fast as mergesort.<p>1000000 is peanuts. We need to see convergence for about billions+ of numbers.
评论 #22325609 未加载
aliabdover 5 years ago
Man, that&#x27;s a good gif
评论 #22323550 未加载
WillyNoursonover 5 years ago
Oh lord it&#x27;s almost 1k long
评论 #22326996 未加载
ummonkover 5 years ago
I’ll read the article when I get the chance, but would this be in-place?
评论 #22323341 未加载
评论 #22325063 未加载