3 ms·
I've written a sort perhaps a handful of times in the last 40 years. Each of those was a bubble sort. I was nearly-always sorting <4 elements (and often just 1,
by jfengel 3y ago
I've written a sort perhaps a handful of times in the last 40 years. Each of those was a bubble sort. I was nearly-always sorting <4 elements (and often just 1, or even 0).
These were rare cases where I didn't have a sort algorithm in a library -- perhaps because I needed to pass in a comparator that wasn't easily supported in the language. So I banged out whatever was easiest to get me moving on to the actual problem. If the sort became a noticeable part of the running time, I'd deal with that when I had performance numbers.
Bubble sort was easy to remember and quick to bang out. For all I know I really was writing an insertion sort; all O(n^2) sorts look pretty much the same.
I haven't written any sorts at all in well over a decade, since all languages have substantial support. If I'm writing a sort, something has gone very wrong.
- JohnFen 3y ago> I was nearly-always sorting <4 elements (and often just 1, or even 0). In my opinion, a bubble sort is a solid choice for that sort of use case.
- Trufa 3y agoAre you being cheeky? Cause I would think insertion sort would be a more than reasonable alternative
- JohnFen 3y agoNo, I'm serious. With datasets that small, there's very little performance difference between the two methods, so a bubble sort isn't unreasonable at all. Depending on the circumstances, a bubble sort can be the better choice due to other considerations becoming relatively more important.
- bastawhiz 3y agoIn what way?
- usefulcat 3y agoEspecially if there’s little or no performance difference, may as well minimize code size.
- financltravsty 3y agoIs everyone here feeling a bit... devilish? I would pick a standard library sort function at that point.
- grog454 3y agoFor <4 elements, a pure "if" / "else" implementation would probably be optimal.
- layer8 3y agoBut more tedious and somewhat more error-prone to write by hand.