排序方法之插入排序

21 篇文章 0 订阅

排序方法之插入排序


插入排序的思想重点是如果插入在中间 那么需要将元素后移,插入的时候就排好顺序
 

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>


#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#define MAXSIZE 20

typedef int KeyType;

typedef struct{
    KeyType key;

}RedType;//record type

typedef struct{
    RedType r[MAXSIZE+1];//0是做哨兵或者缓冲区的
    int length;
}SqList;

void
print_array(int array[], int n)
{
    int i;

    for(i = 0; i < n; i++)
        printf("%d ", array[i]);
    printf("\n");
}

//插入排序
void
insertion_sort(int array[], int n)
{
    int j, p;
    int tmp;

    //从前往后找
    for(p=1;p<n;p++)
    {
        tmp = array[p];
        for(j=p;j>0 && array[j-1] > tmp;j--)
        {
            array[j] = array[j-1];
        }
        array[j] = tmp;
    }
}


int main()
{
    {8,32,34,51,64}
    int array[] = {34, 8, 64, 51, 32, 21};

    printf("Before sorted:");
    print_array(array, 6);
    insertion_sort(array, 6);
    printf("After sorted :");
    print_array(array, 6);

    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值