# Thread: my new sorting algorithm - mansort

1. ## my new sorting algorithm - mansort

Code:
```import random

def mansort(x):
while not sorted?(x):
random.shuffle(x)
return x

def sorted?(x):
if not x:
return True
last = x[0]
for y in x[1:]:
if y < last:
return False
last = y
return True```
constructive criticism appreciated

2. Originally Posted by man
Code:
```import random

def mansort(x):
while not sorted?(x):
random.shuffle(x)
return x

def sorted?(x):
if not x:
return True
last = x[0]
for y in x[1:]:
if y < last:
return False
last = y
return True```
constructive criticism appreciated
It is a wonderfully whimsical sorting algorithm: there is no guarantee of termination...and the probability of termination is somewhere around ~1/n! Awesome. The average bound for the time complexity of this sort is factorial time or rather O(n*n!) because the "sorted?" function is linear time (and I am assuming python's shuffle is also linear time).

This is even more whimsical than the "lets try every permutation, till we find the sorted one", because this brute force algorithm guarantees termination, and can only get a worst-case time complexity to match your average.

Well done! I wish I was in the mood to write a perl poem describing it, but I am not.

EDIT: One failing, however, is that when it does terminate, it terminates with the correct output...but I suppose removing that would change it from an act of whimsy to just plain wrong.

3. Originally Posted by ygolo
EDIT: One failing, however, is that when it does terminate, it terminates with the correct output...but I suppose removing that would change it from an act of whimsy to just plain wrong.
Listen up brah, this isn't just code -- it's a revolution.

Why would I implement a sorting algorithm that doesn't return the correct output???

4. It's genius. It's like.. it's like it embraces the perpetual chaos of the world and locks in on the absolute perfect moment in time, where the stars just happen to align and everything just 'clicks.'

Although, it does already exist. But at least you've reaffirmed genius.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
Single Sign On provided by vBSSO