Skip to content

Instantly share code, notes, and snippets.

@venkatsgithub1
Last active July 25, 2017 18:02
Show Gist options
  • Save venkatsgithub1/65ccb83071224cd5afaffe89dcf6ae89 to your computer and use it in GitHub Desktop.
Save venkatsgithub1/65ccb83071224cd5afaffe89dcf6ae89 to your computer and use it in GitHub Desktop.
Quick sort algorithm in Python
def quicksort(templist):
if len(templist)<=1:
return templist
pivot=templist[0]
left,right=[],[]
for data in templist:
if data<pivot:
left.append(data)
elif data>pivot:
right.append(data)
left=quicksort(left)
right=quicksort(right)
# uncomment below line to see how data changes.
#print(" ".join((list(map(str,left))+[str(pivot)]+list(map(str,right)))))
return left+[pivot]+right
print(quicksort([5,8,1,3,7,9,2]))
'''take a pivot. In the above program element in first index is considered pivot.
bring items less than pivot to left.
now items greater than pivot are on right.
recurse until list size=1, base condition here: return
left+pivot+right is at the core of quicksort.'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment