二进制搜索返回其所属位置的索引

BinarySearch returning index of where it belongs

本文关键字:位置 索引 搜索 返回 二进制      更新时间:2023-10-16

所以我正在寻找一个代码来返回键所在的索引,或者如果它不存在,它应该在哪里。 我错过了什么?最小值为 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);
}