[HN Gopher] When would you ever want bubblesort? (2023)
___________________________________________________________________
When would you ever want bubblesort? (2023)
Author : atan2
Score : 28 points
Date : 2025-12-10 21:45 UTC (1 hours ago)
(HTM) web link (buttondown.com)
(TXT) w3m dump (buttondown.com)
| caycep wrote:
| I learned this from President Obama...
| beeforpork wrote:
| A: For small arrays. I would add: particularly if you need a
| stable sort algorithm, which is either complex (Block Sort) or
| uses O(n) space (Merge Sort).
| nick__m wrote:
| if you apply quicksort to 2^20 random integers, at some point
| you're sorting 2^17 8-integer subpartitions
|
| why not use an 8 wide optimal sort network for those 8 integers?
| observationist wrote:
| Embarrassingly parallel sort, lol.
| 13415 wrote:
| Well, I used Bubblesort to sort the results of lottery draws
| because it was very easy to implement.
| zitterbewegung wrote:
| Related to this is Timsort which combines merge sort and
| insertion sort https://en.wikipedia.org/wiki/Timsort
| aappleby wrote:
| Can confirm, have used bubble sort for incrementally sorting
| particles in a particle system and plants in a terrain renderer.
| jandrewrogers wrote:
| The only use I've seen is incrementally sorting large arrays
| during brute-force search of said arrays, since that is
| approximately free and brute-force search is pretty efficient and
| fast on modern CPUs. Set a "sorted" flag if/when the array is
| eventually sorted.
|
| The idea was that the vast majority of arrays in a large set are
| not searched often enough to justify the cost of sorting them and
| sorting is an expensive operation if you are computing on a
| deadline. You also don't always know which ones will be heavily
| searched ahead of time. Using bubblesort, only the heavily
| accessed arrays end up sorted but as a side-effect of search
| rather than having separate heuristics to decide when/what to
| sort.
| pestatije wrote:
| to compare other sort algos against it
| zeta0134 wrote:
| I used bubblesort on purpose in a game project. Specifically, to
| sort sprites in an NES game back to front, lazily, spending as
| few CPU cycles as possible. Bubblesort on the very small list (a
| dozen objects max), and early exit after the first swap. It
| _eventually_ completes, and that was just fine. It 's tiny,
| incredibly simple, and somewhat resilient to the list changing
| from frame to frame as objects spawn and despawn. Each partial
| sort makes some progress no matter what.
|
| A few other algorithms would have fit the bill just as well, but
| bubblesort is perfectly adequate, so that's what will likely
| ship. More complex algorithms end up losing out due to greater
| initial overhead or larger ROM size.
___________________________________________________________________
(page generated 2025-12-10 23:00 UTC)