什么是泡沫排序适合?

Jas*_*ker 45 language-agnostic sorting algorithm bubble-sort

泡泡种类有任何现实世界的用途吗?每当我看到一个提到的,它总是要么:

  1. 用于学习的排序算法.
  2. 使用排序算法的示例.

Jer*_*fin 75

Bubble sort is (provably) the fastest sort available under a very specific circumstance. It originally became well known primarily because it was one of the first algorithms (of any kind) that was rigorously analyzed, and the proof was found that it was optimal under its limited circumstance.

Consider a file stored on a tape drive, and so little random access memory (or such large keys) that you can only load two records into memory at any given time. Rewinding the tape is slow enough that doing random access within the file is generally impractical -- if possible, you want to process records sequentially, no more than two at a time.

Back when tape drives were common, and machines with only a few thousand (words|bytes) of RAM (of whatever sort) were common, that was sufficiently realistic to be worth studying. That circumstance is now rare, so studying bubble sort makes little sense at all -- but even worse, the circumstance when it's optimal isn't taught anyway, so even when/if the right situation arose, almost nobody would realize it.

As far as being the fastest on an extremely small and/or nearly sorted set of data, while that can cover up the weakness of bubble sort (to at least some degree), an insertion sort will essentially always be better for either/both of those.

  • @gen我相信定义的限制是这样的:当顺序访问比随机访问快得多时,冒泡排序很好,并且你只能在内存中保留两个对象.使用磁带驱动器,它*机械地*已经顺序移动,因此您可以在执行操作时尽可能多地工作,而不会减慢/停止/反转磁带机. (5认同)
  • @Mark:哦,是的 - 放松几乎*任何*的限制,Bubblesort几乎总会失败,而且通常非常糟糕. (3认同)
  • 但是,如果您可以保留多余的磁带,则合并排序仍然可以胜任。 (2认同)

Rem*_*arp 43

这取决于您的数据分发方式 - 如果您可以做出一些假设.

我发现要了解何时使用冒泡排序的最佳链接之一 - 或者其他类型,这是一个关于排序算法的动画视图:

http://www.sorting-algorithms.com/

  • http://www.sorting-algorithms.com/也有一些很好的动画! (10认同)
  • 我真的很喜欢那个动画!根据它,看起来贝壳类型对于50号大小最好. (2认同)

Bil*_*ard 19

它在现实世界中并没有得到很多应用.这是一个很好的学习工具,因为它易于理解和快速实施.它具有不良(O(n ^ 2))最坏情况和平均性能.当您知道数据几乎已经排序时,它具有良好的最佳案例性能,但是有许多其他算法具有此属性,具有更好的最差和平均案例性能.

  • 我实际上发现在插入或选择排序之前(经常)教授泡泡排序是令人惊讶的.我发现这些都非常直观.除非我弄错了,否则大多数人在整理扑克牌时会做一个或另一个.冒泡排序需要多一点思考. (10认同)

wor*_*ad3 10

最近我在一个优化轶事中发现了它的一个很好的用途.一个程序需要一组精灵按每个帧的深度顺序排序.spites顺序在帧之间不会有太大变化,因此作为优化,它们通过每帧一次通过进行冒泡排序.这是在两个方向(从上到下和从下到上)完成的.因此,精灵总是几乎用非常有效的O(N)算法进行排序.

  • 实际上插入排序仍然更好.许多实时渲染系统对非常大的事物列表使用插入排序,因为事物往往对每个帧"几乎"排序.冒泡排序非常相似. (6认同)
  • @TM我相信你错过了每帧*两个固定传球*的位置.它最终将被排序,但可能需要几(100)帧.每帧插入排序的单次传递将确保第一个(或最后一个)项目位于正确的位置.气泡将使所有的精灵朝着正确的位置移动. (2认同)

Cri*_*rdo 7

对于小型套装来说,它可能是最快的.

说到教育.链接到排序排序的最后一个场景,这太棒了.必看.

  • +1让我喊"GO QUICKSORT GO!" 这是我生命中的第一次. (3认同)
  • 不它不是.它不应该像初学者那样接受初学程序员的教育. (2认同)