def quicksort(nums):
if len(nums) <= 1:
return nums
# 左子数组
less = []
# 右子数组
greater = []
# 基准数
base = nums.pop()
print('sssssssss')
print(base)
# 对原数组进行划分
for x in nums:
if x < base:
less.append(x)
else:
greater.append(x)
# 递归调用
return quicksort(less) + [base] + quicksort(greater)
def main():
nums = [6,1,2,7,9,3,4,5,10,8]
print(quicksort(nums))
main()
输出:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]