直接插入排序--简单(c语言)

//

//  main.cpp

//  straightinsert_sort

//

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

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

//  直接插入排序

 

#include <iostream>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define N 10

//定义结构体的数据类型

typedef struct student

{

    int num;

    char name[20];

    char sex[2];

    int age;

}stu[N];

 

//按姓名,直接插入排序

void straightinsert_sort(struct student stud[], int n)

{

    int i, j;

    struct student temp;

    printf("\n\t按姓名排序:\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;

    }

}

 

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

    // insert code here...

    struct student stu1[5];

    int i, len;

    printf("请输入5个学生的情况:\n");

    for (i = 0; i<5; i++)

    {

        scanf("%d%s%s%d", &stu1[i].num,

              stu1[i].name,

              stu1[i].sex,

              &stu1[i].age);

    }

    len = sizeof(stu1) / sizeof(stu1[0]);

    straightinsert_sort(stu1, len);

    printf("学号\t姓名\t性别\t年龄\n");

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

    {

        printf("%d\t%s\t%s\t%d\n", stu1[i].num,

               stu1[i].name,

               stu1[i].sex,

               stu1[i].age);

    }

    printf("%d\n", len);

    system("pause");

    return 1;

}

 

运行结果:

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值