views:

380

answers:

4

How would you describe the running time of this sort, given a function sorted which returns True if the list is sorted that runs in O(n):

def sort(l):
    while not sorted(l): random.shuffle(l)

Assume shuffling is perfectly random.

Would this be written in big-O notation? Or is there some other way of categorizing algorithms with random components?

+4  A: 

Believe it or not, there's a wiki entry for this: http://en.wikipedia.org/wiki/Bogosort

Average case: O(N*N!)

Pyrolistical
And "Worst case: infinite"
yjerem
But the worst case has probability 0, which is good news for the average :-)
Steve Jessop
Worst case is highly unlikely, though. But average case still sucks :-)
tgamblin
A: 

Big-O notation assumes worst case scenario. In the worst case, this algorithm never terminates.

ykaganovich
It will eventually terminate, since random shuffle is truly random (according to the problem description), it will eventually result in the correct shuffle.
SoapBox
No, SoapBox, for this algorithm to be guaranteed to terminate, the random function must be guaranteed to produce a particular order (the sorted one). But if it has a guarantee of producing that order, then it's not truly random.
Rob Kennedy
If the shuffle is truly random, there's always a case that's worse than a given case. There's a case that will take a billion cycles; there's a case that will take 10^10^10^10^10 cycles. The "worst" case is worse than both of these! Ergo it never ends.
Artelius
The probability of this *never* terminating is zero. So you could say that you're pretty much guaranteed it'll finish :-).
tgamblin
Oh it IS guaranteed to finish - unless you get bored after a few weeks and just hit Ctrl-C.
Artelius
Big-O notation does not assume worst-case scenario. Big-O notation is often used to describe worst-case complexity, but can be used for other cases as well.
Dave L.
Big-O notation does assume worst-case scenario. Other letters (Theta, Omega) are used for other cases. http://en.wikipedia.org/wiki/Big_O_notation
ykaganovich
No. Big-O notation describes an upper bound and can be used when analyzing different cases. Theta, Omega, etc. describe other bounds and can also be applied to worst case analysis.
Dave L.
+3  A: 

This Algorithm is called Bogosort. It is an instance of a class of Algorithms called Las Vegas Algorithms. Las Vegas Algorithms are Randomized Algorithms which always guarantee correct results but make no guarantees about the computing resources.

The time-complexity of Bogosort cannot directly be expressed in Bachmann-Landau Notation, because of its probabilistic nature. However, we can make a statement about its expected time-complexity. The expected time-complexity of Bogosort is O(n·n!).

Jörg W Mittag
+1  A: 

The average case is indeed O( N N!):

Observe that there are exactly N! permutations of N elements. The probability of picking the right one is exactly 1/N!. Hence, by the strong law of large numbers, the expected number of sorts is N!.

Where does the other factor N come from? You have to check, at every step, what permutation you picked. That can be done linearly be comparing adjacent elements. Hence an extra factor N.

Comments above have indicated that O(g(n)) notation is "worst case":

1) That's not true. The definition of O(g(n)) is: f(n) is O(g(n)) if there exists some c,d such that f(n) < c * g(n) + d for sufficiently large n. There's nothing about "worst case". It just so happens that g(n) is a bigger function than f(n), but the pure mathematical definition says nothing about "case".

2) For randomized algorithms, it's pointless to do a "worst case" analysis anyway. You can come up with some execution that's going to be really bad.

3) The really bad executions occur on a set of measure 0 (A probabilist would say that they "almost surely" don't happen). They're actually impossible to observe.

Ying Xiao
"They're actually impossible to observe." - for example, quite aside from the philosophical discussions of whether a random generator is or is not guaranteed to eventually produce every output in its range, because you'd die first ;-)
Steve Jessop