
Bubble Sort: An Archaeological Algorithmic Analysis - mac01021
https://users.cs.duke.edu/~ola/bubble/bubble.html
======
yoz-y
For me it seems that bubble sort is mostly popular because it is very
intuitive. It is the typical way of how would one sort a bunch of heavy bags
with only scales, which is another question that comes up often.

~~~
mannykannot
I suspect that if you took students who knew just enough programming to write
a sort, but with no prior exposure to sorting algorithms, and asked them to
write a sort, many would come up with a bubble sort (I think that was true of
me.) If so, then it is worth mentioning simply to point out its inefficiency.
Also, the bubble sort serves a useful pedagogical purpose for introducing the
concept of time complexity analysis, especially if my guess is correct, as
then it does not look like a contrived example.

After all, the goal of computing education is not just to churn out mechanical
algorithm-implementors, and it is hard to teach good design without something
to compare it to.

~~~
jdmichal
Bubble sort is _still_ the only sort I will write by hand. It's fast to write
and easy to read. It's in-place and stable. It has a stupidly low coefficient
in front of that n² that keeps it in the running until about 100 elements.

Once I get beyond that, I select a pre-written sort that's tailored to my
needs. Do I need stability? Do I need in-place? Do I need a local copy at all,
or can I stream the results?

~~~
s-phi-nl
Do you know of any source that compares the coefficients for bubble sort,
insertion sort, and selection sort? This sounds interesting.

~~~
jdmichal
Given that those are all average case n² algorithms, the article's figure is
probably a good representation:

[https://users.cs.duke.edu/~ola/bubble/bubble.html#fig:quadso...](https://users.cs.duke.edu/~ola/bubble/bubble.html#fig:quadsort)

Mannykannot is correct that the full coefficient is dependent on the machine
and exact code. However, we can make some refinements based on the algorithms
alone.

Bubble sort is n² / 2, as it processes one less element per pass. This is a
pretty common pattern, so it's good to remember.

Insertion sort starts at zero elements and processes one extra element per
pass. So it's also n² / 2\. However, _on average_ each pass only needs to
process half the elements in order to find the correct insertion point. So
it's actually n² / 4\. If you use a binary search, it becomes n log n -- n
passes, each perform a log n binary search. (This is usually called _binary
insertion sort_.)

However, those numbers are in _comparisons_ , which is typically what sort
algorithms are measured by. It assumes that all memory operations are constant
time. But you might also want to keep track of swaps or other memory movement.
For instance, when you add the memory swaps back to the binary insertion sort,
it's O(n²) again, because it still has to perform an average of n / 2 swaps on
every pass -- O(n * (log n + n / 2)) = O(n²).

Choice of data structure can also make an impact. Insertion sort is not
awesome on memory arrays, because it has to perform a block copy for every
insert. However, on linked lists, that single-element shift happens for "free"
when you perform the insert.

------
jjuhl
The only situation where I've actually employed bubble sort in production code
was in a situation where the container to be sorted was guaranteed to be at
most 5 elements (and usually 3) and was usually already sorted - and calling
this sort was done _a lot_. All other sorts proved to have too much startup
overhead or too much code that bloated caches.. so yes, I wrote a bubble sort
to be used there and it performed quite well. But I would generally _never_
recommend bubble sort for 99.9% of cases - but just once in a blue moon it
actually _can_ be the right answer.

------
juancn
I re-discovered bubble sort when I was 12, with a deck of cards and a C64. It
wasn't until I got to college that I learned it had a name.

I believe it's a very intuitive algorithm, it has likely been rediscovered
many times.

------
flashdance
"Well, I think the bubble sort would be the wrong way to go."

\--Barack Obama

~~~
yoz-y
I did not know about this and Googled the line out of curiosity. TIL

~~~
rkuykendall-com
Same, googled and found this great story:

[http://fortune.com/2015/08/18/mindware-
nisbett/](http://fortune.com/2015/08/18/mindware-nisbett/)

> Shortly after Barack Obama announced he was running for president in the
> fall of 2007, Google’s CEO, Eric Schmidt, interviewed him in front of a
> large audience of Google employees. As a joke, Schmidt’s first question was,
> “What is the most efficient way to sort a million 32- bit integers?” Before
> Schmidt could ask a real question, Obama interrupted: “Well, I think the
> bubble sort would be the wrong way to go,” a response that was in fact
> correct. Schmidt slapped his forehead in astonishment, and the room broke
> out in applause. Later, in the question-and-answer period, Obama assured his
> audience, “I am a big believer in reason and facts and evidence and science
> and feedback,” and promised that he would run the government accordingly.

> In the audience that day was a product manager named Dan Siroker, who made a
> decision on the spot to go to work for Obama. “He had me at bubble sort.”

