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

-7

u/dzamlo Dec 02 '19

A good implementation of bubblesort is an implementation of another algorithme. Bubblesort is a very bad algo no matter the implementation.

114

u/Free_Math_Tutoring Dec 02 '19

Yes, everybody knows that bubblesort is never the best option. Thank you for contributing. Discussing specific optimizations that can be applied frequently is nonetheless interesting.

9

u/Adventurer32 Dec 02 '19

what is the best option?

53

u/Gangsir Dec 02 '19 edited Dec 02 '19

The best option depends on the data you're sorting. Is it large, or small? Will it vary in size? Mostly sorted, or completely random? Will it ever start out already sorted? How fast is it to compare two entries (do you have to check several sub-fields to say a[1] should be in front of a[3])? Does it need to be deterministically sorted (two values that evaluate equal will always end up in the same order, not be switched every time you sort)?

Etc. Choosing the right algo is hard sometimes, especially the more "chaotic" your data is (varies in size, sometimes sorted, sometimes random, takes forever to compare values, etc). On top of that you have to consider different constraints: Does the sort need to be fast? (Yes you could sort 1000000 entries with a bubblesort, but it'd take forever) Does the sort need to use little memory, or is the sort the only thing the computer is doing (so it's okay if it hogs memory)? How complex can the sort be, how much dev time do you want to put into it?

Bubblesort is never the best because no matter what your data is, there's always a better algo than bubble. Even bubble's perfect condition (data is already sorted, or only the last two values are out of order) can be sorted just as well or better with a different algo.

31

u/d4harp Dec 03 '19

Even bubble's perfect condition (data is already sorted, or only the last two values are out of order) can be sorted just as well or better with a different algo.

Bubblesort on pre-sorted data would have a time complexity of O(n), and space complexity of O(1). Isn't that the maximum possible efficiency? The only algorithm I know of with an equal best case is insertion sort.

(This is a question, not a correction. Sorry if I got this completely wrong)

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.

→ More replies (13)

7

u/IdiotCharizard Dec 03 '19

whichever one is in the stdlib of the language you're using.

6

u/itmustbemitch Dec 03 '19

To give a more concrete answer which is less philosophically true than the other poster but perhaps more immediately useful, in very general cases, I believe the most common best sort is called quick sort.

The idea of it is that you pick a number, called a pivot iirc, in your list that you hope belongs close to the middle, then shove everything larger than the pivot to its right side and everything smaller than the pivot to its left side. Then you repeat the process on the left-side and right-side sub-lists, with a new pivot for each sub-list, until the whole thing is sorted.

There are some problems with this sort (the biggest being that it becomes extremely bad in the case that you pick pivots in an extremely dumb or unlucky way), and there are some optimizations I'm leaving out here, but in terms of average case performance it's generally the strongest choice

4

u/bubblesort Dec 03 '19

The best option is to ignore all the jerks in this thread talking shit about me. Bunch a trolls around here.

→ More replies (1)

6

u/Gawdl3y Dec 03 '19

4

u/Chand_laBing Dec 03 '19

I know tremendous things about sorting bigly crooked data sets. That's why I use bubble sort. Hey Quicksort? Ya fired!

11

u/digitaldreamer Dec 02 '19

I wouldn't say that it's never good. It's fine for sorting small sets. It's simple and easy to implement. There's no need to complicate things if you know you're never going to sort over a certain size.

23

u/chucker23n Dec 02 '19 edited Dec 02 '19

But there’s also no need to implement your own sort at all, especially for small sets.

Bubble sort exists purely for educational purposes. Further optimizations are mildly interesting for the same reason bubble sort is: not for production use, but to explain how stuff works.

16

u/k-selectride Dec 03 '19

If you're in a hard real-time environment and you have data that you want to make progress towards sorting for each tick without storing intermediate state, you probably want something like bubble sort.

8

u/OMGItsCheezWTF Dec 03 '19

Yeah but that's a pretty niche case. In the vast majority of workloads your language, framework or underlying datastore's sort() method is probably good enough.

5

u/k-selectride Dec 03 '19

Yes absolutely.

→ More replies (1)

16

u/tommcdo Dec 02 '19

It's rare to be in an environment that doesn't have an available sort library.

→ More replies (1)

3

u/Katanamatata Dec 03 '19

I didn't know this but I'm new to algorithms

→ More replies (1)

5

u/corporaterebel Dec 03 '19 edited Dec 03 '19

if you are sorting 2-10 items it has good performance.

(I use it to sort dynamic information while analysts are categorizing leads on data entry forms)

4

u/IdiotCharizard Dec 03 '19

Bubblesort is still good for learning, and while learning, it's still useful to know not to waste cycles.

2

u/bubblesort Dec 03 '19

I'm not bad, I'm just drawn that way.

2

u/[deleted] Dec 03 '19

If I have less than 10 elements, I wouldn't bother implementing anything more complex.

→ More replies (1)