简体   繁体   中英

Sorting/Swapping elements of vector of structs for quickSort algorithm (C++)

I have a vector of structures to hold student records:

struct record {
   int id;
   string name;
   string address;
   double gpa;
   int *scorePtr;
};

I'm trying sort the vector by id but I can't seem to figure it out. I read the values into the vector from a .txt file, but when I try to sort it, none of the elements are swapped.

below are my functions for the quick sort:

void quickSort(vector<record> set, int start, int end) {
   int pivotPoint;
   if (start < end) {
       //get the pivot point
       pivotPoint = partition(set, start, end);
       //sort first sublist
       quickSort(set, start, pivotPoint - 1);
       //sort second sublist
       quickSort(set, pivotPoint + 1, end);
   }
}

int partition(vector<record> set, int start, int end) {
    int pivotValue, pivotIndex, mid;

    mid = (start + end) / 2;
    swapInt(set[start].id, set[mid].id);
    swapString(set[start].name, set[mid].name);
    swapString(set[start].address, set[mid].address);
    swapDouble(set[start].gpa, set[mid].gpa);

    pivotIndex = start;
    pivotValue = set[start].id;

    for(int scan = start + 1; scan <= end; scan++) {
        if (set[scan].id < pivotValue) {
            pivotIndex++;
            swapInt(set[pivotIndex].id, set[scan].id);
            swapString(set[pivotIndex].name, set[scan].name);
            swapString(set[pivotIndex].address, set[scan].address);
            swapDouble(set[pivotIndex].gpa, set[scan].gpa);
        }
    }

    swapInt(set[start].id, set[pivotIndex].id);
    swapString(set[start].name, set[pivotIndex].name);
    swapString(set[start].address, set[pivotIndex].address);
    swapDouble(set[start].gpa, set[pivotIndex].gpa);
    return pivotIndex;
}

void swapInt(int &value1, int &value2) {
    int temp = value1;
    value1 = value2; 
    value2 = temp;
}
void swapString(string &value1, string &value2) {
    string temp = value1;
    value1 = value2; 
    value2 = temp;
}
void swapDouble(double &value1, double &value2) {
    double temp = value1;
    value1 = value2; 
    value2 = temp;
}

And the loops in main() for displaying the vector:

//output vector
for(int k = 0; k < 20; k++) {
    cout << sub[k].id << endl << sub[k].name << endl;
    cout << sub[k].address << endl << fixed << setprecision(1) << sub[k].gpa << endl;
}

quickSort(sub, 0, 19);

cout << endl << endl;
//output vector
for(int k = 0; k < 20; k++) {
    cout << sub[k].id << endl << sub[k].name << endl;
    cout << sub[k].address << endl << fixed << setprecision(1) << sub[k].gpa << endl;
}

When I run it I just get the same list twice, unsorted. Any help is appreciated!

you can simply use the std::sort function to sort the vector:

bool comp(record rl, record rr) {
  return (rl.id < rr.id);
}

vector<record> v;
std::sort(v.begin(), v.end(), comp);

As Slava pointed out in the comments, I needed to pass the vector by reference to my functions. Before I was just editing a local copy of the vector.

void quickSort(vector<record> &set, int start, int end) {

int partition(vector<record> &set, int start, int end) {

Thanks Slava!

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