Created
August 20, 2016 16:59
-
-
Save sbsatter/4fcdc6ff9d74d220d7d9f70550b82f6b to your computer and use it in GitHub Desktop.
Quicksort algorithm in python
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
def partition(alist, first, last): | |
pivotValue= alist[first] | |
left= first+1 | |
right=last | |
done= False | |
while not done: | |
while alist[left]<pivotValue and left<right: | |
left=left+1 | |
while alist[right]>pivotValue and left<=right: | |
right=right-1 | |
if left>=right: | |
done= True | |
else: | |
alist[left],alist[right]=alist[right],alist[left] | |
alist[first],alist[right]=alist[right],alist[first] | |
return right | |
def quickSortHelper(alist, first, last): | |
if first<last: | |
splitpoint= partition(alist, first, last) | |
quickSortHelper(alist, first, splitpoint-1) | |
quickSortHelper(alist, splitpoint+1, last) | |
def quicksort(alist): | |
quickSortHelper(alist, 0, len(alist)-1) | |
print(alist) | |
alist=[5,4,3,2,1,0] | |
quicksort(alist) | |
alist = [54,26,93,17,77,31,44,55,20] | |
quicksort(alist) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment