0

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!

jreed
  • 314
  • 3
  • 11
  • 1
    To modify the vector you have to pass it by reference, otherwise you are modifying a local copy – Slava Nov 21 '16 at 22:29
  • You wrote `swapInt` and `swapString` etc, why not to write `swapRecored` the same way, instead of duplicating that code? – Slava Nov 21 '16 at 22:33
  • Thanks Slava, I just needed the ampersand. and I'll be writing that function per your recommendation. That makes sense! – jreed Nov 21 '16 at 22:39
  • @Joseph -- You should also [read this](http://stackoverflow.com/questions/24650626/how-to-implement-classic-sorting-algorithms-in-modern-c) – PaulMcKenzie Nov 21 '16 at 22:47
  • @PaulMcKenzie that's good stuff, i just saved it. Thanks! – jreed Nov 21 '16 at 22:56

2 Answers2

0

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);
Vivek
  • 320
  • 1
  • 5
  • Thanks for the tip, but the goal of the program was to write my own sorting function and implement it for a vector of structs – jreed Nov 21 '16 at 22:53
0

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!

jreed
  • 314
  • 3
  • 11