c++ - 排序然后搜索( vector C++)

标签 c++ sorting search vector

我已经尝试过使用 vector 而不是固定内存数组单独进行搜索,并且效果很好。但是现在,当我尝试首先对 vector 排序以使其能够在Binary Search 中工作时,在用户输入 vector 列表后程序停止
这是代码

#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;
}

我希望程序进行排序(但不要打印出排序的 vector ,只是从后面进行排序,然后在搜索后在最后显示结果)
问题肯定在排序部分,但是我不知道在它之前使用Bubble Sort是否可以,有人可以指出我正确的排序方式然后搜索吗?

最佳答案

@Oliver_Queen代码中有一些麻烦,但是如果您遵循@ 1201ProgramAlarm和@Blastfurnace的注释,则可以解决部分问题,但在Binary_search()函数中,应分别将mean-1mean+1用于maximumminimum,并进行检查如果最后的条件为真,那么这里的代码是固定的:

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;
            }
        }
    }
}

并在函数Binary_search()中如下所示:
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;
}

或者为避免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;
}

但是我的建议是您使用算法库中的 std::sort() 来执行O(N*log(N))中的排序,其中N是要排序的项目数,并使用 std::binary_search 检查其是否属于该类别,或者使用 std::lower_bound 查找不小于给定值的第一项。
#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;
}

关于c++ - 排序然后搜索( vector C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60694928/

相关文章:

javascript - 按特定顺序对对象数组进行排序

用 C 计算数组中包含的字母字符

c++ - 在 C++ 中调用继承的类特定函数

c++ - std::sort 中的 SIGSEGV,如何缩小范围

c++ - 何时使用指向类的指针以及何时将其实例化为变量

java - 就地快速排序

c - 将数组分成两部分,对每个部分进行排序,然后重新组合成单​​个数组

javascript - 遍历大对象也返回空值

python - 当两个数组都已排序时,更快的搜索排序方法

c++ - ARMv5tejl 中用于 C++ 的良好内存泄漏工具