Skip to content

Instantly share code, notes, and snippets.

@nsfyn55
Created June 27, 2013 14:20
Show Gist options
  • Save nsfyn55/5876810 to your computer and use it in GitHub Desktop.
Save nsfyn55/5876810 to your computer and use it in GitHub Desktop.
recursive quicksort
def sort(l):
if len(l) <= 1 :
return l
left = []
right = []
pivot = l[len(l)/2]
for item in l[:len(l)/2]:
if item <= pivot:
left.append(item)
else:
right.append(item)
for item in l[len(l)/2+1:len(l)]:
if item <= pivot:
left.append(item)
else:
right.append(item)
return sort(left)+ [pivot] + sort(right)
l = [4, 78, 54, 2, 1, 1, 4, 15, 18, 7, 9, 2, 18]
print sort(l)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment