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.

→ More replies (3)

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?

→ More replies (11)

12

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

CENSORED

117

u/pedrovhb Dec 02 '19

Perhaps the title should be "Naive bubble sort visualization" (:

36

u/hylet Dec 03 '19

more like "paranoid bubble sort", keeps checking the last elements even though they are sorted

15

u/[deleted] Dec 03 '19

[removed] — view removed comment

3

u/SmokeyDBear Dec 03 '19

bubblesortandhash

6

u/[deleted] Dec 03 '19

I have my bubble sort algorithm run continuously in the background. Just in case.

87

u/schplat Dec 02 '19

yup, you can "lock down" the last element after the first path, because it will assuredly be the correct spot after all passes.

Also, if the last n elements do not switch spots on given pass, you can "lock down" all of those spots. i.e.:

4,1,2,3,5 <1st sort pass> 1,2,3,4,5* <2nd sort pass> 1*,2*,3*,4*,5*

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.

→ More replies (18)

2

u/irqlnotdispatchlevel Dec 03 '19

I feel like this talk by Andrei Alexandrescu is relevant here.

→ More replies (53)