6 ms·
From the paper: > There is nothing good about this algorithm. It is slow – the algorithm obviously runs in Θ(n²) time, whether worst-case, average-case or best
by SamBam 5y ago
From the paper:
> There is nothing good about this algorithm. It is slow – the algorithm
obviously runs in Θ(n²) time, whether worst-case, average-case or best-case.
It unnecessarily compares all pairs of positions, twice (but see Section 3).
There seems to be no intuition behind it, and its correctness is not entirely
obvious. You certainly do not want to use it as a first example to introduce
students to sorting algorithms. It is not stable, does not work well for
external sorting, cannot sort inputs arriving online, and does not benefit
from partially sorted inputs. Its only appeal may be its simplicity, in terms
of lines of code and the “symmetry” of the two loops.
- dhruvdh 5y agoWell, I teach Algorithms, I think there is an obvious intuition as to why this works and I use this algorithm to introduce sorting.
- ColinWright 5y agoDo you really use this algorithm? Look more closely, if compares every element against every other element, and the comparison appears to be in the wrong order. This is not the Bubble Sort[0] as defined in Wikipedia. For one thing, it always has n^2 comparisons, is not always comparing adjacent elements, and does not terminate early. [0] There are some who disagree and claim that it is.
- dhruvdh 5y agoYes. I can read. It finds the largest number and places it at `i`. In the next iteration the largest number from 0 to `i` is placed at `i+1` Right up until `j` reaches `i`. Leaving a trail of orderliness. As the value of `i` increases we approach and eventually equal ascending order. What’s non-intuitive or what makes the comparison in the wrong order? It’s much easier to explain if we just stop `j` at `i` each time.
- ColinWright 5y ago> Yes. I can read. I apologise. Multiple comments elsewhere have been made by people who initially thought it was a Bubble Sort, and every other course and tutorial I know of starts with the Bubble Sort. So I admit to being surprised that you use this to teach algorithms. Personally, I find the algorithm given here completely unobvious, but perhaps that's just because I derived the Bubble Sort for myself back in the mid 70s, then read about O(n.log n) algorithms, and was hooked. For me, this algorithm is very misleading, but if you find it a useful teaching tool then I've learned something, and I find it intriguing. I'd be interested to know if it's used in any other courses. Do you know of any? snip description > What’s non-intuitive or what makes the comparison in the wrong order? It works, so obviously the comparison is right. But compare it with a classical Bubble Sort: procedure bubbleSort( A : list of sortable items ) n := length(A) repeat swapped := false for i := 1 to n-1 inclusive do /* if these are out of order */ if A[i-1] > A[i] then /* swap them and remember something changed */ swap(A[i-1], A[i]) swapped := true end if end for until not swapped end procedure It's looking to see if A[i-1] > A[i], because then they are the wrong way round. Compare that with the code in this submission: if A[i] < A[j] then ... It's the other direction, which pulls me up short. And it's comparing every place with every other place, whereas Bubble Sort only ever compares adjacent items. Being as familiar as I am with sorting algorithms in general, this, to me, feels unintuitive. I guess my intuition doesn't match yours.
- dhruvdh 5y agoI see now that people seem to be equating intuition with making steady, obvious progress. If you think about the point of all the adjacent comparisons in the inner loop for bubble sort is, it is finding the largest number and moving it to the last place in the array. It keeps doing that in a loop, the next time the second largest is found and at the second last place, etc. This algorithm doesn’t particularly do anything different, it just finds the largest and places it at `i` instead of the last place in the array. You’ll find that as it does it’s thing the array from 0 to `i` is always in ascending order. It’s very easy to teach this algorithm, the next variation that stops the inner loop at `i` instead of `n`, and the next variation that doesn’t do unnecessary swaps, which ends up being insertion sort I think? I would have to check.
- iainmerrick 5y agoWho said it was bubble sort? It’s clearly not.
- ColinWright 5y agoThere are others in this discussion who are claiming that it is Bubble Sort. Some have recanted, others are sticking to their guns. I am in email conversation with someone who is claiming that it is just a more efficient version of the original Bubble Sort, and I'm trying to get the time to track down their references. So: I don't think it is Bubble Sort, I think it's clearly different, I have said so elsewhere, not everyone agrees.
- cwoolfe 5y agoRight. Isn't the intuition that the comparison and swap rule is applied to every possible combination? Put another way: How can you rearrange everything based on order and expect the net result to not be ordered at the end?
- kilovoltaire 5y agoNote that the comparison is backwards, so by that line of thinking it would sort in reverse (descending) order …but it doesn't
- iainmerrick 5y agoHmm, come to think about it, it does have a lot going for it as an illustrative algorithm! - It’s really simple - It’s a bit counterintuitive, so makes a cute puzzle - It’s a good exercise for a first correctness proof - And it’s obviously a pretty pessimal algorithm, so students shouldn’t be tempted to use it in real code (hopefully...) Compared to bubble sort, which seems somewhat plausible, so people end up actually using it for real even though it’s a terrible algorithm.
- ColinWright 5y agoI've seen now from other comments that I'm not alone in finding this algorithm intuitive. Can you share with us your intuition as to why it works?