且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

多少次比较会使用该算法的最坏情况下的二进制搜索做什么呢?

更新时间:2023-09-16 17:13:13

在最坏的情况下在这种情况下,如果元素K不present A和比A.然后我们有两个要素都较小在每一步的比较: K> A [M] K< A [M]

The worst-case in this case is, if the element K is not present in A and smaller than all elements in A. Then we have two comparisons in each step: K > A[m] and K < A[m].

有关在每个步骤中,阵列被切成两部分,每部分的大小(N-1)/ 2 ,我们有一个最大的 log_2(N-1)步骤。

For in each step the array is being cut into two parts, each of the size (n-1)/2, we have a maximum of log_2(n-1) steps.

这导致了总 2 * log_2(N-1)比较,这渐近实际上等于 O(日志(N))

This leads to a total of 2*log_2(n-1) comparisons, which asymptotically indeed equals to O(log(n)).