time complexity of non-inplace binary search

时间:2015-05-12 23:21:51

标签: big-o time-complexity complexity-theory binary-search

Assuming that binary search is called upon a subarray of approximately length n/2 and that there are at most three comparions at a level I came up with $('#iframe_display_modules').attr('src', ''); as a recurrence relation.

But: When I call binarySearch recursively isn't the cost of the slice proportional to n? Thus, isn't the solution then T(n) = T(n/2) + 3 rather than T(n) = nlogn?

This is confusing to me. I checked Python's cost model, and (as expected) the cost of a slice is proportional to logn.

2 个答案:

答案 0 :(得分:3)

与往常一样,算法的复杂性及其实现可能存在差异。

如果在递归函数中实现二进制搜索,它需要输入数组的一部分的副本,则必须在REQUIRED中生成此数组,所以你是对的,这将导致整体O(n)的复杂性(可能更糟糕,取决于你究竟在复制什么)。

所以你不应该这样实现它。仅移交对数组的引用以及最小和最大索引值,以将搜索限制为所需的数组部分。 (或类似的东西)

答案 1 :(得分:-1)

看看Master Theorem

client = Twitter::REST::Client.new do |config|
  config.consumer_key        = "YOUR_CONSUMER_KEY" #I added my rail app at twitter dev center
  config.consumer_secret     = "YOUR_CONSUMER_SECRET" #idem
  config.access_token        = "YOUR_ACCESS_TOKEN" #from the ios app
  config.access_token_secret = "YOUR_ACCESS_SECRET" #from the ios app
end

因此

T(n) = T(n/2) + 3
log a / log b = 1
d = 1