top | item 25408380

Bubblesort, rocksort, and cocktail-shaker sort

64 points| ingve | 5 years ago |quuxplusone.github.io | reply

45 comments

order
[+] KMag|5 years ago|reply
Quantum Bogosort[0] sorts in O(N) time, but the destroy_universe() step is only safe if the Many Worlds interpretation of Quantum Mechanics is correct.

[0] https://en.wikipedia.org/wiki/Bogosort#Quantum_bogosort

[+] steerablesafe|5 years ago|reply
Also you you need "real" (in the quantum sense) random generator for the shuffling. If you use a PRNG then you risk destroying all the universes.
[+] YokoZar|5 years ago|reply
If you're using a cloud host that doesn't charge you anything for instances with a very low runtime, you can implement quantum bogosort for free - just have every unsorted instance self-destruct promptly.
[+] bluGill|5 years ago|reply
You can trivially parallelize this algorithm to be O(1): for each permutation you create universes to check all finite lengths of the list in parallel and destroy the universes that check either less elements than are in the list or that run off the end of the list.

I'm not sure if this can extend to infinite size lists. Sorting infinite lists in finite time makes my head hurt.

[+] jedisct1|5 years ago|reply
The sleep sort only requires n operations.

for x in "$@"; do (sleep $x; echo $x) &; done

[+] ironmagma|5 years ago|reply
Computationally, this (ab)uses the scheduler to do your sorting for you. So really the performance becomes whatever your scheduler’s performance is, probably heapsort time (O(n log n)).
[+] bluGill|5 years ago|reply
Although if your computer is slow compared to the size of the list this will not work due to race conditions. Still cleaver.
[+] avocad|5 years ago|reply
That was a great article! The last paragraph, however:

"By the way, Knuth is interested in these algorithms mainly for the interesting mathematical problems (...) not because they’re in any way efficient algorithms! "

Made me wonder, these kind of algorithms, where there is a lot of comparing and swapping of adjacent records might be more efficient on a tape-based architecture. Or an architecture with a lot of prefetch caching.

[+] vanderZwan|5 years ago|reply
Insertion sort is known to be the fastest option for small ranges, which is why it often is the sorting option hybrid sorts switch to for that case (like when the partitions in quicksort or merge sort are small enough).

AFAIK, this is mainly because even though it is expected O(n²), it has a really tiny overhead because insertion sort is incredibly simple, so for small enough n it wins. However, being very cache-friendly is also part of it (it's a linear search over the array).

IIRC some people found similar results for bubble sort.

[+] bestboy|5 years ago|reply
just want to quickly share an IMHO interesting sorting algorithm visualization video: https://youtu.be/kPRA0W1kECg

"Visualization and "audibilization" of 15 Sorting Algorithms in 6 Minutes. Sorts random shuffles of integers, with both speed and the number of items adapted to each algorithm's complexity. The algorithms are: selection sort, insertion sort, quick sort, merge sort, heap sort, radix sort (LSD), radix sort (MSD), std::sort (intro sort), std::stable_sort (adaptive merge sort), shell sort, bubble sort, cocktail shaker sort, gnome sort, bitonic sort and bogo sort (30 seconds of it)."

[+] ogogmad|5 years ago|reply
Bubblesort appears to be inferior to insertion sort in every single way. It's more complicated, it's slower in every situation. I don't know why you'd ever use it. What's the point of it?
[+] LordDragonfang|5 years ago|reply
The "point" is that it is one of the three intuitive sorting algorithms that someone unfamiliar with algorithmic complexity will write when asked to write a computer sorting algorithm (and happened to be the first sort I wrote when tasked with just that in college long ago). This makes it a useful thing to discuss because it's so clearly inferior to the other two despite being just as intuitive.
[+] taneq|5 years ago|reply
It's good when there are a lot of elements off by a short distance in a mostly sorted list. The go-to use case that I've seen is depth sorting for 3D rendering on really low end hardware. Polygons usually don't move much in the Z-order each frame, so you run a few passes of bubblesort per frame for a good-enough linear-time sort.
[+] jansan|5 years ago|reply
I Germany we have a saying that goes "Nobody is completely useless - you can still serve as a bad example."
[+] mahmoudhossam|5 years ago|reply
It's easier to teach and visualize, that's why it's usually taught as the first algorithm to computer science students.
[+] mortehu|5 years ago|reply
You might need bubble sort if you have to rearrange a train using a turntable that can fit exactly two cars.
[+] fefe23|5 years ago|reply
I feel that if you have to go to Stackoverflow after reading Knuth, maybe you didn't actually read the Knuth? Just skimmed it?<p>Not trying to piss on the OP here, but the idea with the Knuth books is that you spend time with them. Not just reading the table of contents and then going to Stackoverflow.<p>In fact, the reason for having books by authorities in the field like Knuth is so you become less dependent on places like Stackoverflow that replace factual validation by popularity validation. If you really did read Knuth you should be able to produce content on Stackoverflow instead of consuming it.
[+] xg15|5 years ago|reply
OP being in a book club - and then even writing a blog post about their takeaways - suggest to me that they did read it carefully.

No book is perfect and everything has a different level of knowledge that they have to integrate the new information from the book with. It seems reasonable to me to go to Stackoverflow to clear up questions, especially if you have such a well-known topic.

> If you really did read Knuth you should be able to produce content on Stackoverflow instead of consuming it.

Nope. Reading is just the first step. Then you have to find out whether or not you actually understood things correctly by discussing what you read and/or getting your hands dirty. After you've gained some experience, then you can start answering questions from others.