简体   繁体   中英

Selection sort of struct in C++

My task is to create a selection sort algorithm to store data. So i use struct to store the data and i have implemented the selection sort, but i am really confused by result. I appreciate your help.

#include <iostream>
#include <vector>
#include <cstdlib>
#include <vector>
#include <string>

using namespace std;

struct employee
{
    string name;
    int salary;
};

void swap(int& x, int& y)
{
    int temp = x;
    x = y;
    y = temp;
}


int min_position(vector<employee>& a, int from, int to)
{
    int min_pos = from;
    int i;
    for (i = from + 1; i <= to; i++)
        if (a[i].salary < a[min_pos].salary)
            min_pos = i;
    return min_pos;
}

void selection_sort(vector<employee>& a)
{
    int next; // the next position to be set to the minimum

    for (next = 0; next < a.size() - 1; next++)
    { // find the position of the minimum
        int min_pos = min_position(a, next, a.size() - 1);
        if (min_pos != next)
            swap(a[min_pos].salary, a[next].salary);
    }
}


void print(vector<employee>& a)
{
    for (int i = 0; i < a.size(); i++)
        cout << a[i].name << ", " << a[i].salary;
    cout << "\n";
}

int main()
{
    int empl;
    cout << "enter the number of employees:\n";
    cin >> empl;
    vector<employee> v(empl);
    for (int i = 0; i < empl; i++)
    {
        cout << "Enter the employee and the salary: " << endl;

        employee e; // create an employee
        cin >> e.name; // get name from user
        cin >> e.salary; // get salary from user

        v.push_back(e); // put employee into vector
    }
    print(v);
    selection_sort(v);
    cout << "_________________________" << endl;
    print(v);
    return 0;
}

Here is the output:

Enter the number of employees:
2
Enter the employee and the salary: 
John
60
Enter the employee and the salary: 
Ron
20
, 0, 0John, 60Ron, 20
_________________________
, 0, 0John, 20Ron, 60

I'm not exactly sure what your question is or what you were expecting, but you are only swapping the salary here:

swap(a[min_pos].salary, a[next].salary);

You want to swap the whole employee :

swap(a[min_pos], a[next]);

EDIT: Now that you edited with what your problem is, here is the problem you are seeing:

vector<employee> v(empl);
for (int i = 0; i < empl; i++)
{
    cout << "Enter the employee and the salary: " << endl;

    employee e; // create an employee
    cin >> e.name; // get name from user
    cin >> e.salary; // get salary from user

    v.push_back(e); // put employee into vector
}

Before this loop, you initialize your vector to the number employees, but then add in the ones you read. So you are ending up with 2 times the number of employees you expect, half of which are garbage. To fix this, either declare the vector different:

vector<employee> v;

or don't push_back :

v[i] = e;

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