Top
Best
New

Posted by atan2 12/10/2025

When would you ever want bubblesort? (2023)(buttondown.com)
108 points | 86 commentspage 2
Findecanor 12/11/2025|
I've used bubblesort in a coding interview, because it was the easiest to remember and get correct on a whiteboard in short time.-
sureglymop 12/11/2025|
Reminds me of an interview I had a while ago. The interviewer in all seriousness asked me to code up a sorting algorithm on the whiteboard. He was more of a business person than technical so was probably thinking of insertion, selection and bubblesort.

I said sure, quicksort, mergesort or radixsort?

He just said "okay, let's skip to the next question". :)

arethuza 12/11/2025||
I'd be tempted to ask under what circumstances they'd expect me to be coding a sorting routine... Seems a bit like asking an accountant to add two 10 digit numbers.
zitterbewegung 12/10/2025||
Related to this is Timsort which combines merge sort and insertion sort https://en.wikipedia.org/wiki/Timsort
dspillett 12/10/2025||
For small sets, or small-ish sets when you are coding quick, don't have a convenient standard library sort to hand, and are prioritising correctness over absolute performance.

Though in reality almost never: you almost always have a convenient built-in sort that is as quick & easy to use (likely quicker & easier), and in circumstances where the set is small enough for bubblesort to be just fine, the speed, memory use, or other properties of what-ever other sort your standard library uses aren't going to be a problem either.

As others have pointed out, sometimes it is useful for partial sorts due to the “always no less sorted than before at any point in the process (assuming no changes due to external influence)” property.

wrt:

> 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,

There are hundreds of sort demos out there, both live running and on publicly hosted videos, that show the final positions by hue, getting this effect. Seems odd that they couldn't find a single one.

EDIT: actually, I can't find any of the rainbow based sort demos I was thinking of, a lot of promising links seem dead. I take back my little moan!

aappleby 12/10/2025||
Can confirm, have used bubble sort for incrementally sorting particles in a particle system and plants in a terrain renderer.
ErroneousBosh 12/10/2025||
If you need a stable sort, can't be bothered finding a massive oversize library to link to, and only need to sort a relatively small number of objects on a system that's resource-constrained, I'm guessing?
thomasmg 12/11/2025|
I'm surprised that the simple, ~80 lines version of stable-in-place merge sort (see link in the above comments) is not more widely known. It is O(n log n log n) and not all that hard to implement.
another_twist 12/11/2025||
When the array is almost sorted. Bubble sort complexity is linear + inversions so if the inversions are low (the more sorted the array the lower the number of inversions), bubble sort is close to a linear pass.
CyLith 12/11/2025||
When sorting eigenpairs of a dense matrix, usually tou end up with a Schur decomposition. The basic operation that you can do is swap two adjacent eigenvalues on the diagonal, so bubblesort is a natural candidate.
AnimalMuppet 12/11/2025||
In all your big-O analyses, remember: n = 3 more often than you think. n = 12 a lot more often than you think. If that's your case, there's nothing wrong with bubble sort unless you have very tight performance constraints.

Worse, big-O always hides a constant factor. What's bubblesort's constant? What's quicksort's? It wouldn't surprise me if, for small enough n (2 or 3, and maybe a bit higher), bubblesort is actually faster.

Note well: I have not actually benchmarked this.

Also note well: Determine what your n is; don't assume that it's either large or small.

caycep 12/10/2025|
I learned this from President Obama...
sdsd 12/10/2025|
For the downvoters, he's referring to this instance when (then) Senator Obama jokingly referenced bubble sort during this Google event: https://www.youtube.com/watch?v=koMpGeZpu4Q

It was one of the many viral moments during Obama's original campaign where he seemed cool and in touch.

yunnpp 12/11/2025|||
Of course, being able to call out a sorting algorithm is the kind of thing that makes you very "cool and in touch" in the grander scheme of things, unlike, say, playing sports or lifting 500lbs.
meindnoch 12/11/2025|||
It was also when Google seemed cool and in touch.
More comments...