//
// main.cpp
// bubble
//
// Created by duanqibo on 2019/7/17.
// Copyright © 2019年 duanqibo. All rights reserved.
// 冒泡排序 c语言
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 4
typedef struct student
{
int num;
char name[20];
char sex[2];
int age;
}stu[N];
//按姓名冒泡排序
void bubble_sort(struct student stud[],int n)
{
int i,j;
struct student temp;
printf("\n\t按学生姓名排序,采用冒泡排序\n\n");
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(strcmp(stud[j].name,stud[j+1].name)>0)
{
temp=stud[j];
stud[j]=stud[j+1];
stud[j+1]=temp;
}
}
}
}
int main(int argc, const char * argv[]) {
// insert code here...
student stu1[4]={{1001,"zhangsan","m",20},
{1002,"lisi","f",21},
{1003,"wangwu","m",19},
{1004,"zhaoliu","f",20}};
int i,len;
len=sizeof(stu1)/sizeof(stu1[0]);
bubble_sort(stu1,len);
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 0;
}
运行结果: