good implementations of bubblesort won't do the extra comparisons after the n-kth index (n elements, kth iteration). Also, it can be very fast to check if the list is sorted rather than possibly wasting a few useless iterations
I'm talking about "your micro have amount of flash in single digit kilobytes" cases so every byte counts. Even then I'd say it is still "try it after you optimized everything else" kind of problem
It is true that the quadratic sorts can be better than the linearithmic sorts for small datasets. But insertion sort is almost always the best of the quadratic sorts.
Edit: I should add that the bidirectional bubble sort can be as good as insertion sort for random arrays, but insertion sort is an adaptive sort so it's still better for real world data.
Also, it's the most efficient algorithm on pre-sorted data and gets less efficient slowly, so if you think your data is mostly sorted, bubble sort can be the best choice.
Of course it will become the worst option quickly thereafter, not counting shuffle sort.
With *'s showing which iteration they get locked So you only do 2 passes on the above instead of 1 pass for each element. This is why bubble sorting can have a variable performance factors on different data. It depends on how out of order the data is initially.
724
u/IdiotCharizard Dec 02 '19
good implementations of bubblesort won't do the extra comparisons after the n-kth index (n elements, kth iteration). Also, it can be very fast to check if the list is sorted rather than possibly wasting a few useless iterations