如何按名称、年龄和id对C中的结构数组进行排序?
我不知道如何创建一个函数来按值对结构数组进行排序。任何提示都是有用的 我不知道如何将值传递到函数中,然后按年龄、名称对结构列表进行排序 我想看看如何组织结构数组,因为我以后将不得不使用多个文件,包括名称、年龄和ID如何按名称、年龄和id对C中的结构数组进行排序?,c,sorting,struct,selection,bubble-sort,C,Sorting,Struct,Selection,Bubble Sort,我不知道如何创建一个函数来按值对结构数组进行排序。任何提示都是有用的 我不知道如何将值传递到函数中,然后按年龄、名称对结构列表进行排序 我想看看如何组织结构数组,因为我以后将不得不使用多个文件,包括名称、年龄和ID //- Functions needed //Swap //Selection sort //Bubble sort #include <stdio.h> #include <stdlib.h> #include <string.h> type
//- Functions needed
//Swap
//Selection sort
//Bubble sort
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct person
{
int age;
double id;
char name[64];
} person;
int main()
{
person **array = calloc(2, sizeof(person*));
int sizes[2];
for (int i = 0; i < 5; i++) {
array[i] = calloc(sizes[i], sizeof(person));
}
//Num1
strcpy(array[1][0].name, "0yblTP");
array[1][0].id = 7.567;
array[1][0].age = 34;
//Num 2
strcpy(array[1][1].name, "t700nwzB");
array[1][1].id = 8.6576;
array[1][1].age = 85;
//Num3
strcpy(array[1][2].name, "Vx8eR");
array[1][2].id = 179;
array[1][2].age = 59;
//Num4
strcpy(array[1][3].name, "n5FUgA");
array[1][3].id = 1.082797;
array[1][3].age = 45;
//Num5
strcpy(array[1][4].name, "Bzm9dq");
array[1][4].id = 179;
array[1][4].age = 23;
}
/-需要的函数
//交换
//选择排序
//气泡排序
#包括
#包括
#包括
typedef结构人
{
智力年龄;
双id;
字符名[64];
}人;
int main()
{
person**array=calloc(2,sizeof(person*));
整数大小[2];
对于(int i=0;i<5;i++){
数组[i]=calloc(size[i],sizeof(person));
}
//Num1
strcpy(数组[1][0]。名称,“0yblTP”);
数组[1][0].id=7.567;
数组[1][0]。年龄=34;
//数字2
strcpy(数组[1][1]。名称,“t700nwzB”);
数组[1][1].id=8.6576;
数组[1][1]。年龄=85;
//Num3
strcpy(数组[1][2]。名称,“Vx8eR”);
数组[1][2].id=179;
数组[1][2]。年龄=59;
//Num4
strcpy(数组[1][3]。名称,“n5FUgA”);
数组[1][3].id=1.082797;
数组[1][3]。年龄=45;
//Num5
strcpy(数组[1][4]。名称,“Bzm9dq”);
数组[1][4].id=179;
数组[1][4]。年龄=23;
}
我认为“发送一组人员”对函数进行排序,并在函数中对其进行排序有助于解决您的问题
void main {
//Swap (&person, &person) === Swap(&array[1][i], &array[i][j])
//Selection sort(array[1])
//Bubble sort(array[1])
}
像这样。这将向每个排序函数发送person数组的地址,所以您只需要执行其中的每个函数
void selectionSort(person*) {
for(int i = 0; i < length of person array; i++) {
(algorithm for sorting your person by age, name)
you can use them by person[i].age
}
}
person* A = calloc..
A.name = ".."
A.id = ".."
A.age = ".."
//and then put your struct in the array
array[1][i] = A;