5 ms·
When would you ever want bubblesort? (2023)
- caycep 10mo agoI learned this from President Obama...
- sdsd 10mo agoFor the downvoters, he's referring to this instance when (then) Senator Obama jokingly referenced bubble sort during this Google event: https://www.youtube.com/watch?v=koMpGeZpu4Q https://www.youtube.com/watch?v=koMpGeZpu4Q It was one of the many viral moments during Obama's original campaign where he seemed cool and in touch.
- beeforpork 10mo agoA: For small arrays. I would add: particularly if you need a stable sort algorithm, which is either complex (Block Sort) or uses O(n) space (Merge Sort).
- thomasmg 10mo agoThere is stable in-place merge sort [1], which is O(n*log(n)^2) and not that complex or hard to implement (about 80 lines, and that includes the ~15 lines of binarySearch, which you might need anyway). [1] https://github.com/thomasmueller/bau-lang/blob/main/src/test/java/org/bau/stdlib/sort/MergeSort.java https://github.com/thomasmueller/bau-lang/blob/main/src/test...
- nick__m 10mo agoif you apply quicksort to 2^20 random integers, at some point you're sorting 2^17 8-integer subpartitions why not use an 8 wide optimal sort network for those 8 integers?
- observationist 10mo agoEmbarrassingly parallel sort, lol.
- pieter3d 10mo agoI think because they are not necessarily consecutive.
- 13415 10mo agoWell, I used Bubblesort to sort the results of lottery draws because it was very easy to implement.
- zitterbewegung 10mo agoRelated to this is Timsort which combines merge sort and insertion sort https://en.wikipedia.org/wiki/Timsort https://en.wikipedia.org/wiki/Timsort
- aappleby 10mo agoCan confirm, have used bubble sort for incrementally sorting particles in a particle system and plants in a terrain renderer.
- jandrewrogers 10mo agoThe only use I've seen is incrementally sorting large arrays during brute-force search of said arrays, since that is approximately free and brute-force search is pretty efficient and fast on modern CPUs. Set a "sorted" flag if/when the array is eventually sorted. The idea was that the vast majority of arrays in a large set are not searched often enough to justify the cost of sorting them and sorting is an expensive operation if you are computing on a deadline. You also don't always know which ones will be heavily searched ahead of time. Using bubblesort, only the heavily accessed arrays end up sorted but as a side-effect of search rather than having separate heuristics to decide when/what to sort.
- pestatije 10mo agoto compare other sort algos against it
- zeta0134 10mo agoI used bubblesort on purpose in a game project. Specifically, to sort sprites in an NES game back to front, lazily, spending as few CPU cycles as possible. Bubblesort on the very small list (a dozen objects max), and early exit after the first swap. It eventually completes, and that was just fine. It's tiny, incredibly simple, and somewhat resilient to the list changing from frame to frame as objects spawn and despawn. Each partial sort makes some progress no matter what. A few other algorithms would have fit the bill just as well, but bubblesort is perfectly adequate, so that's what will likely ship. More complex algorithms end up losing out due to greater initial overhead or larger ROM size.
- cubefox 10mo agoA time traveler.
- bri3k 10mo agoTraveling through time at a rate of 1sec per second.
- jeltz 10mo agoWhy use it over insertion sort which is faster and easier to implement?
- GuB-42 10mo agoThere is "early exit after the first swap", which actually makes his algorithm closer to insertion sort than bubble sort. If the list couldn't change between each pass, it would be a very inefficient insertion sort in O(n^3) as you are constantly scanning the part of the list that is already sorted. But this is a case where theory doesn't count as much as having an acceptable result. It is typical in video games, if it plays well and looks fine, who cares if it is incorrect. Here I guess that sometimes a sprite appears on top of another sprite when it should have been behind it, because of the early exit, but while playing, it turned out not to be noticeable enough to change the algorithm.
- zeta0134 10mo ago
- JSR_FDED 10mo agoI’ve used it when I didn’t want the hassle of another dependency
- jhallenworld 10mo agoI've used it on a tiny microcontroller because libc's quicksort was huge.
- bxparks 10mo agoShell sort is sooo much faster than Bubble sort for tiny microcontrollers, for only a little bit more flash memory, like 40-100 bytes. If that's too much, then Insertion sort is 6X faster than Bubble sort, for only 10-20 bytes of extra flash.
- mhandley 10mo agoI've used bubblesort when simulating LEO satellite constellations, calculating which satellite is closest to a location. I used one single backwards pass of bubblesort, so O(n) every k timesteps to bring the closest to the head of the array, then every timestep just do one backwards bubblesort pass over the first few in the array. Given satellites move smoothly, if you initialize right (a few full passes at the start to get the closest few at the front) and get the constants right so a satellite outside the front few in the array can't have moved far enough to become closest without being promoted to the front few by a periodic full pass, then you always maintain the closest at the front of the array very cheaply. And this has the advantage of also being very simple to code.
- ErroneousBosh 10mo agoIf you need a stable sort, can't be bothered finding a massive oversize library to link to, and only need to sort a relatively small number of objects on a system that's resource-constrained, I'm guessing?
- thomasmg 10mo agoI'm surprised that the simple, ~80 lines version of stable-in-place merge sort (see link in the above comments) is not more widely known. It is O(n log n log n) and not all that hard to implement.
- deleted 10mo ago[deleted]
- dspillett 10mo agoFor small sets, or small-ish sets when you are coding quick, don't have a convenient standard library sort to hand, and are prioritising correctness over absolute performance. Though in reality almost never: you almost always have a convenient built-in sort that is as quick & easy to use (likely quicker & easier), and in circumstances where the set is small enough for bubblesort to be just fine, the speed, memory use, or other properties of what-ever other sort your standard library uses aren't going to be a problem either. As others have pointed out, sometimes it is useful for partial sorts due to the “always no less sorted than before at any point in the process (assuming no changes due to external influence)” property. wrt: > If you make each frame of the animation one pass of bubblesort, the particles will all move smoothly into the right positions. I couldn't find any examples in the wild, There are hundreds of sort demos out there, both live running and on publicly hosted videos, that show the final positions by hue, getting this effect. Seems odd that they couldn't find a single one. EDIT: actually, I can't find any of the rainbow based sort demos I was thinking of, a lot of promising links seem dead. I take back my little moan!
- Findecanor 10mo agoI've used bubblesort in a coding interview, because it was the easiest to remember and get correct on a whiteboard in short time.-
- sureglymop 10mo agoReminds me of an interview I had a while ago. The interviewer in all seriousness asked me to code up a sorting algorithm on the whiteboard. He was more of a business person than technical so was probably thinking of insertion, selection and bubblesort. I said sure, quicksort, mergesort or radixsort? He just said "okay, let's skip to the next question". :)
- arethuza 10mo agoI'd be tempted to ask under what circumstances they'd expect me to be coding a sorting routine... Seems a bit like asking an accountant to add two 10 digit numbers.
- JKCalhoun 10mo agoThe appeal of bubble sort for me is that is it the only one I understand well enough to implement myself without having to think much about it.
- ExtremisAndy 10mo agoI felt this comment in my soul. I’ll never understand it: I’ve written thousands of lines of code (as a hobbyist) to solve all sorts of problems I’ve run into and yet always seem to struggle to wrap my mind around the core algorithms any real developer should be able to handle easily. This is why I’ve never pursued programming as a career.
- vbezhenar 10mo agoIt took computer scientists of the past, a lot of effort to come up with these complicated algorithms. They are not easy or trivial. They are complicated and that's OK that you can't just quickly understand them. Your imaginary "real developer" at best memorised the algorithms, but that hardly differs from smart monkey, so probably not something to be very proud of. It is your choice which career to pursue, but in my experience, absolute majority of programmers don't know algorithms and data structures outside of very shallow understanding required to pass some popular interview questions. May be you've put too high artificial barriers, which weren't necessary. To be a professional software developer, you need to write code to solve real life tasks. These tasks mostly super-primitive in terms of algorithms. You just glue together libraries and write so-called "business-logic" in terms of incomprehensible tree of if-s which nobody truly understands. People love it and pay money for it.
- melagonster 10mo agoThanks for your kind comment! I do not have any systematic leaning of computer science; I often feel confused when reading textbooks on algorithms hahaha. Should I be familiar with every step of Dijkstra’s search algorithm and remember the pseudocode at all times? Why don’t the textbooks explain why the algorithm is correct?
- opensourcemaxi 10mo agobubble sort is sometimes used in information retrieval use cases for reranking top k based on some signals, especially specific to a user profile. I feel heap sort comes up as well, yet neither are necessarily the most efficient.
- johnnyanmac 10mo agoYeah, the article beat me to the gamedev example. Bubble sort being able to always "soft sort" on every iteration makes it the easiest to suspend and resume when you have a lot of other work to do, and when sorting is low priority. Also, general wisdom to be mindful of data sizes and cache coherency. O(NLogN) vs. O(N^2) doesn't mean much when you're only sorting a few dozen items. Meanwhile, O(N) space can have drastic performance hitches when reallocating memory.
- deleted 10mo ago[deleted]
- addaon 10mo agoThe article, and many of the responses, are hinting at the fact that bubblesort is an example of an anytime algorithm. This is a wide class of algorithms which provide a correct answer after some amount of time, but provide an increasingly good answer in increasing amounts of time short of the completion time. This is a super valuable property for real time systems (and many of the comments about games and animations discuss that). The paper that introduced me to the category is "Anytime Dynamic A*" [0], and I think it's both a good paper and a good algorithm to know. [0] https://cdn.aaai.org/ICAPS/2005/ICAPS05-027.pdf https://cdn.aaai.org/ICAPS/2005/ICAPS05-027.pdf
- amilios 10mo agoAm I missing something? If the algorithm is interrupted, the list will not be sorted. How exactly does it fit the criteria of an anytime algo?
- Pannoniae 10mo agoOkay, but it gives you a mostly good answer! Unlike many other sorts where if you interrupt it before the last step, you get total nonsense. It's basically asymptotically approacting the correct (sorted) list instead of shuffling the list in weird ways until it's all magically correct in the end.
- NooneAtAll3 10mo ago> Unlike many other sorts where if you interrupt it before the last step, you get total nonsense. which ones you have in mind? and doesn't "nonsense" depend on scoring criteria? selection sort would give you sorted beginning, cocktail shaker would have sorted both ends quick sort would give vast ranges separation ("small values on one side, big on the other"), and block-merge algorithms create sorted subarrays in my view those qualities are much more useful for partial state than "number of pairs of elements out of order" metric which smells of CS-complexity talk
- addaon 10mo agoAn anytime algorithm monotonically improves some evaluation metric. For a sort, the evaluation metric is usually the number of inversions in the list. At completion, there will be zero inversions. If at time 0 there are N inversions, then at time 0 < t < completion time there will be ≤ N inversions; that is, the list is "more sorted" than it was before. As the various examples about games and animation elsewhere in the comments show, this can be interpreted as "somewhat smoothly moving towards sorted over time," which is an (occasionally) ((rarely)) useful property.
- bxparks 10mo agoOn 8-bit and 32-bit microcontrollers (e.g. 8-bit AVR, 32-bit ESP8266/ESP32), Insertion sort is 6X faster than Bubble Sort on random data. I have tested this up to about N=1000. Both Insertion sort and Bubble sort are O(N^2). Both are stable sorts. Insertion sort consumes only about 10-20 bytes more flash memory than Bubble sort. It's hard to think of situations where Bubble sort would be preferred over Insertion sort. Shell sort is vastly superior if you can afford an extra 40-100 bytes of flash memory. (It's not too much more complicated than Insertion sort, but sometimes, we don't have 100 extra bytes.) It is O(N^k), where k ≈ 1.3 to 1.5. As soon as N ⪆ 30, Shell sort will start clobbering Insertion sort. For N ≈ 1000, Shell sort is 10X faster than Insertion sort, which in turn is 6X faster than Bubble sort. Unfortunately Shell sort is not stable. Comb sort has a similar O(N^k) runtime complexity as Shell sort. But it seems slower than Shell sort by a constant factor. Comb sort is also not stable. I cannot think of any reason to use Comb sort over Shell sort. Quick sort is not much faster than Shell until about N ≈ 300. Above that, the O(N*log(N) of Quick sort wins over the O(N^k) of Shell sort. But Quick sort is not stable. Merge sort is stable and runs in O(N*log(N)), but it consumes an extra O(N) of RAM, which may be impossible on a microcontroller. You may be forced back to Insertion sort for a stable sort.
- thomasmg 10mo agoThere is stable in-place merge sort, it runs in O(n*log(n)^2). It is about 3 times more complex than shell sort. I implemented it here https://github.com/thomasmueller/bau-lang/blob/main/src/test/java/org/bau/stdlib/sort/MergeSort.java https://github.com/thomasmueller/bau-lang/blob/main/src/test... (most sort algos you mentioned above are in the same direcory btw) You didn't mention heap sort. A simple implementation, which doesn't do any method calls just like shell sort (also next to the merge sort above) is about twice as complex than shell sort.
- bxparks 10mo agoThanks for the reference to in-place merge sort. GitHub is having a lot problems right now, I cannot see your code. I will look at it when I get a chance. I think I ignored Heap sort because it uses O(N) extra RAM, which is precious on a resource-constrained microcontroller.
- LorenPechtel 10mo agoUsed it a couple of times when n was inherently very low. And while I've never hit a case I would think it would have merit with data known to be pretty close to properly sorted.
- AnimalMuppet 10mo agoIn all your big-O analyses, remember: n = 3 more often than you think. n = 12 a lot more often than you think. If that's your case, there's nothing wrong with bubble sort unless you have very tight performance constraints. Worse, big-O always hides a constant factor. What's bubblesort's constant? What's quicksort's? It wouldn't surprise me if, for small enough n (2 or 3, and maybe a bit higher), bubblesort is actually faster. Note well: I have not actually benchmarked this. Also note well: Determine what your n is; don't assume that it's either large or small.
- CyLith 10mo agoWhen sorting eigenpairs of a dense matrix, usually tou end up with a Schur decomposition. The basic operation that you can do is swap two adjacent eigenvalues on the diagonal, so bubblesort is a natural candidate.
- 6Roman6 10mo ago[dead]
- Syzygies 10mo agoWhen I was a senior at Swarthmore College, Herb Wilf came over from U Penn to teach a course in combinatorial algorithms. I was encouraged to attend. He claimed that choosing a subset of k integers at random from {1..n} should have a log in its complexity, because one needs to sort to detect duplicates. I realized that if one divided [1..n] into k bins, one could detect duplicates within each bin, for a linear algorithm. I chose bubble sort because the average occupancy was 1, so bubble sort gave the best constant. I described this algorithm to him around 5pm, end of his office hours as he was facing horrendous traffic home. I looked like George Harrison post-Beatles, and probably smelled of pot smoke. Understandably, he didn't recognize a future mathematician. Around 10pm the dorm hall phone rang, one of my professors relaying an apology for brushing me off. He got it, and credited me with many ideas in the next edition of his book. Of course, I eventually found all of this and more in Knuth's books. I was disillusioned, imagining that adults read everything. Later I came to understand that this was unrealistic.
- itemize123 10mo agointeresting, when is this? because that seems obvious today - basically hash-set, right?
- Syzygies 10mo ago1977. And I didn't know what a hash table was, though I can't explain now why they didn't think of using a hash table. I was effectively using a dumb hash function. Their 1978 second edition works in exactly the memory needed to store the answer, by simulating my algorithm in a first pass but only saving the occupancy counts. Oh, and thanks (I guess). I really didn't expect to ever be reading FORTRAN code again. One learned to program at Swarthmore that year by punching cards, crashing our IBM 1130, and bringing the printout to my supervisor shift. I'd find the square brackets and explain how you'd overwritten your array. I even helped an economics professor Frederic Pryor (the grad student in the "Bridge of Spies" cold war spy swap) this way, when I made an ill-advised visit to the computer center on a Saturday night. Apparently I could still find square brackets.
- refibrillator 10mo ago
- kazinator 10mo agoDoesn't Shell sort also have the property of the exchanges leaving the array more ordered than before? https://en.wikipedia.org/wiki/Shellsort https://en.wikipedia.org/wiki/Shellsort Shellsort can be regarded as an improvement over either Bubble Sort or Insertion Sort.
- jaw0 10mo agoat a previous workplace, every new hire would discover the handwritten bubblesort in our codebase, freak out, and submit a pull request to fix it. and every new hire got taken to the whiteboard to learn about sort algorithm performance: bubblesort is O(n) in the best case. and in our codebase, the data being sorted fit that best case (the data was already sorted or almost sorted).
- avmich 10mo agoNot only in best case. Haven't seen this elsewhere, and know only few people who know that, so, a kind of a puzzle: what are the conditions when bubblesort is always O(n)?
- pilord314 10mo agoWhen you get into C code sometimes you know the most thinngs that will be in the priority queue is like 3. So bubble sort is fine. You can also do something like a calendar queue with bubble sort for each bin.
- whateveracct 10mo agoit's great if you need to sort in the face of unreliable memory iirc
- another_twist 10mo agoWhen the array is almost sorted. Bubble sort complexity is linear + inversions so if the inversions are low (the more sorted the array the lower the number of inversions), bubble sort is close to a linear pass.
- lucraft 10mo agoIt's way easier to remember and program When I was playing The Farmer Was Replaced and needed to implement sorting, I just wrote a bubble sort. Worked first time.