#快速排序 alist要排序的元素start為起始元素
def quick_sort(alist, start, end):
if start >= end:
return
# 設定起始元素為要尋找位置的基準元素
mid = alist[start]
# low為序列左邊的由左向右移動的游標
low = start
# high為序列右邊的由右向左移動的游標
high = end
while low < high:
# 如果low與high未重合,high指向的元素不比基準元素小,則high向左移動
while low < high and alist[high] >= mid:
high -= 1
alist[low] = alist[high]
while low < high and alist[low] < mid:
low += 1
alist[high] = alist[low]
alist[low] = mid
# 對基準元素左邊的子串行進行快速排序
quick_sort(alist, start, low - 1)
# 對基準元素右邊的子串行進行快速排序
quick_sort(alist, low + 1, end)
python快速排序排序 python快速排序
import random def rand n for i in range n yield random.randint 0,1000 建立乙個隨機數列表 def createlist n lists for i in rand n return lists 挖坑法快速排序 def quick ...
排序 快速排序 Python
快速排序 快排 是非常常用的排序方法,在技術面試中出現頻率也特別高。它主要採用交換和分治的策略進行排序。是不穩定排序。步驟 1 在序列中選乙個元素作為劃分的基準元素 pivot 2 將所有不大於pivot的數字放在pivot的前面,大於pivot的數字放在pivot的後面 3 以pivot為界,對前...
python快速排序
coding utf 8 class quicksort def sort self,list,left,right 開始位置小於 位置 if left 取到中間的乙個下標值 s list left right 2 i 0 i left j 下標結束 j right while true 從開始位置...