• Shell Sort Aizu


    Shell Sort is a generalization of Insertion Sort to arrange a list of n elements A.

    1 insertionSort(A, n, g)
    2 for i = g to n-1
    3 v = A[i]
    4 j = i - g
    5 while j >= 0 && A[j] > v
    6 A[j+g] = A[j]
    7 j = j - g
    8 cnt++
    9 A[j+g] = v
    10
    11 shellSort(A, n)
    12 cnt = 0
    13 m = ?
    14 G[] = {?, ?,…, ?}
    15 for i = 0 to m-1
    16 insertionSort(A, n, G[i])
    A function shellSort(A, n) performs a function insertionSort(A, n, g), which considers every g-th elements. Beginning with large values of g, it repeats the insertion sort with smaller g.

    Your task is to complete the above program by filling ?. Write a program which reads an integer n and a sequence A, and prints m, Gi(i=0,1,…,m−1) in the pseudo code and the sequence A in ascending order. The output of your program must meet the following requirements:

    1≤m≤100
    0≤Gi≤n
    cnt does not exceed ⌈n1.5⌉

    Input

    In the first line, an integer n is given. In the following n lines, Ai(i=0,1,…,n−1) are given for each line.

    Output

    In the first line, print an integer m. In the second line, print m integers Gi(i=0,1,…,m−1) separated by single space character in a line.
    In the third line, print cnt in a line. In the following n lines, print Ai(i=0,1,…,n−1) respectively.

    This problem has multiple solutions and the judge will be performed by a special validator.

    Constraints

    1≤n≤1,000,000
    0≤Ai≤109

    Sample Input 1

    5
    5
    1
    4
    3
    2

    Sample Output 1

    2
    4 1
    3
    1
    2
    3
    4
    5

    Sample Input 2

    3
    3
    2
    1

    Sample Output 2

    1
    1
    3
    1
    2
    3

    code

    /*
                                    ^....0
                                   ^ .1 ^1^
                                   ..     01
                                  1.^     1.0
                                 ^ 1  ^    ^0.1
                                 1 ^        ^..^
                                 0.           ^ 0^
                                 .0            1 .^
                                 .1             ^0 .........001^
                                 .1               1. .111100....01^
                                 00             ^   11^        ^1. .1^
                                 1.^                              ^0  0^
                                   .^                                 ^0..1
                                   .1                                   1..^
                                 1 .0                                     ^  ^
                                ^ 00.                                     ^^0.^
                                 1 ^ 0                                     ^^110.^
                               0.   0 ^                                    ^^^10.01
                       ^^     010^   1 1                                   ^^^1110.1
                       0001  10 0   ^ 1.1                                   ^^^1111110
                       0^ 10 . 01   ^^  ^^                                   ^^^1111^1.^           ^^^
                       10  10^ 0^                                             ^^111^^^0.1^       1....^
                        11     0                                               ^^11^^^ 0..  ....1^   ^ ^
                        1.     0^                                               ^11^^^ ^ 1 111^     ^ 0.
                       10   00 11                                               ^^^^^   1 0           1.
                       0^  ^0  ^0                                                ^^^^    0            0.
                       0^  1.0  .^                                               ^^^^    1 1          .0
                       ^.^  ^^  0^                             ^1                ^^^^     0.         ^.1
                       1 ^      11                             1.                ^^^     ^ ^        ..^
                      ^..^      ^1                             ^.^               ^^^       .0       ^.0
                      0..^      ^0                              01               ^^^       ..      0..^
                     1 ..        .1                             ^.^              ^^^       1 ^  ^0001
                    ^  1.        00                              0.             ^^^        ^.0 ^.1
                    . 0^.        ^.^                             ^.^            ^^^         ..0.0
                   1 .^^.         .^                  1001        ^^            ^^^         . 1^
                   . ^ ^.         11                0.    1         ^           ^^          0.
                    0  ^.          0              ^0       1                   ^^^          0.
                  0.^  1.          0^             0       .1                   ^^^          ..
                  .1   1.          00            .        .1                  ^^^           ..
                 1      1.         ^.           0         .^                  ^^            ..
                 0.     1.          .^          .         0                                  .
                 .1     1.          01          .        .                                 ^ 0
                ^.^     00          ^0          1.       ^                                 1 1
                .0      00           .            ^^^^^^                                   .
                .^      00           01                                                    ..
               1.       00           10                                                   1 ^
              ^.1       00           ^.                                            ^^^    .1
              ..        00            .1                                        1..01    ..
             1.1         00           1.                                       ..^      10
            ^ 1^         00           ^.1                                      0 1      1
            .1           00            00                                       ^  1   ^
             .           00            ^.^                                        10^  ^^
           1.1           00             00                                              10^
           ..^           1.             ^.                                               1.
          0 1            ^.              00                 00                            .^
            ^            ^.              ^ 1                00   ^0000^     ^               01
         1 0             ^.               00.0^              ^00000   1.00.1              11
         . 1              0               1^^0.01                      ^^^                01
          .^              ^                1   1^^                                       ^.^
        1 1                                                                              0.
        ..                                                                              1 ^
         1                                                                               1
       ^ ^                                                                             .0
       1                                                                             ^ 1
       ..                                                          1.1            ^0.0
      ^ 0                                                           1..01^^100000..0^
      1 1                                                            ^ 1 ^^1111^ ^^
      0 ^                                                             ^ 1      1000^
      .1                                                               ^.^     .   00
      ..                                                                1.1    0.   0
      1.                                                                  .    1.   .^
      1.                                                                 1    1.   ^0
     ^ .                                                                 ^.1 00    01
     ^.0                                                                  001.     .^
     */
    // Virtual_Judge —— Shell Sort Aizu - ALDS1_2_D .cpp created by VB_KoKing on 2019,04,28,19.
    /* Procedural objectives:
    
     Variables required by the program:
    
     Procedural thinking:
    
     Functions required by the program:
    
    */
    /* My dear Max said:
    "I like you,
    So the first bunch of sunshine I saw in the morning is you,
    The first hurricane that passed through your ear is you,
    The first star you see is also you.
    The world I see is all your shadow."
    
    FIGHTING FOR OUR FUTURE!!!
    */
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    using namespace std;
    
    vector<int> G;
    long long cnt=0;
    int l,A[1000000],n;
    
    //指定了间隔g的插入排序
    void insert_ion_sort(int A[],int n,int g)
    {
        for (int i = g; i < n; i++) {
            int v=A[i],j=i-g;
            while (j>=0&&A[j]>v)
            {
                A[j+g]=A[j];
                j-=g;
                cnt++;
            }
            A[j+g]=v;
        }
    }
    
    void shell_sort(int A[],int n)
    {
        //生成数列G={1,4,13,40,121,……}
        int h=1;
        while (true)
        {
            if (h>n) break;
            G.push_back(h);
            h=3*h+1;
        }
    
        for (int i = G.size()-1; i >=0 ; i--)
            //按逆序制定G[i]=g
            insert_ion_sort(A,n,G[i]);
    }
    
    int main()
    {
        cin>>n;
        for (int i = 0; i < n; i++)
            scanf("%d",&A[i]);
    
        shell_sort(A,n);
    
        cout<<G.size()<<endl;
        for (int i = G.size()-1; i >=0 ; i--) {
            printf("%d",G[i]);
            if (i) printf(" ");
        }
        cout<<endl<<cnt<<endl;
        for (int i = 0; i < n; i++)
            printf("%d
    ",A[i]);
        return 0;
    }
    
  • 相关阅读:
    数学基础详解 1——微积分
    logistic回归梯度上升优化算法
    决策树
    西瓜书学习笔记(1)——模型评估与选择
    关于map与set的一点理解;
    STL中的set容器的一点总结(转)
    HDOJ 题目分类
    Train Problem I(栈)
    猜数字(规律)
    Change the ball(找规律)
  • 原文地址:https://www.cnblogs.com/AlexKing007/p/12338397.html
Copyright © 2020-2023  润新知