Live data from Hacker News

When would you ever want bubblesort?

buttondown.email

61–70 of 70 posts

Re: When would you ever want bubblesort?

#61

Bubblesort is hard to beat if you're optimizing for object code size. I'm surprised that wasn't mentioned.

I was expecting that, too. The one person I’ve heard say they’d implemented bubble sort in the current century worked on automotive controllers where they had absurdly tight memory budgets and the data he had to sort was relatively small so they quite happily traded a few cycles for the smaller executable.

Re: When would you ever want bubblesort?

#62
post #58
post #13

Earlier quoted context omitted.

> I was nearly-always sorting In my opinion, a bubble sort is a solid choice for that sort of use case.

For <4 elements, a pure "if" / "else" implementation would probably be optimal.

But more tedious and somewhat more error-prone to write by hand.

Re: When would you ever want bubblesort?

#63
Before I knew anything about sorting algorithms, I invented my own algorithm to sort the high score list in my little BASIC game. It was very simple: Find the highest score, swap it to the top and continue, this time starting with the second item, until done. I imagine that if I ever find myself in need of writing a sorting algorithm from memory, I'll still go with this one.

Re: When would you ever want bubblesort?

#64
post #23
post #13

Earlier quoted context omitted.

> I was nearly-always sorting In my opinion, a bubble sort is a solid choice for that sort of use case.

Are you being cheeky? Cause I would think insertion sort would be a more than reasonable alternative

Is everyone here feeling a bit... devilish?

I would pick a standard library sort function at that point.

Re: When would you ever want bubblesort?

#65

Why bubble sort when a slight modification can create a still simple but much more respectable algorithm called "comb sort". https://www.delftstack.com/tutorial/algorithm/comb-sort/

Comb sort tip --- instead of dividing by 1.3 use 1.333 (repeating) instead.

In practical terms using integers, this makes almost no difference *except* it can be performed using faster shift and add operations:

gap = ((gap > 2;

For even more speed, I'd also recommend in-lining this to eliminate the function call. Properly implemented, respectable speed can be achieved from a really trivial implementation.

Re: When would you ever want bubblesort?

#66
post #63

Before I knew anything about sorting algorithms, I invented my own algorithm to sort the high score list in my little BASIC game. It was very simple: Find the highest score, swap it to the top and continue, this time starting with the second item, until done. I imagine that if I ever find myself in need of writing a sorting algorithm from memory, I'll still go with this one.

Sounds like Selection sort[1]. It was probably the first sorting algorithm I ever implemented. I don't know why bubble sort is usually taught first. Selection sort seems more obvious. Perhaps the implementation is slightly harder due to the need to keep track of two indexes.

1. https://en.wikipedia.org/wiki/Selection_sort

Re: When would you ever want bubblesort?

#67
post #35

Earlier quoted context omitted.

>> You do the compare-and-swap when you're iterating over the objects to do some other render pass OP is describing using CAS to bubblesort an array while it is concurrently read by another thread rendering something based on its contents? That seems kind of wild to me but I don't know why else you add the words "some other". Sometimes sorting is an optimization and isn't necessary for correctness. The idea of two th…

You misunderstood. The CAS term isn't related to threads in aappleby's comment. aappleby is saying the following: He wrote a game engine, it renders transparent items using the painter's algorithm, which requires the items to be sorted in Z. He has a routine that iterates over all items to render them, and bubble-sorts the items in-place while iterating over the items (all in the same thread). The "compare and swap"…

I was writing code for production game engines ~20 years ago.

Re: When would you ever want bubblesort?

#68

Earlier quoted context omitted.

>> You do the compare-and-swap when you're iterating over the objects to do some other render pass OP is describing using CAS to bubblesort an array while it is concurrently read by another thread rendering something based on its contents? That seems kind of wild to me but I don't know why else you add the words "some other". Sometimes sorting is an optimization and isn't necessary for correctness. The idea of two th…

No, there was no mention of concurrency. As I understsand it, OP was describing a "render pass" that looked like this: for (int i = 0; i And adding a "compare and swap" to the loop: one step of a bubble sort, to keep the objects "mostly sorted": for (int i = 0; i obj[i+1]) swap(&obj[i], &obj[i+1]) // do some rendering or something } Thus, "do[ing] the compare-and-swap when you're iterating over the objects to do some…

This.

Re: When would you ever want bubblesort?

#69

Bubblesort is hard to beat if you're optimizing for object code size. I'm surprised that wasn't mentioned.

Yes. Ignorant question: if you use bubble sort but go through the array in alternating senses, could it not improve locality and speed?

Array v with N elements:

First pass: go from v[0] to v[N-1] comparing v[i] with v[i+i] Second pass: go from v[N-1] to v[0] comparing v[i] with v[i+i]

This is so obvious that it should have a name...

Re: When would you ever want bubblesort?

#70
post #69

Bubblesort is hard to beat if you're optimizing for object code size. I'm surprised that wasn't mentioned.

Yes. Ignorant question: if you use bubble sort but go through the array in alternating senses, could it not improve locality and speed? Array v with N elements: First pass: go from v[0] to v[N-1] comparing v[i] with v[i+i] Second pass: go from v[N-1] to v[0] comparing v[i] with v[i+i] This is so obvious that it should have a name...

Answering myself. Yes, it was an ignorant question as the name is Cocktail Shaker sort: https://en.wikipedia.org/wiki/Cocktail_shaker_sort
Post reply on HN