

Show that this extremely simple sort has sub-cubic expected running time. - amichail

Pick two elements at random.  If they are out of order, swap them.<p>How long will it take until the array is sorted?<p>UPDATE:  To turn this into an algorithm, you will need to determine how to check whether the array is sorted.
======
jibiki
Each element can be swapped _at most once_ with each other element. So the
number of swaps is less than n choose 2, i.e., quadratic. (This does not mean
that the run time is quadratic, since pairs can be chosen multiple times, and
left alone each time because they are already in order.)

EDIT: Whoops, I lied:

3,2,1 -> 2,3,1 -> 1,3,2 -> 1,2,3

2 and 3 switch twice.

Anyways, the (whole number) quantity sum_{j=1,...,n}(|x_j - j|) seems to be
decreasing under this sort, and it's initial value is O(n squared), so I guess
the algorithm should have performance O(n squared), but it will be slower in
reality because random choice is not O(1).

------
nostrademons
This sounds a lot like BogoSort, which has an average-case running time of O(n
* n!), basically exponential. Best-case is only O(n), though:

<http://en.wikipedia.org/wiki/Bogosort>

There is a variant called Quantum Bogosort, assuming the many-worlds
hypothesis, which works like this:

1\. Perform BogoSort

2\. If the array is out of order, destroy the universe.

This has an average-case running time of O(n) for the universes that survive,
at the cost of destroying n! universes. Destroying the universe is left as an
exercise to the reader.

~~~
rms
Edited for wrongness. <http://en.wikipedia.org/wiki/Bogosort#Bozo_sort>

~~~
nostrademons
The difference between Bogosort and Bozosort is where the check for sortedness
occurs - in BogoSort, you permute the whole array (by swapping individual
pairs) and _then_ check to see if it's in order, in Bozosort you swap the
pairs and then check to see if it's in order. Amichail didn't specify which
one he wanted.

