A few rules:* It must be a single top-level function (nested helper functions are fine) that sorts a random LIST of integers into ascending order
* Built-in sorting functions are not allowed, nor functions from another module not packaged with Python
* There are allowed to be repeats in the random sequence of integers, it must deal with this
* Right now, only Python entries will be accepted, I may open it up to other languages in the future, implementations in other languages are fine but will not be judged in the challenge.
* Python 2 or 3 is fine
* The winner will be the person who enters the fastest sorting method timed by me, there is also an award for the most creative submission.

After a few entries or a bit of interest I'll release my solution I've already written for you all to have a peek at

Happy Challenging! Let's see what you can do.

EDIT: I should really set a deadline, 2 weeks from today should be good to be get plenty of entries so Sunday 16th September 2012 will be the cut off.

Re: Sorting without Indices - Challenge!

Posted 02 September 2012 - 08:57 AM

You are allowed nested helper functions, like the merge sort example you gave. Especially if it's going to be recursive of course! What I probably meant is that I need to be able to call it as a single top-level function:

aFunction(aList)

Anything beyond that is up to you.

Any construct not involving square brackets is allowed, yep. I'd prefer if at least a little effort went in to it. The entire sorting method has to be written manually, after all.

As for in-place functions - I don't mind. If it's timeable and adheres to the rest of the rules, I'll allow it.

Re: Sorting without Indices - Challenge!

Posted 02 September 2012 - 11:21 AM

Okay, I've come up with two solutions. One is a non-cheaty implementation of mergesort:

Spoiler

def mergesort(lst):
def merge(lst1, lst2):
merged = list()
i1 = lst1.__iter__()
i2 = lst2.__iter__()
v1 = i1.__next__()
v2 = i2.__next__()
try:
while True:
if v1 <= v2:
merged.append(v1)
last = v2
v1 = i1.__next__()
else:
merged.append(v2)
last = v1
v2 = i2.__next__()
except StopIteration:
# The value in last has already been taken out of its iterator, but has not yet been
# added to the result list. Let's do that now.
merged.append(last)
# One of the two iterators is now empty, but the other may still have items
# Iterate over both iterators to get the items that are left in one of the iterators
for v in i1:
merged.append(v)
for v in i2:
merged.append(v)
return merged
if len(lst) == 1:
return lst
lst1 = list()
lst2 = list()
for i, v in enumerate(lst):
if i < len(lst)/2:
lst1.append(v)
else:
lst2.append(v)
lst1 = mergesort(lst1)
lst2 = mergesort(lst2)
return merge(lst1, lst2)

Re: Sorting without Indices - Challenge!

Posted 02 September 2012 - 11:59 AM

Haha, I see what you meant by "cheaty", but I guess it doesn't break the rules. The first one is nicely done, except one tiny thing: you created two empty lists in your code with square brackets! Change it to list() and it's perfect.

This is just a quick one bashed together, there's probably some improvements that could be made, but it seems to work for positive integers. Negative numbers, of course, would require some special attention.

EDIT: the special attention required for negative numbers turns out to be minimal. Revised code follows.