7-13 Shell Sort

Use Shell sort algorithm and Shell Increment Sequence to sort a sequence in descending orders.

输入格式:

2 lines.
The first line means the number(<=20) of keys.
The second line is consisted of several integers divided by comma.

输出格式:

Several lines.
Each line shows the middle result of shell sort. Add '\n' to the end of each line

输入样例:

在这里给出一组输入。例如:

10
49,38,65,97,76,13,27,50,2,8,

输出样例:

在这里给出相应的输出。例如:

49,38,65,97,76,13,27,50,2,8,
76,97,65,50,49,38,27,13,2,8,
97,76,65,50,49,38,27,13,8,2,

C语言代码

#include<stdio.h>

void printstring(int * A,int N){
    int i;
    for(i=0;i<N;i++){
        printf("%d,",A[i]);
    }
    printf("\n");
}

void ShellSort(int * A,int N){
    int Increment=N/2;
    int P,i;
    for(;Increment>0;Increment/=2){
        for(P=Increment;P<N;P++){
            int temp=A[P];
            for(i=P;i>=Increment;i-=Increment){
                if(temp>A[i-Increment]){
                    A[i]=A[i-Increment];
                }
                else break;
            }
            A[i]=temp;
        }
        printstring(A,N);
    }
}

int main(){
    int N;
    scanf("%d",&N);
    int A[N];
    int i;
    for(i=0;i<N;i++){
        scanf("%d,",&A[i]);
    }
    ShellSort(A,N);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值