Created
August 19, 2015 06:29
-
-
Save zhangys-lucky/4ded069d6fe61fad5e5b to your computer and use it in GitHub Desktop.
quick sort implement using 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
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
def sub_sort(array,low,high): | |
key = array[low] | |
while low < high: | |
while low < high and array[high] >= key: | |
high -= 1 | |
while low < high and array[high] < key: | |
array[low] = array[high] | |
low += 1 | |
array[high] = array[low] | |
array[low] = key | |
return low | |
def quick_sort(array,low,high): | |
if low < high: | |
key_index = sub_sort(array,low,high) | |
quick_sort(array,low,key_index) | |
quick_sort(array,key_index+1,high) | |
if __name__ == '__main__': | |
array = [8,10,9,6,4,16,5,13,26,18,2,45,34,23,1,7,3] | |
print(array) | |
quick_sort(array,0,len(array)-1) | |
print(array) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment