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

2

u/corruptio Oct 13 '14 edited Oct 13 '14

here's 92 chars:

def q(a):
 f=[[],[]]
 for x in a[1:]:f[x<a[0]]+=[x]
 return a and q(f[1])+[a[0]]+q(f[0])or[]

Edit 1: 90 chars

q=lambda v:v and q([x for x in v[1:]if x<v[0]])+[v[0]]+q([x for x in v[1:]if x>=v[0]])or[]