算法导论第三版,9.3
import random
import math
#returns the number of elements that smaller than x
#the input is A[p...r] inclusive in the convention of book, 1 <= p <= r <= n
#in Python, to represent A[p...r], we should use a[p-1:r]
def partition(a,p,r,x):
low = [m for m in a if m < x]
high = [m for m in a if m > x]
a[p-1:r] = low + [x] + high
r

本文探讨了如何使用Python实现最坏情况下运行时间为线性的选择算法,该算法来源于《算法导论》第三版第9.3章节。通过详细步骤解析,帮助读者理解并掌握这一高效的选择方法。
最低0.47元/天 解锁文章
3733





