Created
November 27, 2018 02:54
-
-
Save zhuifengshen/1010065c520bca6c4a1ab9164b5d00b9 to your computer and use it in GitHub Desktop.
快速排序算法
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 quicksort(data): | |
if len(data) < 2: | |
return data | |
pivot = data[1] | |
less = [i for i in data[1:] if i <= pivot] | |
greater = [i for i in data[1:] if i > pivot] | |
return quicksort(less) + pivot + quicksort(greater) | |
实现二: | |
def quicksort(data, start, end): | |
i = start | |
j = end | |
key = data[i] | |
while i < j: | |
while i < j and data[j] >= key: | |
j = j - 1 | |
data[i] = data[j] | |
while i < j and data[i] < key: | |
i = i + 1 | |
data[j] = data[i] | |
data[i] = key | |
if i-1 > start: | |
quicksort(data, start, i-1) | |
if i+1 < end: | |
quicksort(data, i+1, end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment