Python 重心列表搜索?
假设我有一个隐藏的数字列表,例如:Python 重心列表搜索?,python,list,search,Python,List,Search,假设我有一个隐藏的数字列表,例如: [3397, 3343, 3297, 3251, 3215, 3159, 3107, 3061, 3029, 2979, 2939, 2879, 2829, 2781, 2733, 2673, 2615, 2579, 2633, 2669, 2713] 我看不到名单;我只能探测单个值。如何用最少的探针数找到最小的值 我所知道的列表是,它有重心,也就是说,它们从两侧朝着这个位置排序。在上述情况下,列表从位置17(值2579)向两个方向向外排序 基于P
[3397, 3343, 3297, 3251, 3215,
3159, 3107, 3061, 3029, 2979,
2939, 2879, 2829, 2781, 2733,
2673, 2615, 2579, 2633, 2669,
2713]
我看不到名单;我只能探测单个值。如何用最少的探针数找到最小的值
我所知道的列表是,它有重心,也就是说,它们从两侧朝着这个位置排序。在上述情况下,列表从位置17(值2579)向两个方向向外排序
基于Prune建议:
def mini(lst, low, high):
zrange = (high-low)
if zrange <= 3 : return min(lst[low:high])
i25 = int(low + zrange*0.25); i50 = int(low + zrange*0.5); i75 = int(low + zrange*0.75)
q25 = lst[i25]; q50 = lst[i50]; q75 = lst[i75]
if q25 <= q50 <= q75 : high = i50
elif q25 >= q50 >= q75 : low = i50
elif q25 > q50 < q75 :
low = i25; high = i75
return mini(lst, low, high)
def minis(lst):
low = 0; high = len(lst)
return mini(lst, low, high)
def mini(低、低、高):
zrange=(高-低)
如果zrange q50这样我就可以重用探测器。您可以使用二进制分治算法。探测列表的三个四分位位置。称他们为a、b、c;让端点为
L
和R
。鉴于列表已部分排序,您有以下可能性和操作:
a < b < c
The low point must be to the left of `b`.
Recur on the list from `L` to `b`.
You already have `a` as the midpoint of this slice.
a > b > c
The low point must be to the right of `b`.
Recur on the list from `b` to `L`.
You already have `c` as the midpoint of this slice.
a > b < c
The low point must be between `a` and `c`.
Recur on that portion of the list.
You already have `b` as the midpoint of this slice.
ab>c
低点必须在“b”的右边。
在列表中从'b'到'L'重复出现。
您已经将'c'作为该切片的中点。
a>b
我相信你可以处理基本情况,当你没有未经处理的元素剩余
由于每次都将列表减半,因此将有log2(n)次迭代,每次迭代都会有2个额外的探测。第一个探测器上有一个额外的探测器,最后一个可能只有一个。您可以使用二进制分治算法。探测列表的三个四分位位置。称他们为a、b、c;让端点为
L
和R
。鉴于列表已部分排序,您有以下可能性和操作:
a < b < c
The low point must be to the left of `b`.
Recur on the list from `L` to `b`.
You already have `a` as the midpoint of this slice.
a > b > c
The low point must be to the right of `b`.
Recur on the list from `b` to `L`.
You already have `c` as the midpoint of this slice.
a > b < c
The low point must be between `a` and `c`.
Recur on that portion of the list.
You already have `b` as the midpoint of this slice.
ab>c
低点必须在“b”的右边。
在列表中从'b'到'L'重复出现。
您已经将'c'作为该切片的中点。
a>b
我相信你可以处理基本情况,当你没有未经处理的元素剩余
由于每次都将列表减半,因此将有log2(n)次迭代,每次迭代都会有2个额外的探测。第一个上有一个额外的探测,最后一个上可能只有一个。似乎您想进行某种类型的二进制搜索。我想试试。看起来你想做一些二进制搜索。我想试试。