直接插入排序--简单(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("\n\t按学生姓名排序\n\n");

    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<<'\t'<<name<<'\t'<<sex<<'\t'<<age<<'\t'<<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<<"学号"<<'\t'<<"姓名"<<'\t'<<"性别"<<'\t'<<"年龄"<<endl;

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

    {

        stu[i].show();

        cout<<endl;

    }

    return 0;

}

 

 

运行结果:

 

转载于:https://www.cnblogs.com/duanqibo/p/11200423.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值