排序/交换结构向量的元素,用于快速排序算法(C++) [英] Sorting/Swapping elements of vector of structs for quickSort algorithm (C++)

查看:70
本文介绍了排序/交换结构向量的元素,用于快速排序算法(C++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结构向量来保存学生记录:

I have a vector of structures to hold student records:

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

我正在尝试按 id 对向量进行排序,但我似乎无法弄清楚.我从 .txt 文件将值读入向量,但是当我尝试对其进行排序时,没有任何元素被交换.

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;
}

以及 main() 中用于显示向量的循环:

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!

推荐答案

正如 Slava 在评论中指出的那样,我需要通过引用我的函数来传递向量.之前我只是编辑矢量的本地副本.

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) {

谢谢斯拉瓦!

这篇关于排序/交换结构向量的元素,用于快速排序算法(C++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆