python中位数代码_python:中位数为3的快速排序

本文详细介绍了如何在Python中优化快速排序算法,通过确定三个数的中位数来选择枢轴元素,避免了原始代码的问题,并提供了一个更简洁的解决方案,将枢轴移到子列表的开头,从而简化了交换过程。
摘要由CSDN通过智能技术生成

让我们首先实现三个数的中值,所以是一个独立的函数。我们可以对三个元素的列表进行排序,然后返回第二个元素,如:

def median_of_three(a, b, c):

return sorted([a, b, c])[1]

现在是一个范围

low .. high

low

包括,和

high

不包括在内),我们应该确定哪些元素是我们应该构建三个中值的元素:

第一个要素:

L[low]

,

最后一个元素

L[high-1]

中间元素(如果有两个这样的元素,则取第一个)

L[(low+high-1)//2]

.

所以现在我们只需要将分区函数修补到:

def Partition(L, low, high, ascending = True):

print('Quicksort, Parameter L:')

print(L)

result = 0

pivot = median_of_three(L[low], L[(low+high-1)//2], L[high-1])

i = low + 1

for j in range(low + 1, high, 1):

result += 1

if (ascending and L[j] < pivot) or (not ascending and L[j] > pivot):

L[i], L[j] = L[j], L[i]

i += 1

L[low], L[i-1] = L[i-1], L[low]

return i - 1, result

编辑

:确定三个元素的中值。

三个元素的中间值是位于其他两个值中间的元素。所以以防万一

a <= b <= c

然后

b

是中间值。

所以我们需要确定元素的顺序,这样我们就可以确定中间的元素。像:

def median_of_three(a, b, c):

if a <= b and b <= c:

return b

if c <= b and b <= a:

return b

if a <= c and c <= b:

return c

if b <= c and c <= a:

return c

return a

现在我们定义了三和四的中值

if

病例。

编辑2

:这个还是有问题的。执行轴操作后,交换元素

L[i-1]

具有

低[低]

在原始代码中(轴的位置)。但这当然不再起作用:因为轴心现在可以位于三维中的任何一个。因此我们需要

median_of_three(..)

更聪明:它不仅应该返回pivot元素,还应该返回pivot的位置:

def median_of_three(L, low, high):

mid = (low+high-1)//2

a = L[low]

b = L[mid]

c = L[high-1]

if a <= b <= c:

return b, mid

if c <= b <= a:

return b, mid

if a <= c <= b:

return c, high-1

if b <= c <= a:

return c, high-1

return a, low

现在我们可以用以下方法解决这个问题:

def Partition(L, low, high, ascending = True):

print('Quicksort, Parameter L:')

print(L)

result = 0

pivot, pidx = median_of_three(L, low, high)

i = low + (low == pidx)

for j in range(low, high, 1):

if j == pidx:

continue

result += 1

if (ascending and L[j] < pivot) or (not ascending and L[j] > pivot):

L[i], L[j] = L[j], L[i]

i += 1 + (i+1 == pidx)

L[pidx], L[i-1] = L[i-1], L[pidx]

return i - 1, result

编辑3

:清理干净。

尽管上面的方法似乎有效,但它相当复杂:我们需要

i

j

“跳过”轴的位置。

如果我们首先将轴心移到子列表的前面(这样

低的

指数):

def Partition(L, low, high, ascending = True):

print('Quicksort, Parameter L:')

print(L)

result = 0

pivot, pidx = median_of_three(L, low, high)

L[low], L[pidx] = L[pidx], L[low]

i = low + 1

for j in range(low+1, high, 1):

result += 1

if (ascending and L[j] < pivot) or (not ascending and L[j] > pivot):

L[i], L[j] = L[j], L[i]

i += 1

L[low], L[i-1] = L[i-1], L[low]

return i - 1, result

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值