Hacker News new | ask | show | jobs
by enjoyyourlife 22 days ago
Is this just bubble sort?
9 comments

Matt Parker has an excellent video on this algorithm which should help illustrate why it's absurd (and not bubble sort): https://www.youtube.com/watch?v=ixXOUeBOPJw
Really fun video. There's a small mistake in the pseudocode in the video, where the indices are compared instead of the elements. Regardless, when he goes to demonstrate it he does compare the actual element values.

In that demonstration it seems like you can divide each pass into two parts: where i comes before j, and where i comes after j. It seems that only the pass where i comes after j (which is the first half) actually does something useful, the other half seems to sort it the opposite way, which then gets undone with the possibility to change it back being removed (since "i" has moved on permanently).

I suspect you could leave that second half out without changing the result. But if you only consider that first part (of sorting it "oppositely" to the comparison until the other index), it becomes equivalent to bubble sort.

It took me a while, but the surprising part is that the inner loop goes back to the first element again, unlike bubble sort which never revisits the outer loop's elements. So, at some point you'll compare element 3 with element 1, and swap so that 3 is smaller than 1, which is surprising.
It's not, but it's conceptually very similar at first glance.

One important thing to note is that bubble sort places one element in the correct position each pass and then *stops looking at that position ever again*.

This algorithm looks at all N elements every pass.

Others have shared a bunch of great videos and writeups. Without reading them, my initial assumption is that this builds things bottom up (such that after the first pass, the element in position 0 is guaranteed to be the lowest element) and that despite it being compared again and again, it's essentially placing one element each pass, but I could be wrong.

Technically the first pass will ensure that the smallest element ends up at position 0 even if it's looked at more times.

Bubblesort really is a family of "this is something I figured out myself that runs by looping in O(N^2) by moving things to the front/back"

> Technically the first pass will ensure that the smallest element ends up at position 0 even if it's looked at more times

Read it again; First pass ensures that the largest element ends up in position 0. Yet it somehow manages to sort ascending by the end.

It's not very intuitive and it's probably the least efficient sorting algorithm I've seen besides sleepsort[1]

[1]: https://www.reddit.com/r/programminghorror/comments/lgsd18/i...

Ah missed the part about the "swapped" equality, well since I will go ahead of J later on it's all good! :P
The hint is in the "<" operator.
That was my first instinct as well. But if you look closer you will see, it's actually insertion sort by someone who didn't pay attention in class.

It misses the revelation you can skip already sorted positions when deiciding what goes in the next position.

Btw. Ofcourse it sorts, it's basically insertion sort with extra swaps.

Look at it more carefully. Just reading the code, you'd think someone got confused between insertion sort and bubble sort and made a mashup that can't possibly work. But it still does.
No, it's not even selection sort, it's inefficient insertion sort with unintuitive logic.

https://news.ycombinator.com/item?id=50516743

it's bubble sort without the early return.

[edit: my bad, it's not bubble sort]

Pull up the pseudocode for bubble sort and compare it.
indeed, it's not bubble sort
No, it's not. There's a science paper with 3 theorems that prove it actually does sort
Bubble sort can sort
Every other sorting algorithm does, by definition
Well, mostly yes though some are less effective and may surprise time it takes to complete :)

https://en.wikipedia.org/wiki/Bogosort

"Two versions of this algorithm exist: a deterministic version that enumerates all permutations until it hits a sorted one, and a randomized version that randomly permutes its input and checks whether it is sorted.

An analogy for the working of the latter version is to sort a deck of cards by throwing the deck into the air, picking the cards up at random, and repeating the process until the deck is sorted. In a worst-case scenario with this version, the random source is of low quality and happens to make the sorted permutation unlikely to occur."