Unconventional Sorting Algorithms
codingkaiser.blog
codingkaiser.blog
> 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.
> The probability of the original input list being in the exact order it's in is 1/(n!). There is such a small likelihood of this that it's clearly absurd to say that this happened by chance, so it must have been consciously put in that order by an intelligent Sorter. Therefore it's safe to assume that it's already optimally sorted in some way that transcends our naïve mortal understanding of "ascending order". Any attempt to change that order to conform to our own preconceptions would actually make it less sorted.
https://www.dangermouse.net/esoteric/intelligentdesignsort.h...
Given a random shuffle of a list, what are the chances that another shuffle would be more sorted? There's a life lesson in here, I know it...
EDIT: If you allow for the objects to spontaneously sort themselves, the runtime is O(1).
Or rather a reality wherein it wasn't sorted doesn't exist anymore.
def cosmicraysort(list):
while not sorted(list):
pass def qisort(list):
qshuffle(list)
if not sorted(list):
kill_observer()
Only in the universe in which th the list is sorted, the user will survive. There might be no such universe, but then the user is no longer waiting on the result.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!
Another con: you'd better hope your input contains no duplicates. :)
I have thought this through (I'm ashamed to admit).
For an efficient implementation, one might want to round L+1 up to the nearest power of 2 to get crucial micro-optimisations based on instructions for bit scanning.
(I think this ends up being a very complicated phrasing of a counting sort.)
If you imagine that there's no "easy" algorithm to return a sorted list (but sorted lists do exist), then the only methodology possible is to try all combinations. And you do this either systematically, or randomly.
Random NP-complete algorithms, such as WalkSAT, are quite good in practice. Systematic NP-complete algorithms, such as DPLL-SAT, are more "obvious" in how they work, and are sufficient for smaller sizes.
def everett_sort(list):
shuffle(list)
if is_sorted(list):
return
else:
suicide() 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] < a[i]:
index += 1
elif a[j] == a[i] and j < i:
index += 1
output[index] = a[i]
return outputBut... there are two nested `range(n)` for loops. Typo?
import ctypes
def mutation_sort(a):
for i, v in enumerate(a):
ctypes.c_int.from_address(id(v) + 24).value = i # for x86-64, adjust the offset for your platform
return a
def is_sorted(a):
prev = None
for v in a:
if prev is not None and prev > v: return False
prev = v
return True
is_sorted(mutation_sort([3000, 1000, 7000, 8000, 2000])) # prints Truehttps://gist.github.com/ncw/5419af0e255d2fb62b98
It's a Go channel based quicksort. It sorts a channel of ints using O(n) go routines! Interestingly it doesn't need to know how many ints are in the channel at the start.
Not a practical sort method but fun to see the Go concurrency primitives in use.
Really easy: you see it, then you say it, then it’s sorted.
https://www.btp.police.uk/police-forces/british-transport-po... (this is a famous national campaign on the British railways over the past few years)
Having an array and allocating "n"=="array length" qubits for it then implementing parallel version of randomsort (no while, while is serialization!) will be the fastest sorting algorithm.
I wonder what the author, "hilariously" making one sorting function print "<val> sent to gulag" and calling it "stalin-sort", would think of "hitler-sort" which prints "<val> burned in oven"?
Please don't normalize mass-murderers, even for memes.
O(n) sort achieved?
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).
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.
Stay at your index!!!
for j = 1 to n do
if A[i] < A[j] then
swap A[i] and A[j
PUSH A[j]; PUSH A[i]; POP A[j]; POP A[i];