#include <iostream>
#define Maxitem 100
typedef int KeyType;
typedef char ElemType[5];
typedef struct rec{
KeyType key; //key
ElemType data; //数据域
} elemNode[Maxitem] ;
struct rec sqlist[Maxitem];
void bubblesort(elemNode r , int n){
int i ,j;
for(i=1; i<= n-1 ; i++){
for(j = n;j >= i+1;j--){
if(r[j].key < r[j-1].key){ //注意遍历比较从小到大
r[0]=r[j]; //交换
r[j]=r[j-1];
r[j-1]=r[0];
}
}
}
printf("从低到高排列:\n");
for(i=1;i<=n;i++){
printf("%6d",r[i].key);
}
printf("\n");
for(i=1;i<=n;i++){
printf("%6s",r[i].data);
}
printf("\n");
}
void main(){
elemNode s={0,"",75,"王华",87,"李英",68,"张萍",92,"陈涛",88,"刘丽",62,"张强",77,"孙军",96,"朱斌",80,"许伟",72,"曾压"};
int n=10;
bubblesort(s,n);
}
测试结果
从低到高排列:
62 68 72 75 77 80 87 88 92 96
张强 张萍 曾压 王华 孙军 许伟 李英 刘丽 陈涛 朱斌