Last active
October 6, 2015 17:46
-
-
Save cupdike/b1a778384b226cc18759 to your computer and use it in GitHub Desktop.
Basic quicksort impl, inspired by http://me.dt.in.th/page/Quicksort/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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