简体   繁体   English

C ++选择排序(向量)

[英]C++ Selection Sort (vectors)

int temp;
for (int j = 0; j < vecsize - 1; ++j) {

    int min = sort.at(j);
    for (int i = j+1; i < vecsize; ++i) {
        if (min > sort.at(i)) {
            min = sort.at(i);  
            temp = i;
        }

    }  
swap(sort.at(j), sort.at(temp));
}

I am trying to sort (in ascending order) the vector of: 23 42 4 16 8 15 我正在尝试按以下顺序对向量进行排序:23 42 4 16 8 15

However, my attempt at using selection sort outputs: 4 8 15 23 16 42 但是,我尝试使用选择排序输出:4 8 15 23 16 42

What am I doing wrong? 我究竟做错了什么?

When you define min, you seem to be assigning it the value of the array sort at jth index. 当您定义min时,您似乎正在为其分配第j个索引处的数组排序的值。 Yet, you are using an extra variable tmp to swap the elements, and you seem to fail to initialize it before the inner for loop, similar to how you initialize min. 但是,您正在使用额外的变量tmp交换元素,并且似乎无法在内部for循环之前对其进行初始化,类似于初始化min的方式。 And if all the other elements in the array are smaller than the element at sort[j], tmp will be uninitialized for that iteration of the outer loop, possibly causing it to have an incorrect value in it. 而且,如果数组中的所有其他元素都小于sort [j]处的元素,则tmp将针对外循环的该迭代未初始化,这可能会导致其中的值不正确。

int temp;
for (int j = 0; j < vecsize - 1; ++j) {
    int min = sort.at(j);
    temp = j;                                     # HERE'S WHAT'S NEW
    for (int i = j+1; i < vecsize; ++i) {
        if (min > sort.at(i)) {
            min = sort.at(i);  
            temp = i;
        }
    }  
    swap(sort.at(j), sort.at(temp));
}

You may see this code at work here . 您可能会在这里看到此代码。 It seems to produce the desired output. 似乎产生了所需的输出。

Try this : corrected-code 试试这个: 更正代码

#include <iostream>
#include <vector>
using namespace std;

void print (vector<int> & vec) {
    for (int i =0 ; i < vec.size(); ++i) {
        cout << vec[i] << " ";
    }
    cout << endl;
}

int main() {
int temp;
vector<int> sort;

sort.push_back(23);
sort.push_back(42);
sort.push_back( 4);
sort.push_back( 16);
sort.push_back( 8);
sort.push_back(15);
print(sort);

int vecsize = sort.size();
for (int j = 0; j < vecsize - 1; ++j) {

    int min = j;
    for (int i = j+1; i < vecsize; ++i) {
        if (sort.at(min) > sort.at(i)) {
            min = i;
        }

    }  
    if (min != j)
        swap(sort.at(j), sort.at(min));
}

print(sort);
return 0;
}

If you can use C++11, you can also solve sorting (as in your example) with lambdas. 如果可以使用C ++ 11,则还可以使用lambda解决排序问题(如您的示例所示)。 It's a more powerful and optimized way. 这是一种更强大,更优化的方法。 You should try it maybe in the future. 您应该在将来尝试。

[EDITED]: [EDITED]:

A short example: 一个简短的例子:

// Example program
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

int main()
{
    std::vector<int> myVector;

    myVector.emplace_back(23);
    myVector.emplace_back(42);
    myVector.emplace_back(4);
    myVector.emplace_back(16);
    myVector.emplace_back(8);
    myVector.emplace_back(15);

    std::sort(myVector.begin(), myVector.end(),
       [](int a, int b) -> bool
    {
       return a < b;
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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