//按姓名快速排序
#include <stdio.h>
#include <string.h>
#define N 10
typedef struct student
{
int num;
char name[20];
char sex[2];
int age;
}stu[N];
int quickpartition(struct student stu[],int low,int high)
{
struct student x;
x=stu[low];
while(low<high)
{
while((low<high)&&(strcmp(stu[high].name,x.name)<=0))
high--;
stu[low]=stu[high];
while((low<high)&&(strcmp(stu[low].name,x.name)>=0))
low++;
stu[high]=stu[low];
}
stu[low]=x;
return high;
}
void quicksort(struct student stud[],int low,int high)
{
int temp;
if(low<high)
{
temp=quickpartition(stud,low,high);
quicksort(stud,low,temp-1);
quicksort(stud,temp+1,high);
}
}
int main()
{
struct student stu1[4]={{1001,"zhang","m",19},
{1002,"chen","f",20},
{1003,"ma","m",20},
{1004,"sun","m",18}};
int len,i;
len=sizeof(stu1)/sizeof(stu1[0]);
quicksort(stu1,0,len-1);
for(i=0;i<len;i++)
{
printf("\t%d\t%s\t%s\t%d\t\n",stu1[i].num,stu1[i].name,stu1[i].sex,stu1[i].age);
}
return 1;
}
运行结果: