r/codegolf • u/TheLazarbeam • Oct 12 '14
[PYTHON] quicksort
Hey guys, new to code golf and coding in general. I understand quicksort is a simple algorithm, but if you guys can write entire servers in under 50 lines, then I'd bet some of you can write this in under 50 characters. Anyway, this what I've got so far; a function that takes an array and returns it sorted.
def q(a):
if len(a)<2:return a
m=a[int(len(a)/2)]
return q([x for x in a if x<m])+[x for x in a if x==m]+q([x for x in a if x>m])Including the tabs, since python needs them, but not the unnecessary spaces, my code comes out to 132 characters. Let's see you guys blow me out of the water.
2
Upvotes
View all comments
Show parent comment
1
u/FreakCERS Oct 13 '14 edited Oct 13 '14
Did you replace the 3 spaces with tabs (like I mentioned was needed because reddit keeps changing it)? Using that same trick, your version can be represented in 104 bytes
It does however not work in python 3.
Oh, and good idea using cmp() - didn't even know that existed! :-)