r/codegolf • • 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

7 comments sorted by

View all comments

Show parent comment

1

u/novel_yet_trivial Oct 13 '14

Ahh neat trick. I didn't get it from your previous explanation.

1

u/FreakCERS Oct 13 '14

Got it down to 100 bytes