如何根据结构中的字段对结构数组进行排序? (在 C 中)

How to sort an array of structures based on a field in the structures? (in C)

我有一个程序可以将 10 所大学的各种信息存储为数组中的结构。我有一个名为 'inputData' 的函数,它通过将大学的信息转换为结构来将它们加载到数组中。我正在尝试编写一个函数,按学费(从最高到最低)对大学进行排序。我尝试使用选择排序来执行此操作,但我将 运行 保留在某处的错误中。

这是结构:


struct University
{
    char name[50];
    char city[20];
    char state[3];
    int rank;
    int tuition;
};

这是将信息转换为结构的函数:

struct University inputData(char Name[50], char City[20], char State[3], int Rank, int Tuition){
    struct University uni;
    strcpy(uni.name, Name);
    strcpy(uni.city, City);
    strcpy(uni.state, State);
    uni.rank=Rank;
    uni.tuition=Tuition;
    return uni;
};

然后我创建了一个结构数组并添加了所有信息:

struct University university[10];
university[0]= inputData("Princeton University", "Princeton", "NJ", 1, 45320);
university[1]= inputData("University of Virginia", "Charlottesville", "VA", 24, 52040);
university[2]= inputData("Boston College", "Chestnut Hill", "MA", 31, 51296);
university[3]= inputData("Georgia Institute of Technology", "Atlanta", "GA", 34, 32404);
university[4]= inputData("Lehigh University", "Bethlehem", "PA", 44, 48320);
university[5]= inputData("University of Chicago", "Chicago", "IL", 3, 52491);
university[6]= inputData("Duke University", "Durham", "NC", 8, 51265);
university[7]= inputData("University of Georgia", "Athens", "GA", 56, 29844);
university[8]= inputData("University of Denver", "Denver", "CO", 86, 46362);
university[9]= inputData("Loyola University Chicago", "Chicago", "IL", 99, 26270);

我正在尝试使用此函数(使用选择排序)进行排序:

void printSortedUniversity(struct University* list){
    int i, j, max_idx; 
  
      for (i = 0; i < 9; i++) 
    { 
        max_idx = i; 
        for (j = i+1; j < 10; j++) 
          if (list[j].tuition > list[max_idx].tuition) 
            max_idx = j; 
  
        swap(&list[max_idx], &list[i]); 
    } 
    
    for (i=0; i<10; i++){
        printInfo(list[i]);
        printf("\n");
    }
};

这是交换函数:

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

我不断收到:

名称:大学城市:普林斯顿州:新泽西州排名:1 学费:45320
名称:弗吉尼亚王子城 城市:夏洛茨维尔 州:VA 排名:24 学费:52040
名称:Univon College 城市:Chestnut Hill 州:MA 排名:31 学费:51296
名称:Bostgia 理工学院城市:亚特兰大州:GA 排名:34 学费:32404
名称:乔治大学 城市:伯利恒 州:宾夕法尼亚州 排名:44 学费:48320
名称:芝加哥城市:芝加哥 州:IL 排名:3 学费:52491
名称:杜克大学城市:达勒姆州:NC 排名:8 学费:51265
名称:佐治亚大学城市:雅典州:GA 排名:56 学费:29844
名称:丹佛大学城市:丹佛州:CO 排名:86 学费:46362
名称:芝加哥洛约拉大学城市:芝加哥州:伊利诺伊州排名:99 学费:26270

我不明白为什么名称会损坏以及为什么排序不起作用?我认为这是因为修改后的列表没有存储在任何地方,但我不知道如何解决这个问题。 任何帮助,将不胜感激。 谢谢

你的swap函数应该是

void swap(struct University *x, struct University *y) 
{ 
    struct University temp = *x; 
    *x = *y; 
    *y = temp; 
} 

这给出了预期的输出:

Name: University of Chicago City: Chicago Rank: 3 Tuition: 52491
Name: University of Virginia City: Charlottesville Rank: 24 Tuition: 52040
Name: Boston College City: Chestnut Hill Rank: 31 Tuition: 51296
Name: Duke University City: Durham Rank: 8 Tuition: 51265
Name: Lehigh University City: Bethlehem Rank: 44 Tuition: 48320
Name: University of Denver City: Denver Rank: 86 Tuition: 46362
Name: Princeton University City: Princeton Rank: 1 Tuition: 45320
Name: Georgia Institute of Technology City: Atlanta Rank: 34 Tuition: 32404
Name: University of Georgia City: Athens Rank: 56 Tuition: 29844
Name: Loyola University Chicago City: Chicago Rank: 99 Tuition: 26270

备注

这是一种效率极低的排序。没有人会在生产代码中这样做。

David Cullen wrote about the swap() function in their 一样,根据tuition交换结构的swap()函数为:

void swap(struct University *x, struct University *y) 
{ 
    struct University temp = *x; 
    *x = *y; 
    *y = temp; 
}

你的 printSortedUniversity() 函数应该是这样的:

void printSortedUniversity(struct University* list){
    int i, j, max_idx;

      for (i = 0; i < 9; i++)
    {
        max_idx = i;
        for (j = i+1; j < 10; j++)
            if (list[j].tuition > list[max_idx].tuition)
                max_idx = j;
        if(i != max_idx)
            swap(&list[max_idx], &list[i]);
    }

    for (i=0; i<10; i++){
        printInfo(list[i]);
        // printf("%d\n",list[i].tuition);
        printf("\n");
    }
}

这对我有用:

使用了这个交换函数:

void swap(struct University *p, struct University *q)  //function to swap list items
{ 
    struct University hold = *p; 
    *p = *q; 
    *q = hold; 
}

而且我没有更改 printSortedUniversity 函数:

void printSortedUniversity(struct University* list){ int i, j, max_idx;

  for (i = 0; i < 9; i++) 
{ 
    max_idx = i; 
    for (j = i+1; j < 10; j++) 
      if (list[j].tuition > list[max_idx].tuition) 
        max_idx = j; 

    swap(&list[max_idx], &list[i]); 
} 

for (i=0; i<10; i++){
    printInfo(list[i]);
    printf("\n");
}

};