arr = [int(i) for i in input().split()]
def quick_sort(start, end):
global arr
if start >= end:
return
# Select the pivot
pivot = arr[start]
low, high = start, end
# Exchange pivot left side and right side
while low < high:
# Find the element on the right side that are smaller than pivot
while low < high and arr[high] >= pivot:
high -= 1
# Move to the left side
arr[low] = arr[high]
# Find the element on the left side taht are larger than pivot
while low < high and arr[low] < pivot:
low += 1
# Move to the right side
arr[high] = arr[low]
# Fit the pivot in the correct position
arr[low] = pivot
# Sort the left part
quick_sort(start, low-1)
# Sort the right part
quick_sort(low+1, end)
quick_sort(0, len(arr) - 1)
print(arr)
Python - 快速排序
最新推荐文章于 2024-11-02 16:28:26 发布