Live data from Hacker News

Unconventional Sorting Algorithms

codingkaiser.blog

11–20 of 60 posts

Re: Unconventional Sorting Algorithms

#11
post #9

Isn't 'sleep sort' linear with respect to array size? O(n) sort achieved?

It is if you ignore how the underlying OS implements multitasking and sleep() in particular. And the kernel-side implementation usually involves some kind of priority queue of sleeping threads. So taken as a whole sleep-sort is just an convoluted implementation of heap-sort that outsources the actual sorting to OS.

I really like sleep sort because it trades (fundamentally) cycles and memory for time.

In theory you could do this in hardware. Hardware timers are pretty trivial, you could imagine a hardware chip that accepts a number, then writes the value to the next free slot in an array when the time has elapsed. As timers go off, the array fills up. Obviously you need to handle collisions and whatnot but nothing in this screams that you need to go O(n^2).

Re: Unconventional Sorting Algorithms

#12

Isn't 'sleep sort' linear with respect to array size? O(n) sort achieved?

It's perversely linear with respect to the largest element in the array, if that's greater than the runtime of the underlying heapsort. Good luck with negative entries.

Response to dead comment:

> Couldn't this be solved by scaling everything by the largest entry. Of course that requires first a pass to find the largest entry but that's not as expensive?

One presumes that we don't have infinite precision timers, so you should only scale it down so the smallest gap between elements is a few clock cycles (unless you're satisfied with breaking the fiction that sleepsort is not heapsort). This seems like a mildly interesting question. My intuition is that finding the smallest abs(a[i]-a[j]) with i != j should be just as hard as sorting the list on a classical computer, but it seems like a quantum computer might find the smallest gap faster.

Re: Unconventional Sorting Algorithms

#14
Quantum bogosort is probably my personal favorite. Randomly permute the input using a quantum source of entropy. If the input is not sorted, destroy the universe. The only universe remaining is one where the input was already sorted. Runs in O(n) time.

Re: Unconventional Sorting Algorithms

#16
post #10

I recently found a fun linear-time systolic sorting algorithm. I'd describe the serial implementation as unconventionally obvious. It's like insertion sort, without all the bother of swapping. def index_sort(a): n = len(a) output = [None] * n for i in range(n): #can run in parallel, damn the GIL index = 0 for j in range(n): if a[j]

> linear-time

But... there are two nested `range(n)` for loops. Typo?

Re: Unconventional Sorting Algorithms

#18
post #8

My contribution: Power Sort. Start with p=0 and add 2^n for each n. Then subtract the largest power of 2 successively to get your integers ordered. Con: p will be very big. Pro: you don't need ifs!

Ooh, that's a fun one.

Another con: you'd better hope your input contains no duplicates. :)

Re: Unconventional Sorting Algorithms

#19
My favorite is still slowsort. https://en.wikipedia.org/wiki/Slowsort

> It is a reluctant algorithm based on the principle of multiply and surrender. It was published in 1986 by Andrei Broder and Jorge Stolfi in their paper Pessimal Algorithms and Simplexity Analysis.

https://www.mipmip.org/tidbits/pasa.pdf

> The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

Re: Unconventional Sorting Algorithms

#20
post #18
post #8

My contribution: Power Sort. Start with p=0 and add 2^n for each n. Then subtract the largest power of 2 successively to get your integers ordered. Con: p will be very big. Pro: you don't need ifs!

Ooh, that's a fun one. Another con: you'd better hope your input contains no duplicates. :)

Easily solvable with a hash.

I have thought this through (I'm ashamed to admit).

Post reply on HN