二进制搜索返回其所属位置的索引
BinarySearch returning index of where it belongs
所以我正在寻找一个代码来返回键所在的索引,或者如果它不存在,它应该在哪里。 我错过了什么?最小值为 0,最大值为大小 - 1,BUF 排序
int binarySearch(string buf[], string key, int min, int max){
int mid;
while (max >= min){
mid = (min + max) / 2;
if (buf[mid] < key)
min = mid + 1;
else if (buf[mid] > key)
max = mid - 1;
else
return mid;
}
return min;
}
我几乎遇到了同样的问题,所以我写了这个通用代码(也许你可能想使用与 std ;) 不同的命名空间)下面的代码将迭代器返回到序列中小于或等于 val 的最大元素。它使用 O(N log N) 时间表示 N = std::d ifference(first, last),假设 O(1) 随机访问 [first ...最后)。
#include <iostream>
#include <vector>
#include <algorithm>
namespace std {
template<class RandomIt, class T>
RandomIt binary_locate(RandomIt first, RandomIt last, const T& val) {
if(val == *first) return first;
auto d = std::distance(first, last);
if(d==1) return first;
auto center = (first + (d/2));
if(val < *center) return binary_locate(first, center, val);
return binary_locate(center, last, val);
}
}
int main() {
std::vector<double> values = {0, 0.5, 1, 5, 7.5, 10, 12.5};
std::vector<double> tests = {0, 0.4, 0.5, 3, 7.5, 11.5, 12.5, 13};
for(double d : tests) {
auto it = std::binary_locate(values.begin(), values.end(), d);
std::cout << "found " << d << " right after index " << std::distance(values.begin(), it) << " which has value " << *it << std::endl;
}
return 0;
}
来源: http://ideone.com/X9RsFx
代码非常通用,它接受std::vectors,std::arrays和数组,或任何允许随机访问的序列。假设(读取前提条件)是 val>= *first 并且值 [first, last) 被排序,就像 std::binary_search 需要的那样。
请随时提及我使用过的错误或不当行为。
int binary_srch_ret_index(ll inp[MAXSIZE], ll e, int low, int high) {
if (low > high) {
return -1;
}
int mid = (low + high) / 2;
if (e == inp[mid]) {
return mid;
}
if (e < inp[mid]) {
return binary_srch(inp, e, low, mid - 1);
} else {
return binary_srch(inp, e, mid + 1, high);
}
}
您搜索了一个字符,并假设 buf 中的字符已排序。
如果要搜索字符串,请使用字符串匹配模式算法。(http://en.wikipedia.org/wiki/String_searching_algorithm)
如果要搜索有序数组中的字符或数字,请参阅以下内容:http://www.programmingsimplified.com/c/source-code/c-program-binary-search
在二叉搜索中,您可以执行值类型搜索而不是引用类型。如果你想在字符串数组中搜索字符串,你必须编写一个复杂的程序或使用有表
这
似乎有效:
#include <iostream>
#include <cassert>
int binarySearch(int buf[], int key, int min, int max);
int main()
{
int data[] = {1,2,4,6,7,9};
for(int i=0; i<6; i++)
{
int result = binarySearch(data, data[i], 0, 5);
assert(result == i);
}
assert(binarySearch(data, 3, 0, 5) == 1);
assert(binarySearch(data, 5, 0, 5) == 2);
assert(binarySearch(data, 8, 0, 5) == 4);
assert(binarySearch(data, 10, 0, 5) == 5);
return 0;
}
int binarySearch(int buf[], int key, int min, int max)
{
int mid;
while (max >= min){
mid = (min + max) / 2;
if (buf[mid] < key)
min = mid + 1;
else if (buf[mid] > key)
max = mid - 1;
else
return mid;
}
return std::min(min, max);
}
相关文章:
- 访问特定阵列位置/索引时出现分段错误
- 素数索引位置C程序
- 如何计算 3d 数组中的索引值在内存中的位置?如何计算字符**中的索引值在内存中的位置?
- GLES计划资源索引/位置与统一索引/位置
- 为字符串中的每个字符(不是字符位置!)分配唯一的索引
- 我可以将单个顶点索引与具有所有属性(位置、法线、texCoord)的顶点缓冲区一起使用吗?
- C++ std::map 如何按索引位置访问密钥
- C++ 提升日志位置(索引)格式对齐
- 如何返回索引位置而不是值
- C++如何在获取子字符串的索引位置后获取子字符串
- C++:写入 std::string 中的特定索引位置
- 二进制搜索返回其所属位置的索引
- 识别数组在C++中的最小索引位置
- 需要 std::set 元素的位置/索引
- 查找向量中的值索引位置
- 获取元素在boost::multi_index_container的顺序索引中的位置
- 正在将指针从数组值存储到原始数组索引位置
- 不同的结果取决于 for 循环的循环索引在C++中的位置
- 带有Overload[]运算符的C++MyInteger类,因此索引在位置i返回数字
- 2d查找重复元素/获取索引位置并计数重复项