简体   繁体   中英

Sorting then Searching(vectors c++)

I've already tried Searching alone using vectors instead of fixed-memory arrays and it worked perfectly well. But now when im trying to sort the vector first to enable it to work in a Binary Search , The program stops after the user inputs the vector list Here's the code

#include <iostream>
#include <vector>
using namespace std;
void BubbleSort(vector<int>list){
    int temp;
    for (int i=0;i<list.size();i++){
        for (int j=1;j<list.size();j++){
            if(list[i]>list[j]){
                list[i]=temp;
                list[j]=list[i];
                temp=list[j];
            }
        }
    }
}
int Binary_search(vector<int>list,int target){
    int maximum=(list.size())-1;
    int minimum = 0;
    int mean;
    while (maximum>minimum){
        mean = (maximum+minimum)/2;
        if (list[mean] == target){
            cout << "The number you're looking for is found! \n";
            return mean;
        }
        else if(list[mean] > target){
            maximum = mean;
        }
        else{
            minimum = mean;
        }
    }
    return -1;
}
int main()
{
    unsigned int k;
    int x,a,target;
    vector<int>list;
    cout << "Enter the amount of numbers you want to enlist \n";
    cin >> k;
    while((list.size()< k) && (cin >> x)){
        list.push_back(a);
    }
    BubbleSort(list);
    cout << "Enter the target that you want to search for \n";
    cin >> target;
    int result = Binary_search(list,target);
    if(result == -1){
        cout << "Your result is not found ";
    }
    else{
        cout << "Your result is found at the index: " << result;
    }
    return 0;
}

Im expecting the program to sort (but not to print out the vector sorted , Just to sort from behind and then show the result at the end after searching) The problem surely is in the sorting part but I dont know if it's alright to use Bubble Sort before it , Can anyone point me to the right way to sort then search ?

@Oliver_Queen there are some trouble in your code but if you follow the comments of @1201ProgramAlarm and @Blastfurnace you could be solve a part your problem but in the Binary_search() function you should be used mean-1 or mean+1 for maximum and minimum respectively and check if at end is true the condition, well here is your code fixed:

void BubbleSort(vector<int> &list){
    int temp;
    for (int i=0;i<list.size();i++){
        for (int j=i+1;j<list.size();j++){
            if(list[i]>list[j]){
                temp=list[i];
                list[i]=list[j];
                list[j]=temp;
            }
        }
    }
}

and in the function Binary_search() like:

int Binary_search(vector<int>list,int target){
    int maximum=(list.size())-1;
    int minimum = 0;
    int mean;
    while (maximum>minimum){
        mean = (maximum+minimum)/2;
        if (list[mean] == target){
            cout << "The number you're looking for is found! \n";
            return mean;
        }
        else if(list[mean] > target){
            maximum = mean-1;
        }
        else{
            minimum = mean+1;
        }
    }
    if (list[minimum] == target) return minimum;
    return -1;
}

or for avoid the conditional after the ciclo while :

int Binary_search(vector<int>list,int target){
    int maximum=(list.size())-1, minimum = 0, mean;
    while (minimum <= maximum){
        mean = (maximum+minimum)/2;
        if (list[mean] == target)    return mean;
        else if(list[mean] > target) maximum = mean-1;
        else                         minimum = mean+1;
    }
    return -1;
}

But my recommendation is that you use std::sort() from algorithm library for performance the sort in O(N*log(N)) where N is the number of items to sort, and usestd::binary_search to check if it belongs or std::lower_bound to find the first item not less than the given value.

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

using namespace std;

int main()
{
    unsigned int k;
    int x,a,target;
    vector<int>list;
    cout << "Enter the amount of numbers you want to enlist \n";
    cin >> k;
    while((list.size()< k) && (cin >> x)){
        list.push_back(x);
    }
    sort(list.begin(), list.end());
    cout << "Enter the target that you want to search for \n";
    cin >> target;
    auto result = lower_bound(list.begin(), list.end(), target);
    if(result == list.end()){
        cout << "Your result is not found ";
    }
    else{
        cout << "Your result is found at the index: " << result-list.begin();
    }
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM