Search code examples
sortingbubble-sort

Is there a use for Bubble sort other than teaching people the basics of sorting?


I have never come across any utility or a code that uses bubble sort other than tutorials and the in class room. Is there any specific case where one might want to use it in an application? Thanks in advance.


Solution

  • It's good for very small data sets. See this link for a number of answers.

    What is a bubble sort good for?

    Two answers stand out:

    workmad3 wrote:

    I came across a great use for it in an optimisation anecdote recently. A program needed a set of sprites sorted in depth order each frame. The spites order wouldn't change much between frames, so as an optimisation they were bubble sorted with a single pass each frame. This was done in both directions (top to bottom and bottom to top). So the sprites were always almost sorted with a very efficient O(N) algorithm.

    Tetha wrote:

    we recently used bubblesort in an optimality proof for an algorithm. We had to transform an arbitrary optimal solution represented by a sequence of objects into a solution that was found by our algorithm. Because our algorithm was just "Sort by this criteria", we had to prove that we can sort an optimal solution without making it worse. In this case, bubble sort was a very good algorithm to use, because it has the nice invariant of just swapping two elements that are next to each other and are in the wrong order. Using more complicated algorithms there would have melted brains, I think.