64 points · by DyslexicAtheist · 7y ago · 19 comments
Around 1990, on my first real job out of college, I was assigned about 100K lines of C code to maintain. I was told that it had problems scaling up to larger networks (it was a network mapping system).
While going through the code, I discovered that it contained a bubble sort that had been commented out. The comments said that it had been replaced with hand-rolled sorting logic in 68K assembly for "additional speed."
So I found the assembly code for the new sorting logic. It was also a bubble sort!
In disbelief, I replaced all this nonsense with a call to the standard library's `qsort` function. The scaling problems disappeared :-)
The article unfortunately omits actual code for insertion sort and selection sort, except the "jump-down" variant of selection sort it mentions early on.
Bentley gives the "simplest insertion sort" as follows (translated to modern C; the version in the book is pseudocode):
for (int i = 1; i < n; i++)
for (int j = i; j > 0 && x[j-1] > x[j]; j--)
swap(j-1, j);
I think that variant is only faster than bubble sort because it stops iterating once the newly inserted item has reached its final resting place, on average halfway through the sorted part of the list, so it only does about n/4 comparisons and swaps per outer-loop iteration, rather than bubble sort's n or n/2. A better approach — usually the fastest sort for short arrays — stores the element being inserted in an auxiliary variable until its final resting place is found, thus avoiding almost half the work of swapping. Bentley reports from the previous millennium that this is a speedup of about a factor of four: for (int i = 1; i < n; i++) {
data t = x[i];
int j;
for (j = i; j > 0 && x[j-1] > t; j--)
x[j] = x[j-1];
x[j] = t;
}
A similar optimization is available for selection sort, which is otherwise equivalent to bubble sort, necessarily examining as it does almost n/2 elements on average to produce each output element.There's an even simpler single-loop sort algorithm (I forget what it's called) which is unfortunately O(N³):
for (int i = 1; i < n; i++)
if (x[i-1] > x[i])
swap(i-1, i), i = 1;
That won't work in Python, of course, but opinions may vary on whether we should be sorry or glad about that.Is that from one of his books (e.g. Programming Pearls, More Programming Pearls, Writing Efficient Programs), or from some paper he published? I've read parts of the first two books and most of the third book, and all are great.
Shaker sort is an interesting variation of bubble sort (and maybe also selection sort, according to its Wikipedia page) that sorts in both directions.
https://en.wikipedia.org/wiki/Cocktail_shaker_sort
Only slightly more complex than bubble sort, which is itself simple.
Shaker sort is potentially interesting for cases where you're adding a few out-of-order items to an otherwise sorted set, but I think insertion sort still beats it even in that case.
Also, bubble sort, though one of the slowest, has this property:
https://en.wikipedia.org/wiki/Bubble_sort#Use
[ In computer graphics bubble sort is popular for its capability to detect a very small error (like swap of just two elements) in almost-sorted arrays and fix it with just linear complexity (2n). ]
It's actually faster because of a subtler reason: branch prediction.
In insertion sort, the x[j-1] > x[j] comparison is almost always true. Insertion sort performs that comparison O(n^2) times, and it is false only n-1 times. So the branch predictor quickly identifies that it should just assume the comparison is true.
for (int i = 1; i < n; i++)
if (x[i-1] > x[i])
swap(i-1, i), i = 0;https://imgur.com/gallery/RM3wl
https://panthema.net/2013/sound-of-sorting/
https://www.creativeapplications.net/processing/sorting-visu...