快速排序
#也是用到过,找出数组中最大值
def find_bigest(arr):
if(len(arr) == 1):
return arr[0]
if(arr[0] > arr[-1]):
return find_bigest(arr[:-1])
else:
return find_bigest(arr[1:])
_ = [1,45,467,123,8,2,63,]
print(find_bigest(_))
import FindBigest
def quick_sort(arr,max):
if len(arr) == 2:
if arr[1] < arr[0]:
arr = arr[::-1]
return arr
arr.remove(max)
return quick_sort(arr,FindBigest.find_bigest(arr)) + [max]
_ = [1,43,64,12,843,21,13,56]
print(quick_sort(_,FindBigest.find_bigest(_)))
上面是自己想的,跑题了,感觉写的是选择排序
def quick_sort(arr):
if len(arr) < 2:
return arr
else:
piovt = arr[0]
less = [i for i in arr[1:] if i < piovt]
great = [i for i in arr[1:] if i > piovt]
return quick_sort(less) + [piovt] + quick_sort(great)
_ = [1,43,64,12,843,21,13,56]
print(quick_sort(_))
看了答案之后写的