Aizu ALDS1_2_D Shell Sort(希尔排序)

#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);

#define INF 0x3f3f3f3f
#define eps 1e-5

typedef long long LL;
const double pi = acos(-1.0);
const long long mod = 25 * 1E8;
using namespace std;

int a[1000005];
LL cnt;
int l;
int N;
vector<int> G;

void insertionSort(int g,int N)
{
    for(int i = g;i < N;i++)
    {
        int v = a[i];
        int j = i - g;
        while(j >= 0 && a[j] > v)
        {
            a[j + g] = a[j];
            j -= g;
            cnt++;
        }
        a[j + g] = v;
    }
}

void shellSort(int N)
{
    int h = 1;
    while(1)
    {
        //if(h > N)
            //break;
        //printf("h = %d,N = %d\n",h,N);
        G.push_back(h);
        h = 3 * h + 1;
        if(h > N)
            break;
    }
    for(int i = G.size() - 1;i >= 0;i--)
        insertionSort(G[i],N);
}

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(0);cin.tie(0);
    int N;
    cin >> N;
    //printf("N = %d\n",N);
    for(int i = 0;i < N;i++)
        cin >> a[i];
    cnt = 0;
    shellSort(N);
    cout << G.size() << endl;
    for(int i = G.size() - 1;i > 0;i--)
        printf("%d ",G[i]);
    printf("%d\n",G[0]);
    printf("%d\n",cnt);
    for(int i = 0;i < N;i++)
        printf("%d\n",a[i]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值