[英]How to find the number of iterations of binary search algorithm?
How I can get the number of iterations of binary search?如何获得二分搜索的迭代次数?
This is my code:这是我的代码:
int main()
{
int target = 11;
int N = 10;
std::vector<int> index;
int num;
for (int i = 0; i < N; i++) {
index.push_back(i);
}
int counter = 0;
unsigned int M, L = (unsigned int)-1, R = N;
M = (R - L) / 2; // Assume N is not zero
do {
int value;
M = M + L;
value = index[M];
if (value < target) L = M; else R = M;
M = (R - L) / 2;
counter++;
} while (M); // M is the size of the current interval
std::cout << R << '\n';
std::cout << "counter: " << counter << '\n';
system("pause");
return 0;
}
I want to know the number of iterations depending on N
.我想知道取决于
N
的迭代次数。 I know how this algorithm works but I want the number of iterations represented mathematically.我知道这个算法是如何工作的,但我想要以数学方式表示的迭代次数。
I would go for a recursive increment of by using a recursive binary search function.我会通过使用递归二进制搜索函数来进行递归增量。 In each branch of the binary checks, just increment by one and that will count the iterations recursively.
在二进制检查的每个分支中,只需递增 1 即可递归计算迭代次数。
#include <iostream>
#include <vector>
std::size_t binarySearch(
const std::vector<int>& arr, // pass array as non-modifiyable(const ref)
std::size_t start, std::size_t end, // start and end indexes of the array
const int target) // target to find
{
if (arr.size() == 1) return arr[0] == target ? 1 : 0; // edge case
if (start <= end)
{
const std::size_t mid_index = start + ((end - start) / 2);
return arr[mid_index] == target ? 1 : // found the middle element
arr[mid_index] < target ?
binarySearch(arr, mid_index + 1, end, target) + 1: // target is greater than mid-element
binarySearch(arr, start, mid_index - 1, target) + 1; // target is less than mid-element
}
return 0;
}
int main()
{
int target = 11;
const int N = 10;
std::vector<int> index;
index.reserve(N); // reserve some memory
for (int i = 0; i < N; i++) {
index.push_back(i);
}
std::cout << "counter: " << binarySearch(index, 0, index.size() - 1, target) << std::endl;
return 0;
}
Output :输出:
counter: 4
数学上可能的最大迭代(假设只有整数类型的情况)是 = ceil( log2 ( initial_r - initial_l ) ) 对数的基数是 2 因为每次我们通过取一个中间值并切换到一半来将我们的范围减半。
I have been trying to wrap my head around the logarithmic conceptualization too, and this is how I try to understand the answer我也一直试图围绕对数概念化进行思考,这就是我试图理解答案的方式
From https://en.wikipedia.org/wiki/Binary_search_algorithm来自https://en.wikipedia.org/wiki/Binary_search_algorithm
In mathematics, the binary logarithm (log 2 n) is the power to which the number 2 must be raised to obtain the value n.
在数学中,二进制对数 (log 2 n) 是数字 2 必须进行的幂才能获得值 n。 That is, for any real number x,
也就是说,对于任何实数 x,
x=log 2 n <=equivalent to=> 2 x =nx=log 2 n <=等价于=> 2 x =n
& &
Every binary tree with n leaves has height at least log 2 n, with equality when n is a > power of two and the tree is a complete binary tree.
每棵有 n 个叶子的二叉树的高度至少为 log 2 n,当 n 是 > 2 的幂并且这棵树是一棵完全二叉树时相等。
Binary search is kind of like walking down a binary search tree, and halving the nodes, and that series is a logarithmic (base 2) series (my own understanding, no citation, can be erroneous)二分搜索有点像沿着二叉搜索树走,然后将节点减半,该系列是对数(以 2 为底)系列(我自己的理解,没有引用,可能是错误的)
And then from https://www.cct.lsu.edu/~sidhanti/tutorials/data_structures/page305.html然后从https://www.cct.lsu.edu/~sidhanti/tutorials/data_structures/page305.html
a perfect binary tree of height h has exactly 2 h+1 -1 internal nodes.
高度为 h 的完美二叉树正好有 2 h+1 -1 个内部节点。 Conversely, the height of a perfect binary tree with n internal nodes is log 2 (n+1).
相反,具有 n 个内部节点的完美二叉树的高度为 log 2 (n+1)。 If we have a search tree that has the shape of a perfect binary tree, then every unsuccessful search visits exactly h+1 internal nodes, where h=log 2 (n+1).
如果我们有一个具有完美二叉树形状的搜索树,那么每次不成功的搜索都会访问恰好 h+1 个内部节点,其中 h=log 2 (n+1)。
(again following up with my own understanding...) (再次跟进我自己的理解......)
So, to reach a node N (with value N as per binary search tree?), you would make log 2 (N+1) iterations (traverse that many levels down the tree) in worst case (finding is still a probability, hence "worst case" phrasing).因此,要到达节点 N(每个二叉搜索树的值为 N?),在最坏的情况下,您将进行 log 2 (N+1) 次迭代(遍历树的多个级别)(找到仍然是一个概率,因此“最坏情况”的措辞)。
Dry run here (by constructing a small BST and counting manually): https://www.cs.usfca.edu/~galles/visualization/BST.html在这里试运行(通过构建一个小的 BST 并手动计数): https : //www.cs.usfca.edu/~galles/visualization/BST.html
(answer open for review/confirmations/corrections in phrasing/calculations too ofcourse as I am trying to consolidate different resources to reach a theory that makes sense in this context) (回答开放以供审查/确认/更正措辞/计算当然,因为我试图整合不同的资源以得出在这种情况下有意义的理论)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.