r/programming • • Dec 02 '19

Bubble sort visualization

7.4k Upvotes

268 comments sorted by

View all comments

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

659

u/[deleted] Dec 02 '19

good implementations of bubblesort

Say what now?

211

u/[deleted] Dec 03 '19

Algos like bubblesort can be preferable on small data sets as opposed to other "better" algos.

118

u/[deleted] Dec 03 '19

Or of you are on tiny micro and do not care about time but code size

69

u/RICHUNCLEPENNYBAGS Dec 03 '19

In that case isn't shellsort the best choice? Like Sedgewick's book doesn't even bother presenting bubble sort

49

u/[deleted] Dec 03 '19

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

28

u/RICHUNCLEPENNYBAGS Dec 03 '19

I am not an embedded developer but this might be of interest. This guy is pretty down on bubble sort even in embedded scenarios and suggests a shellsort is usually the best choice. https://embeddedgurus.com/stack-overflow/2009/03/sorting-in-embedded-systems/

20

u/[deleted] Dec 03 '19

Sounds like a job for bogosort!

9

u/cbarrick Dec 03 '19 edited Dec 03 '19

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.

38

u/Tyler_Zoro Dec 03 '19

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.

29

u/SirClueless Dec 03 '19

Isn't Insertion Sort strictly better on near-sorted data?

17

u/Tyler_Zoro Dec 03 '19

They're the same for trivial data sets (assuming you always test for last-pass in bubble sort), but yes, for non-trivial cases, IS is better.

9

u/lpreams Dec 03 '19

So is there any case in which bubble is better than insertion?

6

u/Tyler_Zoro Dec 03 '19

I don't think so. Bubble just ends up being insertion for trivial cases.

4

u/G_Morgan Dec 03 '19

Yeah and there are many scenarios that might look like that. For instance tacking onto a previously sorted list.

1

u/thedessertplanet Feb 02 '20

Simple variants of merge sort give you linear time performance on a wide variety of partially presorted data.

→ More replies (2)

10

u/bubblesort Dec 03 '19

Whadda ya mean, 'better'? Them's fightin words! Who says they better than me? I'll give em a poke in the eye!

5

u/StockAL3Xj Dec 03 '19

Are we really shortening algorithm to "algo" now?

2

u/FlatPlate Dec 03 '19

Weren't there a quote from someone important that said, no matter what you're doing you shouldn't use bubble sort?

3

u/[deleted] Dec 03 '19

Try saying that in an interview and see how it goes..

20

u/Ewcrsf Dec 03 '19

It would go well unless the interviewer is utterly incompetent.

9

u/ivosaurus Dec 03 '19

It should go poorly because you should be using insertion sort 100% of the time you could ever want to use bubble sort.

→ More replies (1)
→ More replies (3)

1

u/doublehyphen Dec 04 '19

Insertion sort is virtually always faster than bubble sort. It is also more intuitive.

12

u/jarfil Dec 03 '19 edited Dec 02 '23

CENSORED