直接插入排序算法练习

直接插入排序算法练习
Time Limit: 1000 MS Memory Limit: 32768 K
Total Submit: 462 (218 users) Total Accepted: 246 (217 users) Special Judge: No
Description
利用直接插入排序算法实现递增排序。
Input
输入一组整数
Output

输出递增的整数序列
Sample Input
2 1 5 4 3
Sample Output
1 2 3 4 5

#include<stdio.h>
#include<bits/stdc++.h>
#include<iostream>
#include<stdlib.h>
#define maxn 1005
#define ElemType int
#define OK 1
using namespace std;
typedef struct
{
    ElemType key;
} RcdType;
typedef struct
{
    ElemType *elem;
    int length;
    int listsize;
} sqlist;
sqlist start()
{
    sqlist l;
    l.elem=(int *)malloc(maxn *sizeof(ElemType));
    l.length=0;
    l.listsize=maxn;
    return l;
}
void insert1 (sqlist &l,ElemType e)
{
    l.elem[l.length]=e;
    l.length++;
}
int InsertSort(sqlist &L)//插入
{
    int i,j;
         for( i=1;i<L.length;i++) {
             int temp=L.elem[i];
             for(  j=i-1;j>=0&&temp<L.elem[j];j--)
                    L.elem[j+1]=L.elem[j];
             L.elem[j+1]=temp;
           }
}
void output(sqlist &l)//输出
{
     for(int i=0; i<l.length; i++)
        printf("%d ",l.elem[i]);


}
int main()
{
    sqlist l;
    l=start();
    int a;
    while(~scanf("%d",&a))
    {
        insert1(l,a);
    }
    InsertSort(l);
    output(l);
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值