历史上最全的【插入排序集锦】上

代码是最好的文档【虽然很违心】

#include <stdio.h>

#define SIZE 10

//直接插入排序
void zhi_jie_cha_ru(int a[]) {
    int temp;//标兵位
    for(int i=1; i<SIZE; i++) {
        temp = a[i];
        int j;
        for(j=i-1; temp<a[j]&&j>=0; j--) {
            a[j+1]=a[j];
        }
        a[j+1]=temp;
    }
    for(int i=0; i<SIZE; i++) {
        printf("%d ",a[i]);
    }
}

// 折半插入排序
void zhe_ban_cha_ru(int a[]) {
    int temp;//标兵位
    for(int i=1; i<SIZE; i++) {
        int low =0,heigh=i-1;
        temp=a[i];
        while(low<=heigh) {
            if(a[i]>a[(heigh+low)/2]) {
                low = (heigh+low)/2+1;
            } else {
                heigh=(heigh+low)/2-1;
            }
        }
        for(int j=i-1; j>heigh; j--)a[j+1]=a[j];
        a[heigh+1]=temp;
    }
    for(int i=0; i<SIZE; i++) {
        printf("%d ",a[i]);
    }

}

//2-路插入排序,构造静态循环链表 
void lu_2_cha_ru(int a[]) {
    int b[SIZE],temp;
    int first=0,final=0;//标志位,first代表循环入口,final代表循环出口
    b[0]=a[0];
    for(int n = 1; n<SIZE; n++) {
        temp=a[n];
        int i=0;
        if(a[n]>b[0]) {
            for(i=final; temp<b[i]&&i>0; i--) {
                b[i+1]=b[i];
            }
            b[i+1]=temp;
            final ++;
        } else {
            if(first==0) {
               first=SIZE-1;
               b[first]=temp;
            } else {
                for(i=first; temp>b[i]&&i<SIZE; i++) {
                    b[i-1]=b[i];
                }
                b[i-1]=temp;
                first --;
            }
        }
    }
    for(int i=0; i<SIZE; i++) {
        printf("%d ",b[i]);
    }
}

int main() {
//    int a[SIZE]= {100,56};
    int a[SIZE]= {100,56,1,0,23,3,5,116,3,2};
//    int a[SIZE]= {1,2,3,4,5,6,7,8,9,10};
//    int a[SIZE]= {100,20,3,4};
//    zhi_jie_cha_ru(a);
//    printf("\n");
//    zhe_ban_cha_ru(a);
    lu_2_cha_ru(a);;
    return 1;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值