算法设计:直接插入排序

2020年1月17日 981点热度 0人点赞 0条评论

算法设计:直接插入排序

时间: 1ms        内存:128M

描述:

算法设计:实现直接插入排序。void InsertSort(RecType R[],int n)为对R[0..n-1]按递增有序进行直接插入排序。主函数已经给出。

注意:只提交void InsertSort(RecType R[],int n) //R[0..n-1]部分。

#include <stdio.h>
#define MAXE 20         //线性表中最多元素个数
typedef int KeyType;
typedef char InfoType[10];
typedef struct          //记录类型
{
    KeyType key;        //关键字项
    InfoType data;      //其他数据项,类型为InfoType
} RecType;

int main()
{
    int i,k,n;
    KeyType a[100];
    RecType R[MAXE];
    scanf("%d",&n);
    for (i=0; i<n; i++)
        scanf("%d",&a[i]);
 
    for (i=0; i<n; i++)
        R[i].key=a[i];
    printf("初始关键字: ");      //输出初始关键字序列
    for (k=0; k<n; k++)
        printf("%3d",R[k].key);
    printf("\n");
    InsertSort(R,n);
    printf("最后结果: ");       //输出初始关键字序列
    for (k=0; k<n; k++)
        printf("%3d",R[k].key);
    printf("\n");
    return 0;
}

输入:

输入带排序元素的个数

输入待排序的整数

输出:

输出初识数据

输出排序后的数据

示例输入:

10 
9 2 7 5 6 4 8 3 1 0

示例输出:

初始关键字:   9  2  7  5  6  4  8  3  1  0
最后结果:   0  1  2  3  4  5  6  7  8  9

提示:

参考答案:

解锁文章

没有看到答案?微信扫描二维码可免费解锁文章

微信扫描二维码解锁

使用微信扫描二维码打开广告页面后可以立即关闭,再刷新此页面即可正常浏览此文章

所跳转广告均由第三方提供,并不代表本站观点!

已经扫描此二维码?点此立即跳转

code

这个人很懒,什么都没留下

文章评论