Eric J Korpela wrote:
I'm sure
Knuth mentions "random-sort" as being worse (randomly
shuffle elements, check if order correct, if not then repeat).
Sounds like it
would have the smallest memory ( data and code)
footprint around.
a bit larger than a bubble sort.
But, at least it would tend to be slower.
It's called bogosort. It's O(N!) if your random number generator is perfect.
Pseudocode from the wikipedia entry:
---------------------------------------------------------------------
function bogosort(array)
while not is_sorted(array)
array := random_permutation(array)
---------------------------------------------------------------------
If we're talking about absurd algorithms, and not just absurdly *bad*
algorithms, then we should talk about the quantum bogosort algorithm as
well. I'll defer to the Jargon File regarding this one:
A spectacular variant of bogo-sort has been proposed which has the
interesting property that, if the Many Worlds interpretation of quantum
mechanics is true, it can sort an arbitrarily large array in linear
time. (In the Many-Worlds model, the result of any quantum action is to
split the universe-before into a sheaf of universes-after, one for each
possible way the state vector can collapse; in any one of the
universes-after the result appears random.) The steps are: 1. Permute
the array randomly using a quantum process, 2. If the array is not
sorted, destroy the universe (checking that the list is sorted requires
O(n) time). Implementation of step 2 is left as an exercise for the reader.
http://www.catb.org/~esr/jargon/html/B/bogo-sort.html
Woohoo!
Peace... Sridhar