That is just a bubble sort, except the comparisons are done in different order than conventional bubble sort...
post
Seems straightforward enough. For j values of 1 to i it will not do anything because the largest element in the array has already been moved to position i in some earlier iteration in the i loop. For j values greater than i it then proceeds to find the largest remaining element place in position i.
Edit: I'm leaving my incorrect thought above as is for posterity. As pointed out when j is less than i it does of course swap again resulting in a more difficult (for me) visualization of what the end result looks like. Time to have some fun with it (could read about it, but programming is more fun and I like to experiment).
For the j > i case I think you're right, it sorts largest to smallest (or, backwards), but for the j < i case it grabs larger values from [0, i] that it initially moved to the top of the array and slots them back in, effectively (if roundabout-ly) correcting the backwards sorting of the j > i part of the algorithm. Sort of a "two wrongs that accidentally make a right" maneuver.
Sort of a “two wrongs that accidentally make a right” maneuver.
not exactly (if i understand it correctly). the first swap of a pair, where i < j, basically does not matter, since the same pair will be revisited one more time later with switched values (i = 8, j = 9 does not matter. i = 9, j = 8 does) and that is when the actual sorting happens. that is why the condition is if a[i] < a[j] then swap, which may seem countreintuitive, but we are comparing the values in the reversed order compared to most of the sorting algorithms.
the i < j part can be seen as the part that is handled in bubble sort by making the inner loop progressively smaller as the array is partially sorted (not the same elements, but the same amount of work, sort of). it is just ignored here, which is obviously bad for any kind of efficiency, but it allows for that super simple code.
As the addendum to the paper points out, this is more like insertion sort (with i and j in a confusing order) than bubble sort. The actual "important" part of the algorithm happens when j < i. (In fact, I'm quite certain there aren't even any swaps when j >= i after the first outer iteration.) (I think the page about people misremembering bubble sort is also worth looking at.)
Honestly, I think this might actually be "better" than bubble sort, in the sense that at least it's incredibly easy to remember and not that hard to get correct. The only place where you could realistically mess up is confusing the relative order of i and j -- and any amount of nontrivial testing will immediately show the error, since it reverses the sort order. I'd probably reach for this if I, for whatever insane reason, had to code up a sorting algorithm by hand for some task where O(n^2) sorting was acceptable performance-wise. "Sort a list of 10 items in a very primitive programming language"-type deal.
(Now that I say that, I'm kind of tempted to use it in some example program for my own in-development programming language, which currently doesn't have a builtin sort function...)
I would expect something worse than bubble sort. No idea whether it will even work:
void reverse(auto A, int start, int end) {
for (int i = 0; i < (end - start) / 2; i = i + 1) {
auto tmp = A[start + i];
A[start + i] = A[end - i];
A[end - i] = tmp;
}
}
for (int j = 0; j < N; j = j + 1) {
for (int i = 0; i < N; i = i + 1) {
if (A[i] < A[i + 1]) {
reverse(A, i, N);
}
}
}
My favorite sorts are random sort (toss them up, see where they fall. are they sorted?) and bead sort (connect 4 sort).
One is potentially O(1) sort and the other is potentially O(n). haha. But in practice they are terrible. Love it.
This sort is neat too.
all 10 comments