冒泡排序
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct{ int NO; int Age; char Name[50]; }Student; typedef struct{ int StudentCount; Student *data; }Sqlist; void bubbleSort(Sqlist &L) { int i,j; Student p; for(i=0;i<L.StudentCount;i++) { for(j=0;j<L.StudentCount-1;j++) { if(L.data[j].NO<L.data[j+1].NO) { p=L.data[j]; L.data[j]=L.data[j+1]; L.data[j+1]=p; } } } } int main() { Sqlist L; L.StudentCount=4; L.data = (Student *)malloc(L.StudentCount * sizeof(Student)); L.data[0].Age=20; strcpy(L.data[0].Name,"zhangsan"); L.data[0].NO=1115; L.data[1].Age=20; strcpy(L.data[1].Name,"lisi"); L.data[1].NO=1112; L.data[2].Age=20; strcpy(L.data[2].Name,"wanger"); L.data[2].NO=1113; L.data[3].Age=20; strcpy(L.data[3].Name,"mazi"); L.data[3].NO=1114; bubbleSort(L); for (int i = 0; i < L.StudentCount; i++) { printf("NO: %d, Age: %d, Name: %s\n", L.data[i].NO, L.data[i].Age, L.data[i].Name); } return 0; }