Skip to content

Instantly share code, notes, and snippets.

@zhuifengshen
Created November 27, 2018 02:54
Show Gist options
  • Save zhuifengshen/1010065c520bca6c4a1ab9164b5d00b9 to your computer and use it in GitHub Desktop.
Save zhuifengshen/1010065c520bca6c4a1ab9164b5d00b9 to your computer and use it in GitHub Desktop.
快速排序算法
实现一:
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