简体   繁体   English

在结构数组中合并排序

[英]Merge sort in array of structs

I have an array of structs, where each object is a student with a name and a grade, and I'm trying to merge sort this array (I want to sort them by grade in ascending order).我有一个结构数组,其中每个 object 是一个有姓名和成绩的学生,我正在尝试对这个数组进行合并排序(我想按成绩按升序对它们进行排序)。 Here is the Student Struct:这是学生结构:

struct Student
{
    char grade[42];
    char city[42];
};

grade is a char because I assign the grade value by getting an input from the user with fgets and sscanf. Grade 是一个字符,因为我通过使用 fgets 和 sscanf 从用户获取输入来分配等级值。 I don't think it's necessary to put my whole code of the merge-sort algorithm but I want to share this part which I think might be problematic?我认为没有必要将我的合并排序算法的整个代码放入其中,但我想分享这部分我认为可能有问题的部分?

    int firstHalfSize = midElement - firstElement + 1;
    int secondHalfSize = lastElement - midElement;
    struct Student firstHalfArray[firstHalfSize];
    struct Student secondHalfArray[secondHalfSize];
    char *p;
    char *s;
    int index1 = 0;
    int index2 = 0;
    int mergedArrIndex = firstElement;

    while (index1 < firstHalfSize && index2 < secondHalfSize)
    {
        if (strtol(firstHalfArray[index1].grade, &p, 10) <= strtol(secondHalfArray[index2].grade, &s, 10))
        {
            arr[mergedArrIndex] = firstHalfArray[index1];
            index1++;
        }
        else
        {
            arr[mergedArrIndex] = secondHalfArray[index2];
            index2++;
        }
        mergedArrIndex++;
    }

the part where I sort the student object by comparing the grade is by converting the char grade into a long with strtol which I think I did good so it might not be a problem.我通过比较成绩对学生 object 进行排序的部分是将char grade转换为带有strtol的 long 我认为我做得很好,所以这可能不是问题。 My problems is that I initialize my array of structs like the following:我的问题是我初始化了我的结构数组,如下所示:

struct Student students[5501];

and when I get a new input of the user I just add it into the array like the following:当我得到用户的新输入时,我只需将其添加到数组中,如下所示:

struct Student aStudent;
int lineCounter = 0;
students[lineCounter] = aStudent;

and increase lineCounter by 1 every time I get a new input.每次我得到一个新的输入时,将lineCounter增加 1。 ( aStudent is changed when I get a new input) Here I call the merge-sort function and get weird results: (当我得到一个新的输入时aStudent发生了变化)在这里我调用了合并排序 function 并得到了奇怪的结果:

mergeSort(students, 0, 5501); // 5501 is the size of the array
printArray(students, 5501);

Here is the printArray function created just to see if I get the grade in ascending order这是创建的 printArray function 只是为了查看我是否按升序获得成绩

void printArray(struct Student A[], int size)
{
    char *p;
    int i;
    for (i=0; i < size; i++)
        printf("%ld", strtol(A[i].grade, &p, 10));

}

but I keep getting that printed:但我一直在打印:

00000000000000000000000000000000000000000000000000000000000000000000000000000000000000...

and I don't know why.我不知道为什么。 Also how can I do that If the user adds 3 students object in the array students I only treat it as like an array of 3 elements and not an array of 5501 elements?另外我该怎么做如果用户在数组students中添加 3 个学生 object 我只将其视为 3 个元素的数组而不是 5501 个元素的数组? Thanks.谢谢。 And sorry if it's a little long I really tried to be concise without having a loss of essential informations.对不起,如果它有点长,我真的试图简洁而不会丢失基本信息。

Since the user can input N numbers of students I recommend you to use a Linked List instead and put the node (struct Student) in his right position depending on the grade.由于用户可以输入 N 个学生,我建议您改用链接列表,并根据年级将节点(struct Student)放在他的右侧 position 中。

You can learn more about Linked Lists here .您可以在此处了解有关链接列表的更多信息。

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

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