Python 3 實現(xiàn):
def quick_sort(left, right, nums):
if left >= right:
return
# 選擇最左邊的元素為基準數(shù)
pivot = nums[left]
move_left = left
move_right = right
while move_left < move_right:
# 當基準數(shù)在左端時辨萍,要先移動 move_right禀忆,因為 move_right 找到的數(shù)是可以直接和 pivot 交換的
while nums[move_right] < pivot and move_left < move_right:
move_right -= 1
while nums[move_left] >= pivot and move_left < move_right:
move_left += 1
if move_left < move_right:
temp = nums[move_left]
nums[move_left] = nums[move_right]
nums[move_right] = temp
nums[left] = nums[move_left]
nums[move_left] = pivot
quick_sort(left, move_left - 1, nums)
quick_sort(move_left + 1, right, nums)
if __name__ == '__main__':
nums = [4, 0, 1, 2, 3, 5, 6, 7, 8, 9]
quick_sort(0, len(nums) - 1, nums)
print(nums)