Skip to content

Instantly share code, notes, and snippets.

@cupdike
Last active October 6, 2015 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cupdike/b1a778384b226cc18759 to your computer and use it in GitHub Desktop.
Save cupdike/b1a778384b226cc18759 to your computer and use it in GitHub Desktop.
Basic quicksort impl, inspired by http://me.dt.in.th/page/Quicksort/
# Inspired by: http://me.dt.in.th/page/Quicksort/
def quicksort(l):
if len(l) < 2:
return l
iSwap = 1
pivot = l[0] # left most value is the pivot
for i, val in enumerate(l[1:], start=1): # Skip the pivot cell
if val < pivot:
l[i], l[iSwap] = l[iSwap], l[i]
iSwap += 1
l[0], l[iSwap-1] = l[iSwap-1], l[0] # final swap
# we need to go deeper (on the unsorted right and left halves)
return quicksort(l[:iSwap-1]) + [l[iSwap-1]] + quicksort(l[iSwap:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment