BinarySearch returns the index where it belongs

So, I'm looking to write code to return an index that is a key, or if it is not, where should it be. What am I missing? min is 0, max is size is 1, sorting 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;
}
+4
source share
5 answers

I had almost the same problem, so I wrote this generic code (you might want to use a different namespace than std;)). The code below returns an iterator to the largest element in a sequence that is less than or equal to val. It uses O (N log N) time for N = std :: difference (first, last), assuming that O (1) is random access on [first ... last).

#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:: , . ( ) , val >= * [first, last) , std:: binary_search.

, .

+3
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);
    }
}
+1

In binary search, you can search for a value type without a reference type. If you want to find a string in a string array, you need to write a complex program or use a table

0
source

It works:

#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);
}
0
source

All Articles