简体   繁体   English

n 元搜索的时间复杂度。

[英]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搜索,我们有:

  1. Do k-1 checks in boundaries to isolate one of the k ranges.在边界中执行k-1检查以隔离k范围之一。
  2. Jump into the range obtained from above.跳入从上面获得的范围。

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:转化为上面的算法,就是:

  1. Do 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元素中的线性搜索相同。
  2. Jump into the range obtained from above.跳入从上面获得的范围。 This is the same as checking the last element (in constant time).这与检查最后一个元素(在恒定时间内)相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM