实现1:种轴partition,not in place--取定枢轴,将小于等于枢轴的放到枢轴左边,大于枢轴的放到右边
# python algorithm en_2nd edition p125
def partition_mlh(arr):
# 3-partition, use extra place
pt, seq = arr[0], arr[1:]
lt = [x for x in seq if x <= pt ]
rt = [x for x in seq if x > pt]
return lt, pt, rt
def qsort_mlh(arr):
if len(arr) <= 1: return arr
lt, pt, rt = partition_mlh(arr)
return qsort_mlh(lt) + [pt] + qsort_mlh(rt)
实现2:种轴partition,in place--取出枢轴,将小于等于枢轴的放到枢轴左边,大于枢轴的放到右边
def partition_dfq(arr, stt, end):
lt, rt = stt, end - 1 # for [stt, end)
pvt = arr[lt]
while lt < rt:
while lt < rt and arr[rt] >= pvt: rt -= 1
arr[lt] = arr[rt]
while lt < rt and arr[lt] < pvt: lt += 1
arr[rt] = arr[lt]
arr[lt] = pvt # 种轴partition
return lt
def qsort_dfq(arr, stt, end): # for [stt, end)
# if len(arr) <= 1: return arr
if end - stt < 2: return arr
pvt = partition_dfq(arr, stt, end)
qsort_dfq(arr, stt, pvt)
qsort_dfq(arr, pvt + 1, end)
return arr
实现3:比值partition,in place--以某个值为参考,大于该值的向右移,小于该值的向左移,等于该值的可能左移or右移
def qsort_zxh(arr, beg, end): #[beg, end]
# assert(len(arr) > 0 and beg >= 0 and end > beg and end < len(arr))
lt, rt = beg, end
pvt = arr[lt + (rt - lt)//2]
# pvt = arr[lt + random.randint(0, rt - lt)]
while lt < rt:
while arr[lt] < pvt and lt < end: lt += 1 # lt < end 可以写在后面
while arr[rt] > pvt and rt > beg: rt -= 1
if lt <= rt:
arr[lt], arr[rt] = arr[rt], arr[lt]
lt, rt = lt + 1, rt - 1
if lt < end: qsort_zxh(arr, lt, end)
if rt > beg: qsort_zxh(arr, beg, rt)
return arr