如何找到二分查找算法的迭代次数?
How to find the number of iterations of binary search algorithm?
如何获取二分查找的迭代次数?
这是我的代码:
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;
}
我想知道 N
的迭代次数。
我知道这个算法是如何工作的,但我想要迭代次数
用数学表示。
我会通过使用递归二进制搜索函数来递归递增。
在二进制检查的每个分支中,只需递增 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;
}
输出:
counter: 4
数学上可能的最大迭代(假设只有整数类型的情况)是 = ceil( log2 ( initial_r - initial_l ) ) log 的底数是 2,因为每次我们都在潜水我们的范围一半的一半,然后切换到一半。
我也一直在努力思考对数概念化,这就是我试图理解答案的方式
来自 https://en.wikipedia.org/wiki/Binary_search_algorithm
In mathematics, the binary logarithm (log2n) is the power to which
the number 2 must be raised to obtain the value n. That is, for any
real number x,
x=log2n <=equivalent to=>
2x=n
&
Every binary tree with n leaves has height at least log2n, with equality when n is a > power of two and the tree is a complete binary tree.
二分搜索有点像沿着二叉搜索树走下去,然后将节点减半,该系列是对数(以 2 为底)系列
(个人理解,未引用,可能有误)
然后从https://www.cct.lsu.edu/~sidhanti/tutorials/data_structures/page305.html
a perfect binary tree of height h has exactly 2h+1-1
internal nodes. Conversely, the height of a perfect binary tree with n
internal nodes is log2(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=log2(n+1).
(再次以自己的理解跟进...)
因此,要到达节点 N(根据二叉搜索树的值为 N?),您需要进行 log2(N+1) 次迭代(遍历树下的多个级别)最坏情况(发现仍然是概率,因此“最坏情况”的措辞)。
在这里干运行(通过构造一个小的BST并手动计数):https://www.cs.usfca.edu/~galles/visualization/BST.html
(当然 phrasing/calculations 中的 review/confirmations/corrections 的答案是开放的,因为我正在尝试整合不同的资源以得出在这种情况下有意义的理论)
如何获取二分查找的迭代次数?
这是我的代码:
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;
}
我想知道 N
的迭代次数。
我知道这个算法是如何工作的,但我想要迭代次数
用数学表示。
我会通过使用递归二进制搜索函数来递归递增。 在二进制检查的每个分支中,只需递增 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;
}
输出:
counter: 4
数学上可能的最大迭代(假设只有整数类型的情况)是 = ceil( log2 ( initial_r - initial_l ) ) log 的底数是 2,因为每次我们都在潜水我们的范围一半的一半,然后切换到一半。
我也一直在努力思考对数概念化,这就是我试图理解答案的方式
来自 https://en.wikipedia.org/wiki/Binary_search_algorithm
In mathematics, the binary logarithm (log2n) is the power to which the number 2 must be raised to obtain the value n. That is, for any real number x,
x=log2n <=equivalent to=> 2x=n
&
Every binary tree with n leaves has height at least log2n, with equality when n is a > power of two and the tree is a complete binary tree.
二分搜索有点像沿着二叉搜索树走下去,然后将节点减半,该系列是对数(以 2 为底)系列 (个人理解,未引用,可能有误)
然后从https://www.cct.lsu.edu/~sidhanti/tutorials/data_structures/page305.html
a perfect binary tree of height h has exactly 2h+1-1 internal nodes. Conversely, the height of a perfect binary tree with n internal nodes is log2(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=log2(n+1).
(再次以自己的理解跟进...)
因此,要到达节点 N(根据二叉搜索树的值为 N?),您需要进行 log2(N+1) 次迭代(遍历树下的多个级别)最坏情况(发现仍然是概率,因此“最坏情况”的措辞)。
在这里干运行(通过构造一个小的BST并手动计数):https://www.cs.usfca.edu/~galles/visualization/BST.html
(当然 phrasing/calculations 中的 review/confirmations/corrections 的答案是开放的,因为我正在尝试整合不同的资源以得出在这种情况下有意义的理论)