Last active
September 25, 2017 01:25
-
-
Save roshangautam/ce1b66894a879a670d2be605c9b8b48f to your computer and use it in GitHub Desktop.
Quick Sort - Python
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
def quicksort(array, first, last): | |
if first < last : | |
pivot = array[first] | |
left = first + 1 | |
right = last | |
while 1 : | |
while left <= right and array[left] <= pivot : | |
left += 1 | |
while right >= left and array[right] >= pivot : | |
right -= 1 | |
if right < left : break | |
temp = array[left] | |
array[left] = array[right] | |
array[right] = temp | |
array[first] = array[right] | |
array[right] = pivot | |
quicksort(array, first, right - 1) | |
quicksort(array, right + 1, last) | |
test = [21, 4, 1, 3, 9, 20, 25, 6, 21, 14] | |
quicksort(test, 0 , len(test)-1) | |
print test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment