#include<stdio.h> #include<malloc.h> void put(int* p,int n) { for(int i=0;i<n;i++) { printf("请输入第%d个数:",i+1); scanf("%d",p+i); } } void sort(int* p,int n) { int temp; for(int i=0;i<n-1;i++) for(int j=0;j<n-1-i;j++) { if(*(p+j) > *(p+j+1)) { temp=*(p+j); *(p+j)=*(p+j+1); *(p+j+1)=temp; } } } void print(int* p,int n) { for(int i=0;i<n;i++) { printf("%d ",*(p+i)); } printf("/n"); } void main() { int n; printf("请输入排序的数字个数:"); scanf("%d",&n); int* p=(int*)malloc(sizeof(int)*n); put(p,n); print(p,n); sort(p,n); print(p,n); free(p); } 学生成绩的排序 #include<stdio.h> #include<string.h> #include<malloc.h> struct student { int sid; char name[20]; int age; int score; }; void input(student* ps,int n) { for(int i=0;i<n;i++) { printf("sid="); scanf("%d",&(ps+i)->sid); printf("name="); scanf("%s",(ps+i)->name); printf("age="); scanf("%d",&(ps+i)->age); printf("score="); scanf("%d",&(ps+i)->score); } } void sort(student* ps ,int n) { student st; for(int i=0;i<n-1;i++) { for(int j=0;j<n-1-i;j++) { if((ps+j)->score<(ps+j+1)->score) { st=*(ps+j); *(ps+j)=*(ps+j+1); *(ps+j+1)=st; } } } } void print(student* ps,int n) { for(int i=0;i<n;i++) { printf("%d/n",(ps+i)->sid); printf("%s/n",(ps+i)->name); printf("%d/n",(ps+i)->age); printf("%d/n",(ps+i)->score); printf("/n"); } } void main() { int n; printf("请输入学生数目:"); scanf("%d",&n); struct student* ps=(student*)malloc(sizeof(student)*n); input(ps,n); sort(ps,n); print(ps,n); free(ps); }