6 ms·
Bubblesort appears to be inferior to insertion sort in every single way. It's more complicated, it's slower in every situation. I don't know why you'd ever use
by ogogmad 6y ago
Bubblesort appears to be inferior to insertion sort in every single way. It's more complicated, it's slower in every situation. I don't know why you'd ever use it. What's the point of it?
- LordDragonfang 6y agoThe "point" is that it is one of the three intuitive sorting algorithms that someone unfamiliar with algorithmic complexity will write when asked to write a computer sorting algorithm (and happened to be the first sort I wrote when tasked with just that in college long ago). This makes it a useful thing to discuss because it's so clearly inferior to the other two despite being just as intuitive.
- bonoboTP 6y agoInsertion sort seems more intuitive. Just take the next item and file it at its correct position. I think the whole "swap neighbors and scan many times" thing isn't something a novice would come up with.
- quietbritishjim 6y agoThe refutation of that is in the comment you're replying to: > [Bubble sort] happened to be the first sort I wrote when tasked with just that in college long ago
- virgilp 6y agoIf you need an additional datapoint, bubble sort is also the first sorting algorithm I came up with. I find it super intuitive - the only slightly-less intuitive part maybe is that you need to do the outer loop at most N times (in my implementation, the outer loop was `while(changed)` where `changed` would be set to `true` if any swap was performed in the inner loop ).
- bonoboTP 6y agoEven though I know the algorithm, I still find that I need to think a nontrivial amount to see why it will not miss anything. I mean, sure, you are strictly improving the "sortedness" by a discrete amount in each pass and crucually, you don't destroy any progress you made before (not totally trivial), and there's only a finite amount of "sortedness" to attain, so it will converge to the sorted state. But how exactly and, as you say, after how many iterations, is not so clear. In more complex algorithms, you may get stuck in a local optimum with such a greedy approach though. But contrast that with insertion sort. I have a sorted pile that grows after each insertion. I find it trivially clear that it works and when I will be done. It's also something that people will intuitively do in the physical world to sort paper documents I think: just grow a pile of sorted documents by adding the next one in the right place.
- ernesth 6y agoMy anecdotal experience as a teacher is that when tasked with implementing sort, students go for selection sort. However when asked to do it in real life on a bundle of graded exam sheets, if I ask them to sort alphabetically, they will do insertion sort, if I demand to sort by grade, they will bubble sort. The number of sheets may also change their method.
- iainmerrick 6y agoI think this is a bit dangerous because people take away the “I understand and can implement bubble sort” part, but forget the “it’s very inefficient, don’t use it” part. So people end up using bubble sort in production! It doesn’t help that it has such a cute and catchy name.
- coldpie 6y agoI don't think I have ever implemented a sort. What modern tooling doesn't provide a sort? Even C has qsort.
- lebuffon 6y agoRISC V Assembler :)
- iainmerrick 6y agoWhy even bother teaching it, then? (Semi-serious question, as you're quite right that hardly anyone ever needs to write a sort routine by hand these days.)
- ogogmad 6y agoMaybe it's the things you learn along the way, such as: - Recursion and induction (mergesort, quicksort) - Lower bounds on running time (O(n log n)) - Data structures (heaps in the context of heapsort, binary search trees in the context of treesort, tries in the context of radix sort, etc.) - Difference between worst case and average case (quicksort) - The role of the hidden coefficient in O notation (insertion sort is fast for small n even though its running time is O(n^2)) Etc.
- kadoban 6y agoProbably no real fix for that. In algorithms if you only taught the best algo for a particular problem, you'd lose everyone on the first day, and miss out on a lot of analysis/teaching opportunities. Remember that algorithms study isn't just about speed, it's actually about _correctness_ first. Besides, it's pretty rare for someone to need to write their own sorting algorithm anyway, in production. And if they do but they're so addled they do it from memory and randomly pick bubblesort, hey at least it's fairly easy to be correct.
- mahmoudhossam 6y agoIt's easier to teach and visualize, that's why it's usually taught as the first algorithm to computer science students.
- ogogmad 6y agoLike I clearly said, not when compared to insertion sort.
- jan_Inkepa 6y agoThe differences between bubblesort and insertion sort are quite subtle and learning that it an of itself can be instructive IMO in terms of teaching rigour.
- taneq 6y agoIt's good when there are a lot of elements off by a short distance in a mostly sorted list. The go-to use case that I've seen is depth sorting for 3D rendering on really low end hardware. Polygons usually don't move much in the Z-order each frame, so you run a few passes of bubblesort per frame for a good-enough linear-time sort.
- ogogmad 6y agoDo you know how it compares to insertion sort, which is also fast on nearly sorted lists?
- NortySpock 6y agoAlso, two passes of cocktail-shaker sort (back and forth bubble sort) would guarantee that the top and bottom z-order would be correct, which would probably be good enough to keep really obviously bad z-ordering at bay. Like you say, when it's already partially sorted and you're just having to maintain a slowly-updated list, a single partial-sort pass per frame could be quite enough to maintain a good-enough ordering of a list.
- jansan 6y agoI Germany we have a saying that goes "Nobody is completely useless - you can still serve as a bad example."
- mortehu 6y agoYou might need bubble sort if you have to rearrange a train using a turntable that can fit exactly two cars.
- ogogmad 6y agoAgain, use insertion sort. I swear all the people recommending bubble sort for whatever reason are doing so out of ignorance.