简体   繁体   English

为什么我的选择排序返回的值不在原始向量中?

[英]Why is my selection sort returning a value that is not in the original vector?

I've been tinkering around with it for a while now and I'm so close! 我已经花了一段时间了,我已经很亲密了! Now the output seems to be continuously printing a zero as the first value of the "sorted" vector. 现在,输出似乎将连续打印零作为“排序”向量的第一个值。 This is homework on how to create a selection sort in C++. 这是有关如何在C ++中创建选择排序的家庭作业。

Example Output 示例输出

Vector: 6, 2, 11, 1, 12, 4 向量:6、2、11、1、12、4

Sorted Vector: 0, 2, 11, 6, 12, 4 排序向量:0、2、11、6、12、4

Code

void selectionSort (vector<int>& data)
{
    int min, temp, n=data.size(), i, j;

    for (i=0; i<n; i++)
    {
        min = i;
        for (j=i+1; j<n; j++)
        {   
            if (data[min]>data[j])
            {
                min=j;
            }   
            temp=data[min];
            data[min]=data[i];
            data[i]=temp;
        }
        return;
    }   
}

int main()
{
    int n;
    vector<int> data;

    cout<<"Vector length?: "<<endl;
    cin>>n;

    srand(time(0));
    for (int i=0; i<n; i++)
    {
        data.push_back(rand()%20+1);
    }

    cout<<"Vector: "<<endl;
    for (int i=0; i<n; i++)
    {
        cout<<data[i]<<" "<<endl;
    }

    selectionSort(data);

    cout<<"Selection Sorted Vector: "<<endl;
    for (int i=0; i<data.size(); i++)
    {
        cout<<data[i]<<" "<<endl;
    }

    system("Pause");

    return 0;




}

Given an array of n elements, a selection sort performs n swaps. 给定n元素的数组,选择排序将执行n交换。

  1. You're performing far more swaps than that. 您执行的交换远不止于此。
  2. You also have an unexpectedly early call to return . 你也有一个出乎意料地提早调用return

Let's look at a proper implementation of the sort: 让我们看一下排序的正确实现:

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

void selectionSort (vector<int>& data)
{
    const int n = data.size();

    for (int i=0; i<n; i++)
    {
        int min = i;
        for (int j=i+1; j<n; j++)
            if (data[min]>data[j])
                min = j;

        swap(data[min], data[i]);
    }
}

int main() {
    vector<int> data = {6, 2, 11, 1, 12, 4};
    selectionSort(data);

    for (auto element : data)
        cout << element << " ";
    cout << "\n";
}

Which outputs: 哪个输出:

1 2 4 6 11 12 

Consider the following correct implementation of a selection sort and compare it to yours: 考虑以下选择排序的正确实现,并将其与您的选择进行比较:

#include <iostream>
#include <vector>

void selection_sort(std::vector<int> &numbers)
{
    // Iterate through all possible start indices.
    for (int i = 0; i < numbers.size(); ++i)
    {
        // Determine the index of the minimum for this iteration.
        int index_of_min = i;
        for (int j = i; j < numbers.size(); ++j)
        {
            if (numbers[j] < numbers[index_of_min])
            {
                index_of_min = j;
            }
        }
        // Swap the minimum element with the element at the start index.
        int temp = numbers[i];
        numbers[i] = numbers[index_of_min];
        numbers[index_of_min] = temp;
    }
}

int main()
{
    std::vector<int> numbers = { 20, 17, 13, 12, 25 };

    selection_sort(numbers);

    for (size_t i = 0; i < numbers.size(); ++i)
    {
        std::cout << numbers[i] << " ";
    }
}

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

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