5 ms·
When would you ever want Bubble Sort?
- hilux 3y agoPresumably on some programming exam where they asked for this. Generally (in any field, not just sorting algorithms) I think there's value in understanding the step-by-step evolution of technology to reach the current state of the art, whatever that is. And it does occasionally happen that the state of the art breaks (Russia jams the GPS signal), and then we have to backtrack() to some earlier version of tech. Better hope it still works and we remember how to use it!
- AnimalMuppet 3y agoYou might consider it when you know you have small amounts of data. Say you're sorting peoples' phone numbers - not sorting all the phone numbers of all the people in the system, but for each person, sorting that person's phone numbers. Well, how many phone numbers does each person have in the system? Three would be the upper limit for most people. Is bubble sort faster for n=3? Probably. You could put a threshold - if n>20 (or some experimentally-determined number), use quicksort, else use bubble sort. You'd have to have enough instances of people with n>20 before it would even be worthwhile to make the check, though (unless it's worth it just as a sanity check/safety net).
- simonblack 3y agoSometimes you just want something that's "small, quick 'n' dirty". Maybe not often, but enough times that it's the correct solution. A merge-sort is better but it's a lot more complex to write and run. At a guess, I'd say that 16 would be the upper limit for me to use a bubble sort.