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;

    }

    运行结果:

  • 相关阅读:
    并发与并行
    OpenCV 图像集合操作
    C++ 输出时间
    绘制模型图
    检测图像文件是否损坏
    QImage,Mat ,QByteArray转换
    图像拼接3
    图像拼接2】
    图像拼接 Stitcher
    《将博客搬至CSDN》
  • 原文地址:https://www.cnblogs.com/duanqibo/p/11200572.html
Copyright © 2011-2022 走看看