TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Hoare’s Rebuttal and Bubble Sort’s Comeback

103 点作者 blopeur超过 3 年前

10 条评论

samatman超过 3 年前
&gt; <i>the final surprise is that Bubble Sort takes the crown for small arrays</i><p>This didn&#x27;t need to be a surprise! It was a commonplace bit of wisdom in the microcomputer era, when sorting always came with questions about the application. You can&#x27;t beat it for locality— but you also can&#x27;t beat it for mostly-sorted, small collections.<p>Rather than sorting arrays, the area where bubblesort shines (ok. where bubblesort can still be considered!) is <i>keeping linked lists sorted</i>, especially where the sort order is important rather than critical. So event queues: you want to float the highest priority to the top, but it&#x27;s ok if occasionally #2 is launched before #1, because events aren&#x27;t guaranteed an execution order. We&#x27;re using an intrusive linked list because the scheduler has to take what it&#x27;s given, it can&#x27;t lay out memory.<p>Also, any time spent tinkering with an event queue is wasted time. So every round, walk the event queue and perform one (1) bubble sort. This takes the time it takes to traverse the list, effectively. So if you&#x27;ve placed exactly one event at the end of the queue (head of the list), it ends up precisely where it should be. Two? You leave one of them behind for the next pass.<p>It&#x27;s easy to reason out that when the sort starts performing badly, the problem you have is event congestion, not a poor big-O complexity for your queue sort.<p>Bubblesort is sometimes treated as a pessimal joke like bogosort, it isn&#x27;t, it&#x27;s just that the reason it&#x27;s treated as a basic sort pedagogically has been lost, as the profession&#x27;s center of gravity moves away from these kinds of system-level constructs.
评论 #30138498 未加载
joosters超过 3 年前
...<i>It’s easy to see that the above loop can be implemented without conditional branches. The conditional swapping can be implemented by conditional moves and the conditional pointer increase could be implemented as a unconditional</i><p><pre><code> p += (*it &lt; pivot) </code></pre> Does that really guarantee that it is branchless? Surely a compiler is free to turn that into a comparison and jump?<p>Or, to put it another way, the compiler was also free to turn the original &#x27;if&#x2F;then&#x27; structure into branchless code using conditional instructions. My point is, surely neither version <i>guarantees</i> that the generated code is branchless, it&#x27;s all still up to the compiler.
评论 #30138643 未加载
评论 #30135511 未加载
评论 #30139011 未加载
评论 #30134912 未加载
FrozenVoid超过 3 年前
Why not use combsort, which is generally does similar swaps and outperforms bubble sort in all cases? <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Comb_sort" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Comb_sort</a> <a href="https:&#x2F;&#x2F;www.geeksforgeeks.org&#x2F;comb-sort&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.geeksforgeeks.org&#x2F;comb-sort&#x2F;</a>
JohnHaugeland超过 3 年前
This is well known to game developers, and common advice in 3d engines for sorting for z-culling.<p>You can look this advice up in a raft of 1990s books when we were implementing our own 3d, eg Michael Abrash&#x27;s Graphics Programming Black Book
评论 #30135387 未加载
CJefferson超过 3 年前
So, there are a few reasons it isn&#x27;t fair to compare to std::sort.<p>The C++ standard requires that std::sort work for &quot;move-only types&quot;, so you can&#x27;t copy the pivot. I suspect this is where quite a bit of the slowdown happens -- as the pivot has to be taken by reference&#x2F;pointer, the compiler optimises worse.<p>Could std::sort implementations optimise for when the pivot can be copied? Certainly! Particularly when it is a &quot;small value&quot; like this. I worked on this years ago for libstdc++ (g++&#x27;s std::sort), but it turned out to make the code really horrible, so it was never merged in.<p>std::sort implementations already use an alternative sort (usually insertion sort) for small sized lists.
评论 #30138828 未加载
评论 #30138922 未加载
chkas超过 3 年前
A while ago I was tinkering with Quicksort and avoiding branch mispredictions.<p><a href="https:&#x2F;&#x2F;easylang.online&#x2F;blog&#x2F;qsort_c.html" rel="nofollow">https:&#x2F;&#x2F;easylang.online&#x2F;blog&#x2F;qsort_c.html</a><p>My implementation is pretty fast. At a size of 40 or 50, I switch to Insertion sort, and there the branchless bubblesort is significantly slower (I just tried it).<p>But I have to admit defeat to this <i>Sample sort</i>:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;SaschaWitt&#x2F;ips4o" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;SaschaWitt&#x2F;ips4o</a>
ogogmad超过 3 年前
Bubblesort is usually the worst sorting algorithm, in every conceivable way. Given that in a sorting network, bubble sort is dual to insertion sort [1], why can&#x27;t the optimisations in the blog post be applied to insertion sort? I believe they can be, and the author was just being facetious.<p>Stop teaching bubble sort.<p>[1] - <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Sorting_network#Insertion_and_Bubble_networks" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Sorting_network#Insertion_and_...</a>
评论 #30136283 未加载
评论 #30137303 未加载
评论 #30136152 未加载
评论 #30136088 未加载
评论 #30136091 未加载
评论 #30136521 未加载
评论 #30136891 未加载
lmilcin超过 3 年前
Not a surprise, really.<p>I have battled so many times with smug novice &quot;engineers&quot;, fresh from their CS courses, pointing, without even thinking twice, O(n^2) or even O(n^3) algorithms as errors on code reviews. This without taking into account the size of the input or the underlying machine executing the code.<p>It is my constant source of amazement -- to interview yet another candidate who can write any number of sorting algorithms from memory on a whiteboard but who can&#x27;t tell whether a linked list or an array list insertion is going to be faster for a given application.
评论 #30139113 未加载
gnufx超过 3 年前
Perhaps <a href="https:&#x2F;&#x2F;tug.org&#x2F;svn&#x2F;texlive&#x2F;trunk&#x2F;Build&#x2F;source&#x2F;texk&#x2F;dvipsk&#x2F;afm2tfm.c?revision=61654&amp;view=markup#l974" rel="nofollow">https:&#x2F;&#x2F;tug.org&#x2F;svn&#x2F;texlive&#x2F;trunk&#x2F;Build&#x2F;source&#x2F;texk&#x2F;dvipsk&#x2F;a...</a> has value at the Bank of Sans Serriffe.
kleiba超过 3 年前
Anyone interested in the ins and outs of Quicksort should have a look at this PhD thesis: <a href="https:&#x2F;&#x2F;www.wild-inter.net&#x2F;publications&#x2F;wild-2016.pdf" rel="nofollow">https:&#x2F;&#x2F;www.wild-inter.net&#x2F;publications&#x2F;wild-2016.pdf</a>
评论 #30140024 未加载