[英]Time complexity for n-ary search.
I am studying time complexity for binary search, ternary search and k-ary search in N elements and have come up with its respective asymptotic worse case run- time.我正在研究 N 元素中二元搜索、三元搜索和 k 元搜索的时间复杂度,并提出了其各自的渐近最坏情况运行时间。 However, I started to wonder what would happen if I divide N elements into n ranges (or aka n-ary search in n elements).
但是,我开始想知道如果我将 N 个元素分成 n 个范围(或 n 个元素中的 n 元搜索)会发生什么。 Would that be a sorted linear search in an array which would result in a run-time of O(N)?
这会是一个数组中的排序线性搜索,它会导致 O(N) 的运行时间吗? This is a little confusing.
这有点令人困惑。 Please help me out!
请帮帮我!
What you say is right.你说的是对的。
For a k-ary
search we have:对于
k-ary
搜索,我们有:
k-1
checks in boundaries to isolate one of the k
ranges.k-1
检查以隔离k
范围之一。 Hence the time complexity is essentially O((k-1)*log_k(N))
where log_k(N)
means ' log(N)
to base k
'.因此,时间复杂度本质上是
O((k-1)*log_k(N))
,其中log_k(N)
表示' log(N)
到基数k
'。 This has a minimum when k=2
.当
k=2
时,这具有最小值。
If k = N
, the time complexity will be: O((N-1) * log_N(N))
= O(N-1)
= O(N)
, which is the same algorithmically and complexity-wise as linear search.如果
k = N
,时间复杂度将为: O((N-1) * log_N(N))
= O(N-1)
= O(N)
,这在算法和复杂度方面与线性搜索相同。
Translated to the algorithm above, it is:转化为上面的算法,就是:
N-1
checks in boundaries (each of the first N-1
elements) to isolate one of the N
ranges.N-1
元素中的每一个)中执行N-1
检查以隔离N
范围之一。 This is the same as a linear search in the first N-1
elements.N-1
元素中的线性搜索相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.