Created
July 16, 2015 18:22
-
-
Save chingovan/3d4a955b2c6eba660e47 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Quick sort alogrithm | |
# Author: Chi Ngo ([email protected] - chingovan.blogspot.com) | |
def quicksort(mylist, sIndex, eIndex): | |
# Neu chi co mot phan tu hoac eIndex < sIndex | |
if eIndex <= sIndex: | |
return | |
else: | |
pivot = mylist[int((eIndex + sIndex) / 2)] | |
i = sIndex; | |
j = eIndex; | |
while i < j : | |
while mylist[i] < pivot and i <= j: | |
i += 1 | |
while mylist[j] > pivot and j >= i: | |
j -= 1 | |
if i < j: | |
temp = mylist[i] | |
mylist[i] = mylist[j] | |
mylist[j] = temp | |
i += 1 | |
j -= 1 | |
else: | |
break | |
if i == j: | |
if mylist[i] <= pivot: | |
quicksort(mylist, sIndex, i) | |
quicksort(mylist, i + 1, eIndex) | |
else: | |
quicksort(mylist, sIndex, i - 1) | |
quicksort(mylist, i, eIndex) | |
else: | |
quicksort(mylist, sIndex, j) | |
quicksort(mylist, i, eIndex) | |
# end else | |
a = [3, 6, 9, 2, 6, 8, 9, 1, 7, 21, 7, 12] | |
quicksort(a, 0, len(a) - 1) | |
print("After sorting: ", a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment