I Can't Believe It Can Sort
Muhammad523
38 points
55 comments
November 13, 2026
Related Discussions
Found 5 related stories in 40.2ms across 3,874 title embeddings via pgvector HNSW
- A C++ Quicksort Faster Than std:sort signa11 · 38 pts · November 10, 2026 · 47% similar
- Size-It-Up (Game) whoishiding · 75 pts · November 06, 2026 · 39% similar
- Let Me Ch... alxgsv · 184 pts · November 02, 2026 · 38% similar
- I can’t believe it’s 1 ColinWright · 87 pts · November 16, 2026 · 38% similar
- The Strongest Sokoban Solver ibobev · 148 pts · November 16, 2026 · 38% similar
Discussion Highlights (17 comments)
enjoyyourlife
Is this just bubble sort?
amelius
I'd like to see a formal proof of this. Not because I don't believe it's true, but because this might be a nice entry into formal proofs.
logicallee
I'm pretty impressed by both Grok's and Gemini's static analysis of it. (I changed the name of the function to algo and asked them to figure out if it's supposed to be a sort algorithm or a search algorithm. Both got it right.)[1] If someone else could please check the same prompt on ChatGPT and Claude (which I don't have) please link. [1] https://grok.com/share/c2hhcmQtMw_77a3a16f-642c-4ea7-95c7-28... and https://share.gemini.google/HADqPiEaeHQ9
Someone
Paper about this with proof of how/why it works: https://arxiv.org/pdf/2110.01111 , which says: “There is nothing good about this algorithm. It is slow – the algorithm obviously runs in Θ(n2) 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.”
edflsafoiewq
Previously (2021): https://news.ycombinator.com/item?id=28758106
VMG
I can't believe some people can read grey text on slightly lighter grey background
ivanjermakov
It is very efficient, just for arrays not larger than 2 elements :)
bob1029
The reason it works is because you get redundancy with everything referencing everything else. It looks like one of those CPU core to core latency diagrams. Without iterating like this the sort could only flow in one direction. If we modified the conditional to handle both cases you could split the square along the identity diagonal and only use half.
petercooper
On the topic of fun small sorting algorithms, I was always tickled by "sleep sort": a=[5,3,1,4,2];b=[];a.map(n=>setTimeout(()=>b.push(n),n)) Obviously never use this for real, but I found it fun to think about the behavior and pitfalls of this algorithm in different languages.
david-gpu
It has some redeeming qualities. I would guess its running time is as consistent as the memory hierarchy allows. Having tight bounds on execution can be valuable in real time systems. Simple to verify that the implementation is correct. Simple to implement in assembly. Could be worse! I still prefer bogosort.
iagiagiag
It's more confusing in your head than what the code looks like.
bajard
In simple terms, this is the algorithm: Repeat n times: sort all n pairs of elements.
senfiaj
Looks like suboptimal insertion sort. After the first nested loop run A[1] will have the largest value. After the second nested loop run A[1] will always be swapped with A[2] (unless they are equal, logically doesn't change anything). A[2] now contains the largest value. After the k-th (k >= 2) nested loop run, if you pay attention, the k-th element will be inserted into the first k-1 sorted elements. When j >= i, it can no longer do anything, since A[k] will contain the largest value. After the k-th (k >= 2) nested loop run the first k elements will be sorted relative to each other. The first nested loop is special by bringing the largest element to A[1], the consecutive ones are just inserting A[k] into the sorted first k - 1 elements from the beginning of the array.
virajk_31
"you won't think that it can actually sort" - it does and anyone can tell the same by just looking at it...
plingbang
It's hard to imagine how many times this sort must've been written by accident (see the infinite monkey theorem). Imagine a CS student typing this while not entirely knowing what they're doing, presenting it to their instructor, getting told that the code is wrong and changing it to a proper bubble or selection sort without suspecting a thing.
smusamashah
It behaves somewhat like insertion sort https://xosh.org/VisualizingSorts/sorting.html#IYZwngdgxgBAZ... Tick insertion sort on right panel and tap start.
fwlr
There are two reasons it (intuitively) should not work: one is that the inner for loop also starts from the beginning, without checking where the outer for loop is at, meaning you’ll get `compare(a[0], a[2]) == true` but you’ll also get `compare(a[2], a[0]) == false`, so it would appear you can’t get any useful meaning out of the result of `compare`. The other is that the comparison sign is the wrong way around. Somehow, though, these two reasons it shouldn’t work manage to interact in a way that precisely counteract each other, producing a working sort!