zoukankan      html  css  js  c++  java
  • 冒泡排序--简单(c语言)

    //

    //  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(" 按学生姓名排序,采用冒泡排序 ");

        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(" %d %s %s %d ",stu1[i].num,

                   stu1[i].name,stu1[i].sex,stu1[i].age);

        }

        return 0;

    }

    运行结果:

  • 相关阅读:
    Hibernate关联映射
    mysql 外键约束
    巩固JavaSE基础--IDEA完成实战项目
    PHP--选择排序
    PHP--冒泡排序
    vscode调试单个PHP脚本文件
    Vscode下调试基于Homestead环境的Laravel框架
    Python Web开发
    Python之UDP编程
    Python之TCP编程
  • 原文地址:https://www.cnblogs.com/duanqibo/p/11200572.html
Copyright © 2011-2022 走看看