• You are currently viewing our forum as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to additional post topics, communicate privately with other members (PM), view blogs, respond to polls, upload content, and access many other special features. Registration is fast, simple and absolutely free, so please join our community today! Just click here to register. You should turn your Ad Blocker off for this site or certain features may not work properly. If you have any problems with the registration process or your account login, please contact us by clicking here.

my new sorting algorithm - mansort

man

New member
Joined
Sep 16, 2009
Messages
330
MBTI Type
IntP
Enneagram
=)
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
 

ygolo

My termites win
Joined
Aug 6, 2007
Messages
5,998
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.
 

man

New member
Joined
Sep 16, 2009
Messages
330
MBTI Type
IntP
Enneagram
=)
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???
 
G

garbage

Guest
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.
 
Top