r/programming • • Dec 02 '19

Bubble sort visualization

7.4k Upvotes

268 comments sorted by

View all comments

Show parent comments

21

u/Gangsir Dec 03 '19

Nah, you're right. That's why I said "equal or better algorithm". To my understanding, insertion overall would be better still, because as soon as you mess with that optimal scenario (un-sort the data a bit) bubble becomes slower.

If you had a data set that you could be assured would always come pre-sorted, then there'd be no difference between bubble and insertion. (You also wouldn't need a sort algo in the first place, but that's beside the point)

If bubble is the best algo, it's only because it's equal with another algo. It'll never be a lone upgrade.

6

u/[deleted] Dec 03 '19

If bubble is the best algo, it's only because it's equal with another algo. It'll never be a lone upgrade.

Isnt it smallest code wise ? That's an upgrade

8

u/SirClueless Dec 03 '19

Yes. Though the space savings are very small (Insertion Sort is also very simple), and the performance losses are very large on some lists, so in most cases even if code size important Insertion Sort should be preferred.