MHN
Add a comment on Hacker News →
4 of 55 comments
  1. Paper about this with proof of how/why it works: https://arxiv.org/pdf/2110.01111, which says:

    “There is nothing good about this algorithm. It is slow – the algorithm obviously runs in Θ(n2) time, whether worst-case, average-case or best-case. It unnecessarily compares all pairs of positions, twice (but see Section 3). There seems to be no intuition behind it, and its correctness is not entirely obvious. You certainly do not want to use it as a first example to introduce students to sorting algorithms. It is not stable, does not work well for external sorting, cannot sort inputs arriving online, and does not benefit from partially sorted inputs. Its only appeal may be its simplicity, in terms of lines of code and the “symmetry” of the two loops.”

  2. I can't believe some people can read grey text on slightly lighter grey background

  3. It's more confusing in your head than what the code looks like.

  4. It's hard to imagine how many times this sort must've been written by accident (see the infinite monkey theorem). Imagine a CS student typing this while not entirely knowing what they're doing, presenting it to their instructor, getting told that the code is wrong and changing it to a proper bubble or selection sort without suspecting a thing.