【问题标题】:Sorting then Searching(vectors c++)排序然后搜索(向量c ++)
【发布时间】:2020-03-15 16:12:07
【问题描述】:

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

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

我希望程序进行排序(但不是打印出向量 sorted ,只是从后面排序,然后在搜索后显示结果) 问题肯定出在排序部分,但我不知道在它之前使用冒泡排序是否可以,谁能指出我正确的排序方式然后搜索?

【问题讨论】:

  • for (int j=1;i&lt;list.size();j++) ... 仔细查看条件和您在那里使用的变量...
  • 这很尴尬,我的错,但即使它仍然无法正常工作,它也会继续运行,但程序本身没有完成
  • BubbleSort 按值获取其参数,因此排序列表永远不会返回给调用者。它应该通过引用传递:void BubbleSort(vector&lt;int&gt; &amp;list).
  • 好吧,我没有注意按值/引用传递,虽然每次结果等于-1时它仍然一直显示未找到,是不是以这种方式搜索的问题?
  • 仔细查看用于在排序中交换值的三行。它们不正确,但可以正常工作,但重新排列正确。

标签: c++ sorting search vector


【解决方案1】:

@Oliver_Queen 你的代码有一些问题,但如果你遵循@1201ProgramAlarm 和@Blastfurnace 的cmets,你可以解决部分问题,但在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;
}

【讨论】:

    猜你喜欢
    • 2020-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-28
    • 1970-01-01
    • 1970-01-01
    • 2017-02-19
    相关资源
    最近更新 更多