冒泡法排序结构体数组c语言程序
时间: 2023-12-29 17:25:54 浏览: 104
以下是使用冒泡排序算法对结构体数组进行排序的C语言程序:
```c
#include <stdio.h>
struct Person {
int no;
int age;
int height;
};
void sort(struct Person *array, int n) {
int i, j;
struct Person temp;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (array[j].no > array[j+1].no) {
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
}
int main() {
struct Person people[] = {
{3, 25, 180},
{1, 30, 170},
{2, 20, 190}
};
int n = sizeof(people) / sizeof(people[0]);
printf("Before sorting:\n");
for (int i = 0; i < n; i++) {
printf("No: %d, Age: %d, Height: %d\n", people[i].no, people[i].age, people[i].height);
}
sort(people, n);
printf("\nAfter sorting:\n");
for (int i = 0; i < n; i++) {
printf("No: %d, Age: %d, Height: %d\n", people[i].no, people[i].age, people[i].height);
}
return 0;
}
```
阅读全文