zoukankan      html  css  js  c++  java
  • 直接插入排序--简单(c++)

    //

    //  main.cpp

    //  straightinsert_sortc++

    //

    //  Created by duanqibo on 2019/7/17.

    //  Copyright © 2019年 duanqibo. All rights reserved.

    //  直接插入排序 c++

    #include <iostream>

    #include <string>

    #define N 5

    using namespace std;

    //定义学生类 

    class student

    {

    private:

        int num;

        char name[20];

        char sex[20];

        int age;

    public:

        student(){}; //无参数的构造函数

        //带参数的构造函数

        student(int n,char na[20],char se[2],int ag)

        {

            num=n;

            strcpy(name,na);

            strcpy(sex,se);

            age=ag;

        }

        void friend straightinsert_sort(student stud[],int n);   //友元函数

        void show();

        

    };

    //按姓名直接插入排序

    void straightinsert_sort(student stud[],int n)

    {

        int i,j;

        student temp;

        printf(" 按学生姓名排序 ");

        for(i=1;i<n;i++)

        {

            temp=stud[i];

            j=i-1;

            while((j>=0)&&(strcmp(temp.name,stud[j].name)<0))

            {

                stud[j+1]=stud[j];

                j--;

            }

            stud[j+1]=temp;

        }

    }

    void student::show()

    {

        cout<<num<<' '<<name<<' '<<sex<<' '<<age<<' '<<endl;

    }

    int main(int argc, const char * argv[]) {

        // insert code here...

        student stu[N]={student(1001,"zhangsan","m",20),

            student(1002,"lisi","f",19),

            student(1003,"wangwu","m",18),

            student(1004,"zhaoliu","m",19),

            student(1005,"maqiang","m",20)

        };

        int len=sizeof(stu)/sizeof(stu[0]);

        straightinsert_sort(stu,len);

        cout<<"学号"<<' '<<"姓名"<<' '<<"性别"<<' '<<"年龄"<<endl;

        for(int i=0;i<len;i++)

        {

            stu[i].show();

            cout<<endl;

        }

        return 0;

    }

    运行结果:

  • 相关阅读:
    整形数组与字符串(字符数组)nex_permutation(或者是prve_permutation)的区别
    Dijkstra算法
    弗洛伊德算法(Floyd)
    Happy 2006
    EVENTTARGET 、EVENTARGUMENT 和VIEWSTATE
    C# App.config全攻略
    C#对Excel的样式操作
    Web.Config全攻略
    C# Setting.settings .
    UVa 10050 Hartals
  • 原文地址:https://www.cnblogs.com/duanqibo/p/11200423.html
Copyright © 2011-2022 走看看