什么时候你会想要使用冒泡排序?
When would you ever want bubblesort? (2023)

原始链接: https://buttondown.com/hillelwayne/archive/when-would-you-ever-want-bubblesort/

虽然软件工程缺乏*普适*规则,但许多原则几乎如此——例如,优先使用组合而非继承,或避免冒泡排序。然而,即使是这些“规则”也有例外。令人惊讶的是,冒泡排序,通常被认为效率低下,*可以*是有用的。 对于非常小的数组,冒泡排序优于快速排序等更复杂的算法,使其成为混合排序策略中的潜在优化方案。更独特的是,英伟达的研究表明冒泡排序在特定硬件上具有优势。 然而,其最具吸引力的用例在于游戏开发。冒泡排序快速、易于中断的步骤以及朝着有序状态的稳定进展,非常适合需要严格帧率限制内的增量排序场景——例如,根据距离对渲染对象进行排序,在这种情况下,*一定程度的*排序比没有排序更好。 此外,还存在一个利基应用,用于可视化动画排序过程,尽管更高效的预计算和动画方法可能更受欢迎。 最后,作者强调了对Quanta Magazine关于元复杂性的文章的贡献,该贡献源于对计算困难问题的讨论。

Hacker News新 | 过去 | 评论 | 提问 | 展示 | 工作 | 提交登录 什么时候你会想要冒泡排序? (buttondown.com/hillelwayne) 20 分,atan2 46 分钟前 | 隐藏 | 过去 | 收藏 | 4 评论 nick__m 15 分钟前 | 下一个 [–] 如果你将快速排序应用于 2^20 个随机整数,在某个时候你会排序 2^17 个 8 整数子分区。 为什么不使用一个 8 宽度的最优排序网络来对这些 8 个整数进行排序?回复 observationist 1 分钟前 | 父级 | 下一个 [–] 尴尬并行排序,哈哈。回复 beeforpork 16 分钟前 | 上一个 | 下一个 [–] A:对于小数组。 我补充一点:特别是如果你需要一个稳定的排序算法,它要么复杂(块排序),要么使用 O(n) 空间(归并排序)。回复 caycep 21 分钟前 | 上一个 [–] 我从奥巴马总统那里学到的...回复 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

There are very few universal rules in software engineering, but there are are a lot of near-universal principles. Things like "prefer composition to inheritance" is near-universal. I love finding the rare situations where these principles don't hold, like where you do want inheritance over composition. A similar near-universal principle is "don't use bubblesort". Some would even say it's a universal rule, with Donald Knuth writing "bubble sort seems to have nothing to recommend it, except a catchy name and the fact that it leads to some interesting theoretical problems". But Knuth's been wrong before, so let's see if this universal rule is only near-universal.

Theoretically, bubblesort is faster than quick or mergesort for small arrays. This makes it useful as part of a larger sorting strategy: most of the fast-in-principle sorting algorithms work by recursively sorting subpartitions of an array, ie if you apply quicksort to 2^20 random integers, at some point you're sorting 2^17 8-integer subpartitions. Switching over to bubblesort for those subpartitions would be a nice optimization.

Many production sorting algorithms do use a hybrid approach, but they overwhelmingly use insertion sort instead. Insertion sort is very fast for small arrays and it's also better at using the hardware. On some very particular hardwares bubblesort stills ends up better, like in this NVIDIA study, but you probably don't have that hardware.

So that's one use-case, albeit one still dominated by a different algorithm. It's interesting that NVIDIA used it here because gamedev has a situation that's uniquely useful to bubblesort, based on two of its properties:

  1. While the algorithm is very slow overall, each individual step is very fast and easily suspendable.
  2. Each swap leaves the array more ordered than it was before. Other sorts can move values away from their final positions in intermediate stages.

This makes it really good when you want to do a fixed amount of sorting work per frame. Say you have a bunch of objects on a screen, where some objects can occlude others. You want to render the objects closest to the camera first because then you can determine which objects it hides, and then save time rendering those objects. There's no correctness cost for rendering objects out of order, just a potential performance cost. So while your array doesn't need to be ordered, the more ordered it is the happier you are. But you also can't spend too much time running a sorting algorithm, because you have a pretty strict realtime constraint. Bubble sort works pretty well here. You can run it a little bit of a time at each frame and get a better ordering than when you started.

That reminds me of one last use-case I've heard, apocryphally. Let's say you have a random collection of randomly-colored particles, and you want to animate them sorting into a rainbow spectrum. If you make each frame of the animation one pass of bubblesort, the particles will all move smoothly into the right positions. I couldn't find any examples in the wild, so with the help of GPT4 I hammered out a crappy visualization. Code is here, put it here.

(After doing that I suspect this isn't actually done in practice, in favor of running a better sort to calculate each particles final displacement and then animating each particles moving directly, instead of waiting to move for each bubblesort pass. I haven't mocked out an example but I think that'd look a lot smoother.)

So there you go, three niche use cases for bubblesort. You'll probably never need it.


New Quanta Article!

Okay so I didn't actually write this one, but I played a role in it happening! A while back a friend visited, and we were chatting about his job at quanta. At the time he was working on this mammoth article on metacomplexity theory, so naturally the topic of problems harder than NP-complete came up and I recommend he check out Petri net reachability. So he did, and then he wrote An Easy-Sounding Problem Yields Numbers Too Big for Our Universe. Gosh this is so exciting!

联系我们 contact @ memedata.com