Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binary Search using a vector

Tags:

c++

search

vector

So.. Ive already learnt of Binary Search and how it works and even tried it using a constant array without any input from the user , But now im trying to apply vector instead of array to have the user enter the values of both the list in which to search the numbers from vector and the target to search for. Here i used a normal divide&conquer while using an array

using namespace std;
int Binary_search(int x[],int size,int target){
    int maximum= size-1;
    int minimum = 0;
    int mean;
    while (maximum>minimum){
        mean = (maximum+minimum)/2;
        if (x[mean] == target){
            cout << "The number you're looking for is found! \n";
            return mean;
        }
        else if(x[mean] > target){
            maximum = (mean-1);
        }
        else{
            minimum = (mean+1);
        }
    }
    return -1;
}
int main(){
    int x[]={1,2,3,4,5};
    int a=sizeof(x)/sizeof(x[0]);
    int target=4;
    int show=Binary_search(x,a,target);
    if (show != -1){
        cout << "Your result is in the index: " << show;
    }
    return 0;
}

My problem is that I made pretty much the same method using vector instead but it's showing either an infinite amount of **Your result is found at the index: ** (number of wrong index) . Or it doesnt show any result at all or even show that the result is not found , Differs each time somehow. Here's while using the vector

#include <iostream>
#include <vector>
using namespace std;
int Binary_search(vector<int>x,int target){
    int maximum=(x.size())-1;
    int minimum = 0;
    int mean;
    while (maximum>minimum){
        mean = (maximum+minimum)/2;
        if (x[mean] == target){
            cout << "The number you're looking for is found! \n";
        }
        else if(x[mean] > target){
            maximum = (mean-1);
        }
        else{
            minimum = (mean+1);
        }
    }
    return -1;
}
int main(){
    unsigned int i;
    int n;
    vector<int>x;
    cout << "Enter the amount of numbers you want to evaluate: ";
    cin >> i;
    cout << "Enter your numbers to be evaluated: " << endl;
    while (x.size() < i && cin >> n){
        x.push_back(n);
    }
    int target;
    cout << "Enter the target you want to search for in the selected array \n";
    cin >> target;
    int show = Binary_search(x,target);
    if (show == -1){
        cout << "Your result is not found ! ";
    }
    else{
        cout << "Your result is in the index: " << show;
    }
    return 0;
}

So I think the problem is in this part int maximum=(x.size())-1; , Maybe it's about how to use the size of vectors ? Could someone enlighten me up to this


1 Answers

Well, the other answers have helped to solve the issue, but in case you didn't know, there are built-in functions for performing binary search in C++ that you can use them.

I will list the functions related binary search:

  • sort: you can use binary search only on a sorted data, so you must guarantee that the data is sorted, before searching.
  • lower_bound: this function returns an iterator to the first element that is greater than or equal to value.
  • upper_bound: this function returns an iterator to the first element that is greater than value.
  • binary_search:: this functions returns a boolean, whether the value is found or not (exactly as your program).

Here's a code sample that demonstrates the previous functions:

#include <iostream>
#include <vector>
#include <algorithm>
#include <iostream>

typedef std::vector<int>::iterator iter;

int main() {

    std::vector<int> vec = {10, 20, 30, 30, 20, 10, 10, 20};

    // sort the data
    // the data will be:
    // 10, 10, 10, 20, 20, 20, 30, 30
    std::sort(vec.begin(), vec.end());

    // index of the first element, greater than or equal to 20
    iter low = std::lower_bound(vec.begin(), vec.end(), 20);

    // index of the first element, greater than 20
    iter high = std::upper_bound(vec.begin(), vec.end(), 20);

    std::cout << "index of first element, greater than or equal to 20 is: " << (low - vec.begin()) << '\n';

    std::cout << "index of first element, greater than to 20 is: " << (high - vec.begin()) << '\n';

    // classic binary search
    // check whether a givin value exists in the array or not
    if (std::binary_search(vec.begin(), vec.end(), 99)) {
        std::cout << "Found\n";
    } else {
        std::cout << "Not found\n";
    }

}
like image 70
Reslan Tinawi Avatar answered Oct 26 '25 00:10

Reslan Tinawi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!