prithivMLmods/Coder-Stat huggingface dataset:
Problem p02262 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  int A[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", A[i]);
  }
  int cnt = 0;
  vector<int> G;
  for (int h = 1;;) {
    if (h > n) {
      break;
    }
    G.push_back(h);
    h = 3 * h + 1;
  }
  for (int i = G.size() - 1; i >= 0; i--) {
    for (int j = G[i]; j < n; j++) {
      int v = A[j];
      int k = j - G[i];
      while (k >= 0 && A[k] > v) {
        A[k + G[i]] = A[k];
        k -= G[i];
        cnt++;
      }
      A[k + G[i]] = v;
    }
  }
  cout << G.size() << endl;
  for (int i = G.size() - 1; i >= 0; i--) {
    cout << G[i] << (i == 0 ? "" : " ") << flush;
  }
  cout << endl;
  cout << cnt << endl;
  for (int i = 0; i < n; i++) {
    cout << A[i] << endl;
  }
  return 0;
}


Fixed submission:
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  int A[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }
  int cnt = 0;
  vector<int> G;
  for (int h = 1;;) {
    if (h > n) {
      break;
    }
    G.push_back(h);
    h = 3 * h + 1;
  }
  for (int i = G.size() - 1; i >= 0; i--) {
    for (int j = G[i]; j < n; j++) {
      int v = A[j];
      int k = j - G[i];
      while (k >= 0 && A[k] > v) {
        A[k + G[i]] = A[k];
        k -= G[i];
        cnt++;
      }
      A[k + G[i]] = v;
    }
  }
  cout << G.size() << endl;
  for (int i = G.size() - 1; i >= 0; i--) {
    cout << G[i] << (i == 0 ? "" : " ") << flush;
  }
  cout << endl;
  cout << cnt << endl;
  for (int i = 0; i < n; i++) {
    cout << A[i] << endl;
  }
  return 0;
}
Problem p02262 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>

using namespace std;

const int maxn = 105;

int s[maxn], n, cnt;
vector<int> v;

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

void shellsort() {
  for (int h = 1;;) {
    if (h > n) {
      break;
    }
    v.push_back(h);
    h = h * 3 + 1;
  }
  int len = v.size() - 1;
  for (int i = len; i >= 0; i--) {
    insertionsort(v[i]);
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &s[i]);
  }
  shellsort();
  cout << v.size() << endl;
  for (int i = v.size() - 1; i >= 0; i--) {
    printf("%d", v[i]);
    if (i) {
      printf(" ");
    }
  }
  printf("\n");
  printf("%d\n", cnt);
  for (int i = 0; i < n; i++) {
    printf("%d\n", s[i]);
  }
  return 0;
}
/*
5
5 1 4 3 2
*/


Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e6 + 5;

int s[maxn], n, cnt;
vector<int> v;

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

void shellsort() {
  for (int h = 1;;) {
    if (h > n) {
      break;
    }
    v.push_back(h);
    h = h * 3 + 1;
  }
  int len = v.size() - 1;
  for (int i = len; i >= 0; i--) {
    insertionsort(v[i]);
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &s[i]);
  }
  shellsort();
  cout << v.size() << endl;
  for (int i = v.size() - 1; i >= 0; i--) {
    printf("%d", v[i]);
    if (i) {
      printf(" ");
    }
  }
  printf("\n");
  printf("%d\n", cnt);
  for (int i = 0; i < n; i++) {
    printf("%d\n", s[i]);
  }
  return 0;
}
/*
5
5 1 4 3 2
*/
Problem p02262 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <math.h>
using namespace std;
int insertionSort(int, int, int);
void shellSort(int *, int);
int cnt = 0;
int main(void) {
  int A[100], N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  shellSort(A, N);
}
int insertionSort(int A[], int n, int g) {
  int v, j, cnt = 0;
  for (int i = g; i < n; i++) {
    v = A[i];
    j = i - g;
    while (j >= 0 && A[j] > v) {
      A[j + g] = A[j];
      j = j - g;
      cnt++;
    }
    A[j + g] = v;
  }
  return (cnt);
}
void shellSort(int A[], int n) {
  int cnt = 0;
  int m = 0;
  int G[13] = {};
  do {
    if (m == 0) {
      G[0] = n - 1;
    } else {
      G[m] = G[m - 1] / 2;
    }
    m++;
  } while (G[m - 1] > 1);
  cout << m << endl;
  for (int i = 0; i < m; i++) {
    cnt += insertionSort(A, n, G[i]);
    cout << G[i] << " ";
  }
  cout << endl << cnt << endl;
  for (int i = 0; i < n; i++) {
    cout << A[i] << endl;
  }
}

Fixed submission:
#include <iostream>
#include <math.h>
using namespace std;
int insertionSort(int, int, int);
void shellSort(int *, int);
int cnt = 0;
int main(void) {
  int A[1000000], N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  shellSort(A, N);
}
int insertionSort(int A[], int n, int g) {
  int v, j, cnt = 0;
  for (int i = g; i < n; i++) {
    v = A[i];
    j = i - g;
    while (j >= 0 && A[j] > v) {
      A[j + g] = A[j];
      j = j - g;
      cnt++;
    }
    A[j + g] = v;
  }
  return (cnt);
}
void shellSort(int A[], int n) {
  int cnt = 0;
  int m = 0;
  int G[13] = {};
  do {
    if (m == 0) {
      G[0] = n - 1;
    } else {
      G[m] = G[m - 1] / 2;
    }
    m++;
  } while (G[m - 1] > 1);
  cout << m << endl;
  for (int i = 0; i < m; i++) {
    cnt += insertionSort(A, n, G[i]);
    cout << G[i] << " ";
  }
  cout << endl << cnt << endl;
  for (int i = 0; i < n; i++) {
    cout << A[i] << endl;
  }
}
Problem p02262 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//?????§????????????=?????\??????????????????????´???¨???????????????

void InsertionSort(vector<long> &A, long n, long g, long &cnt) {

  long i, v, j;

  // g????????????????????????,n???????´???°
  //
  for (i = g; i < n; ++i) {
    v = A[i];  // v = A[3];
    j = i - g; // j=3-2=1;
    while (j >= 0 && A[j] > v) {
      A[j + g] = A[j]; //????????????A[3]???A[1]?????£??\
			j = j - g;//
      ++cnt;
    }
    A[j + g] = v;
  }
}

int main() {

  int m;
  long n, i, j;
  long cnt = 0;
  long G[13] = {1,    4,    13,    40,    121,    364,   1093,
                3280, 9841, 29524, 88573, 265720, 797161};

  cin >> n;
  vector<long> A;
  A.resize(n, 0);

  for (i = 0; i < n; i++) {

    cin >> A[i];
  }

  for (i = 0; i < 12; i++) {
    if ((A.size() > G[i] && A.size() < G[i + 1]) || i == 12) {
      j = i;
      break;
    }
  }

  cout << j + 1 << endl;

  for (i = j; i >= 0; --i) {
    cout << G[i];
    if (i != 0)
      cout << " ";
    InsertionSort(A, n, G[i], cnt);
  }
  cout << endl;

  cout << cnt << endl;

  for (i = 0; i < n; i++) {
    cout << A[i] << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//?????§????????????=?????\??????????????????????´???¨???????????????

void InsertionSort(vector<long> &A, long n, long g, long &cnt) {

  long i, v, j;

  // g????????????????????????,n???????´???°
  //
  for (i = g; i < n; ++i) {
    v = A[i];  // v = A[3];
    j = i - g; // j=3-2=1;
    while (j >= 0 && A[j] > v) {
      A[j + g] = A[j]; //????????????A[3]???A[1]?????£??\
			j = j - g;//
      ++cnt;
    }
    A[j + g] = v;
  }
}

int main() {

  int m;
  long n, i, j;
  long cnt = 0;
  long G[13] = {1,    4,    13,    40,    121,    364,   1093,
                3280, 9841, 29524, 88573, 265720, 797161};

  cin >> n;
  vector<long> A;
  A.resize(n, 0);

  for (i = 0; i < n; i++) {

    cin >> A[i];
  }

  for (i = 0; i < 13; i++) {
    if ((A.size() >= G[i] && A.size() < G[i + 1]) || i == 12) {
      j = i;
      break;
    }
  }

  cout << j + 1 << endl;

  for (i = j; i >= 0; --i) {
    cout << G[i];
    if (i != 0)
      cout << " ";
    InsertionSort(A, n, G[i], cnt);
  }
  cout << endl;

  cout << cnt << endl;

  for (i = 0; i < n; i++) {
    cout << A[i] << endl;
  }

  return 0;
}
Problem p02262 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long double lb;

const int INF = 1 << 27;
const int MOD = (int)1e9 + 7;

int cnt;

void read(int *n, int a[]) {
  cin >> *n;
  rep(i, *n) cin >> a[i];
  return;
}

void insertion_sort(int a[], int n, int g) {
  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 = j - g;
      cnt++;
    }
    a[j + g] = v;
  }

  return;
}

void shell_sort(int a[], int n) {
  cnt = 0;
  int m = 0;
  int g[100];
  int k = n;
  g[0] = n - 1;
  for (m = 0; k > 0; k /= 2)
    m++;
  for (int i = 1; i < m; i++)
    g[i] = g[i - 1] / 2;
  cout << m << endl;
  rep(i, m) cout << g[i] << ' ';
  cout << endl;
  rep(i, m) insertion_sort(a, n, g[i]);
  return;
}

void print_array(int a[], int n) { rep(i, n) cout << a[i] << endl; }

int main(void) {
  int n;
  int a[10000];
  read(&n, a);
  shell_sort(a, n);
  cout << cnt << endl;
  print_array(a, n);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long double lb;

const int INF = 1 << 27;
const int MOD = (int)1e9 + 7;

int cnt;

void read(int *n, int a[]) {
  cin >> *n;
  rep(i, *n) cin >> a[i];
  return;
}

void insertion_sort(int a[], int n, int g) {
  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 = j - g;
      cnt++;
    }
    a[j + g] = v;
  }

  return;
}

void shell_sort(int a[], int n) {
  cnt = 0;
  int m = 0;
  int g[100];
  int k = n;
  g[0] = n - 1;
  for (m = 0; k > 0; k /= 2)
    m++;
  for (int i = 1; i < m; i++)
    g[i] = g[i - 1] / 2;
  cout << m << endl;
  rep(i, m) cout << g[i] << ' ';
  cout << endl;
  rep(i, m) insertion_sort(a, n, g[i]);
  return;
}

void print_array(int a[], int n) { rep(i, n) cout << a[i] << endl; }

int main(void) {
  int n;
  int a[1000010];
  read(&n, a);
  shell_sort(a, n);
  cout << cnt << endl;
  print_array(a, n);
  return 0;
}
Problem p02262 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>

int G[101];
int A[1000002];

int insertion_sort(int *A, int n, int g) {
  int cnt = 0;
  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;
  }
  return cnt;
}

int shell_sort(int *A, int n) {
  int cnt = 0;
  G[0] = 1;
  int m = 0;
  while (G[m] + 3 < n) {
    G[m + 1] = G[m] + 3;
    m++;
  }
  m++;
  printf("%d\n", m);
  for (int i = m - 1; i >= 0; --i) {
    if (i < m - 1)
      putchar(' ');
    printf("%d", G[i]);
    cnt += insertion_sort(A, n, G[i]);
  }
  printf("\n%d\n", cnt);
  for (int i = 0; i < n; ++i)
    printf("%d\n", A[i]);
}

int main(int argc, char const *argv[]) {
  int N;
  scanf("%d", &N);
  for (int i = 0; i < N; ++i)
    scanf("%d", &A[i]);

  shell_sort(A, N);

  return 0;
}

Fixed submission:
#include <cstdio>

int G[101];
int A[1000002];

int insertion_sort(int *A, int n, int g) {
  int cnt = 0;
  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;
  }
  return cnt;
}

int shell_sort(int *A, int n) {
  int cnt = 0;
  G[0] = 1;
  int m = 0;
  do {
    G[m + 1] = G[m] * 3 + 1;
  } while (G[++m] < n);
  printf("%d\n", m);
  for (int i = m - 1; i >= 0; --i) {
    if (i < m - 1)
      putchar(' ');
    printf("%d", G[i]);
    cnt += insertion_sort(A, n, G[i]);
  }
  printf("\n%d\n", cnt);
  for (int i = 0; i < n; ++i)
    printf("%d\n", A[i]);
}

int main(int argc, char const *argv[]) {
  int N;
  scanf("%d", &N);
  for (int i = 0; i < N; ++i)
    scanf("%d", &A[i]);

  shell_sort(A, N);

  return 0;
}
Problem p02262 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;
int cnt = 0;

void insertionSort(vector<int> &A, int g) {
  for (int i = g; i < A.size(); ++i) {
    if (A[i - g] > A[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 solve() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }
  cnt = 0;
  vector<int> G;
  int h = 1;
  while (N > h) {
    G.push_back(h);
    h = 3 * h + 1;
  }
  for (int i = G.size() - 1; i >= 0; --i) {
    insertionSort(A, G[i]);
  }
  cout << G.size() << endl;
  for (int i = G.size() - 1; i > 0; --i) {
    cout << G[i] << " ";
  }
  cout << G[0] << endl;
  cout << cnt << endl;
  for (int i = 0; i < A.size(); ++i) {
    cout << A[i] << endl;
  }
}

int main() {
  solve();
  return (0);
}

Fixed submission:
#include <iostream>
#include <vector>

using namespace std;
int cnt = 0;

void insertionSort(vector<int> &A, int g) {
  for (int i = g; i < A.size(); ++i) {
    if (A[i - g] > A[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 solve() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }
  cnt = 0;
  vector<int> G;
  int h = 1;
  while (N >= h) {
    G.push_back(h);
    h = 3 * h + 1;
  }
  for (int i = G.size() - 1; i >= 0; --i) {
    insertionSort(A, G[i]);
  }
  cout << G.size() << endl;
  for (int i = G.size() - 1; i > 0; --i) {
    cout << G[i] << " ";
  }
  cout << G[0] << endl;
  cout << cnt << endl;
  for (int i = 0; i < A.size(); ++i) {
    cout << A[i] << endl;
  }
}

int main() {
  solve();
  return (0);
}
Problem p02262 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;
long long cnt;
int A[1000005];
vector<int> G;
void insertionSort(int A[], int n, int g);
void shellSort(int A[], int n);

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }
  cnt = 0;
  shellSort(A, n);
  cout << G.size() << endl;
  for (int i = G.size() - 1; i >= 0; i--) {
    printf("%d", G[i]);
    if (i) {
      printf(" ");
    }
  }
  printf("\n");
  printf("%lld\n", cnt);
  for (int i = 0; i < n; i++) {
    printf("%d\n", A[i]);
  }
  return 0;
}

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

void shellSort(int A[], int n) {
  for (int h = 1;;) {
    if (h > n) {
      break;
    }
    G.push_back(h);
    h = 3 * h + 1;
  }

  for (int i = G.size() - 1; i >= 0; i--) {
    insertionSort(A, n, G[i]);
  }
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;
long long cnt;
int A[1000005];
vector<int> G;
void insertionSort(int A[], int n, int g);
void shellSort(int A[], int n);

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }
  cnt = 0;
  shellSort(A, n);
  cout << G.size() << endl;
  for (int i = G.size() - 1; i >= 0; i--) {
    printf("%d", G[i]);
    if (i) {
      printf(" ");
    }
  }
  printf("\n");
  printf("%lld\n", cnt);
  for (int i = 0; i < n; i++) {
    printf("%d\n", A[i]);
  }
  return 0;
}

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

void shellSort(int A[], int n) {
  for (int h = 1;;) {
    if (h > n) {
      break;
    }
    G.push_back(h);
    h = 3 * h + 1;
  }

  for (int i = G.size() - 1; i >= 0; i--) {
    insertionSort(A, n, G[i]);
  }
}
Problem p02262 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <string.h>

struct NUM {
  int cnt;
  int m;
  int G[100];
};

int InsertionSort(int A[], int n, int g) {
  int i, v, j, cnt = 0;
  for (i = g; i < n; i++) {
    v = A[i];
    j = i - g;
    while (j >= 0 && A[j] > v) {
      A[j + g] = A[j];
      j = j - g;
      cnt++;
    }
    A[j + g] = v;
  }
  return cnt;
}

struct NUM ShellSort(int A[], int n) {
  struct NUM num;
  int cnt = 0;
  int m = 0;
  int G[100];
  for (int i = n; i > 0; i = i / 2) {
    G[m] = i;
    m++;
  }
  for (int i = 0; i < m; i++) {
    cnt += InsertionSort(A, n, G[i]);
  }
  num.cnt = cnt;
  num.m = m;
  for (int i = 0; i < m; i++)
    num.G[i] = G[i];
  return num;
}

int main() {
  int N, A[1000];
  struct NUM num;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &A[i]);
  }
  num = ShellSort(A, N);
  printf("%d\n", num.m);
  for (int i = 0; i < num.m; i++) {
    if (i == num.m - 1) {
      printf("%d\n", num.G[i]);
      break;
    }
    printf("%d ", num.G[i]);
  }
  printf("%d\n", num.cnt);
  for (int i = 0; i < N; i++) {
    printf("%d\n", A[i]);
  }
  return 0;
}


Fixed submission:
#include <stdio.h>
#include <string.h>

struct NUM {
  int cnt;
  int m;
  int G[100];
};

int InsertionSort(int A[], int n, int g) {
  int i, v, j, cnt = 0;
  for (i = g; i < n; i++) {
    v = A[i];
    j = i - g;
    while (j >= 0 && A[j] > v) {
      A[j + g] = A[j];
      j = j - g;
      cnt++;
    }
    A[j + g] = v;
  }
  return cnt;
}

struct NUM ShellSort(int A[], int n) {
  struct NUM num;
  int cnt = 0;
  int m = 0;
  int G[100];
  for (int i = n; i > 0; i = i / 2) {
    G[m] = i;
    m++;
  }
  for (int i = 0; i < m; i++) {
    cnt += InsertionSort(A, n, G[i]);
  }
  num.cnt = cnt;
  num.m = m;
  for (int i = 0; i < m; i++)
    num.G[i] = G[i];
  return num;
}

int main() {
  int N, A[1000000];
  struct NUM num;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &A[i]);
  }
  num = ShellSort(A, N);
  printf("%d\n", num.m);
  for (int i = 0; i < num.m; i++) {
    if (i == num.m - 1) {
      printf("%d\n", num.G[i]);
      break;
    }
    printf("%d ", num.G[i]);
  }
  printf("%d\n", num.cnt);
  for (int i = 0; i < N; i++) {
    printf("%d\n", A[i]);
  }
  return 0;
}
Problem p02262 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

class cShell {
  int m_n;
  int cnt;
  vector<int> vA;
  vector<int> vg;

  void insertionSort(int g);

public:
  cShell(int n, vector<int> &v) {
    vA = v;
    m_n = n;
    cnt = 0;
  }

  void shellSort();

  int getm() { return vg.size(); }

  string getGapToStr();

  vector<int> getData() { return vA; }

  int getCnt() { return cnt; }
};

void cShell::insertionSort(int g) {
  for (int i = g; i < m_n; ++i) {
    int v = vA[i];
    int j = i - g;
    while (j >= 0 && vA[j] > v) {
      vA[j + g] = vA[j];
      j = j - g;
      cnt++;
    }
    vA[j + g] = v;
  }
}

void cShell::shellSort() {
  int cnt = 0;
  int m = vA.size();
  while (m /= 2) {
    vg.push_back(m);
  }
  for (int i = 0; i < vg.size(); ++i) {
    insertionSort(vg[i]);
  }
}

string cShell::getGapToStr() {
  ostringstream oss;
  for (int i = 0; i < vg.size() - 1; ++i) {
    oss << vg[i] << " ";
  }
  oss << vg[vg.size() - 1];
  return oss.str();
}

int main() {
  int n;
  cin >> n;
  cin.ignore();

  vector<int> va;
  for (int i = 0; i < n; ++i) {
    int t;
    cin >> t;
    va.push_back(t);
  }

  cShell cs(n, va);
  cs.shellSort();

  cout << cs.getm() << endl;
  cout << cs.getGapToStr() << endl;
  cout << cs.getCnt() << endl;
  va = cs.getData();
  for (int i = 0; i < va.size(); ++i) {
    cout << va[i] << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

class cShell {
  int m_n;
  int cnt;
  vector<int> vA;
  vector<int> vg;

  void insertionSort(int g);

public:
  cShell(int n, vector<int> &v) {
    vA = v;
    m_n = n;
    cnt = 0;
  }

  void shellSort();

  int getm() { return vg.size(); }

  string getGapToStr();

  vector<int> getData() { return vA; }

  int getCnt() { return cnt; }
};

void cShell::insertionSort(int g) {
  for (int i = g; i < m_n; ++i) {
    int v = vA[i];
    int j = i - g;
    while (j >= 0 && vA[j] > v) {
      vA[j + g] = vA[j];
      j = j - g;
      cnt++;
    }
    vA[j + g] = v;
  }
}

void cShell::shellSort() {
  int cnt = 0;
  int m = vA.size();
  if (m / 2 > 0) {
    while (m /= 2) {
      vg.push_back(m);
    }
  } else {
    vg.push_back(1);
  }
  for (int i = 0; i < vg.size(); ++i) {
    insertionSort(vg[i]);
  }
}

string cShell::getGapToStr() {
  ostringstream oss;
  for (int i = 0; i < vg.size() - 1; ++i) {
    oss << vg[i] << " ";
  }
  oss << vg[vg.size() - 1];
  return oss.str();
}

int main() {
  int n;
  cin >> n;
  cin.ignore();

  vector<int> va;
  for (int i = 0; i < n; ++i) {
    int t;
    cin >> t;
    va.push_back(t);
  }

  cShell cs(n, va);
  cs.shellSort();

  cout << cs.getm() << endl;
  cout << cs.getGapToStr() << endl;
  cout << cs.getCnt() << endl;
  va = cs.getData();
  for (int i = 0; i < va.size(); ++i) {
    cout << va[i] << endl;
  }

  return 0;
}
Problem p02262 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <ostream>
#include <vector>

void trace_space(std::vector<int> v) {
  for (std::vector<int>::iterator i = v.begin(); i != v.end(); ++i) {
    std::cout << *i << " ";
  }
  std::cout << std::endl;
}

void trace_endl(std::vector<int> v) {
  for (std::vector<int>::iterator i = v.begin(); i != v.end(); ++i) {
    std::cout << *i << std::endl;
  }
}

void insertion_sort(std::vector<int> &v, int n, int g, int &cnt) {
  for (int i = g; i < n; i++) {
    int t = v[i];
    int j = i - g;
    while (j >= 0 && v[j] > t) {
      v[j + g] = v[j];
      j = j - g;
      cnt++;
    }
    v[j + g] = t;
  }
}

void shell_sort(std::vector<int> &v, int n) {
  std::vector<int> G;
  for (int g = 1; g < n; g = 3 * g + 1)
    G.push_back(g);

  int c = 0;
  for (int i = 0; i < G.size(); i++) {
    insertion_sort(v, n, G[G.size() - i - 1], c);
  }
  //
  std::cout << G.size() << std::endl;
  for (int i = 0; i < G.size() - 1; i++) {
    std::cout << G[G.size() - i - 1] << " ";
  }
  std::cout << G[0] << std::endl;

  std::cout << c << std::endl;
}

int main() {
  int n;
  std::cin >> n;
  std::vector<int> a;
  a.resize(n, 0);
  for (int i = 0; i < n; i++) {
    std::cin >> a[i];
  }
  shell_sort(a, n);
  trace_endl(a);
  return 0;
}

Fixed submission:
#include <iostream>
#include <ostream>
#include <vector>

void trace_space(std::vector<int> v) {
  for (std::vector<int>::iterator i = v.begin(); i != v.end(); ++i) {
    std::cout << *i << " ";
  }
  std::cout << std::endl;
}

void trace_endl(std::vector<int> v) {
  for (std::vector<int>::iterator i = v.begin(); i != v.end(); ++i) {
    std::cout << *i << std::endl;
  }
}

void insertion_sort(std::vector<int> &v, int n, int g, int &cnt) {
  for (int i = g; i < n; i++) {
    int t = v[i];
    int j = i - g;
    while (j >= 0 && v[j] > t) {
      v[j + g] = v[j];
      j = j - g;
      cnt++;
    }
    v[j + g] = t;
  }
}

void shell_sort(std::vector<int> &v, int n) {
  std::vector<int> G;
  for (int g = 1; g <= n; g = 3 * g + 1)
    G.push_back(g);

  int c = 0;
  for (int i = 0; i < G.size(); i++) {
    insertion_sort(v, n, G[G.size() - i - 1], c);
  }
  //
  std::cout << G.size() << std::endl;
  for (int i = 0; i < G.size() - 1; i++) {
    std::cout << G[G.size() - i - 1] << " ";
  }
  std::cout << G[0] << std::endl;

  std::cout << c << std::endl;
}

int main() {
  int n;
  std::cin >> n;
  std::vector<int> a;
  a.resize(n, 0);
  for (int i = 0; i < n; i++) {
    std::cin >> a[i];
  }
  shell_sort(a, n);
  trace_endl(a);
  return 0;
}
Problem p02262 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <vector>
using namespace std;
#define NMAX 1000000

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

void printG(vector<int> v) {
  int N = v.size();
  for (int i = N - 1; i >= 1; --i) {
    printf("%d ", v[i]);
  }
  printf("%d\n", v[0]);
}

long long insertionSort(int A[], int g, int N) {
  long long cnt = 0;

  for (int i = g; i < N; ++i) {
    int tmp = A[i];
    int j = i - g;
    while (j >= 0 && A[j] > tmp) {
      A[j + g] = A[j];
      j -= g;
      ++cnt;
    }
    A[j + g] = tmp;
  }

  return cnt;
}

void shellSort(int A[], int N) {
  int m;
  long long cnt = 0;
  vector<int> G;

  for (int g = 1; g < N; g = 3 * g + 1) {
    G.push_back(g);
  }
  m = G.size();

  printf("%d\n", m);
  printG(G);

  for (int i = m - 1; i >= 0; --i) {
    cnt += insertionSort(A, G[i], N);
  }

  printf("%lld\n", cnt);
  printArr(A, N);
}

int main() {
  int N;
  static int A[NMAX];

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

  shellSort(A, N);

  return 0;
}


Fixed submission:
#include <cstdio>
#include <vector>
using namespace std;
#define NMAX 1000000

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

void printG(vector<int> v) {
  int N = v.size();
  for (int i = N - 1; i >= 1; --i) {
    printf("%d ", v[i]);
  }
  printf("%d\n", v[0]);
}

long long insertionSort(int A[], int g, int N) {
  long long cnt = 0;

  for (int i = g; i < N; ++i) {
    int tmp = A[i];
    int j = i - g;
    while (j >= 0 && A[j] > tmp) {
      A[j + g] = A[j];
      j -= g;
      ++cnt;
    }
    A[j + g] = tmp;
  }

  return cnt;
}

void shellSort(int A[], int N) {
  int m;
  long long cnt = 0;
  vector<int> G;

  for (int g = 1; g <= N; g = 3 * g + 1) {
    G.push_back(g);
  }
  m = G.size();

  printf("%d\n", m);
  printG(G);

  for (int i = m - 1; i >= 0; --i) {
    cnt += insertionSort(A, G[i], N);
  }

  printf("%lld\n", cnt);
  printArr(A, N);
}

int main() {
  int N;
  static int A[NMAX];

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

  shellSort(A, N);

  return 0;
}
Problem p02262 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
using namespace std;

int cnt = 0;
int m = 100;
int G[100] = {};
void insertionsort(int A[], int n, int g) {
  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 = j - g;
      cnt++;
    }
    A[j + g] = v;
  }
}
void shellsort(int A[], int n) {
  for (int i = m - 1; i >= 0; i--) {
    if (i == m - 1)
      G[i] = 1;
    else
      G[i] = G[i + 1] * 3 + 1;
  }
  for (int i = 0; i < m; i++) {
    insertionsort(A, n, G[i]);
  }
}
int main() {
  int n;
  cin >> n;
  int A[n];
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  shellsort(A, n);
  int mtrue = 0;
  for (int i = 0; i < m; i++) {
    if (G[i] <= n)
      mtrue++;
  }
  cout << mtrue << endl;
  for (int i = 0; i < m; i++) {
    if (G[i] <= n)
      cout << G[i] << " ";
  }
  cout << endl << cnt << endl;
  for (int i = 0; i < n; i++) {
    cout << A[i] << endl;
  }

  return 0;
}


Fixed submission:
#include <cmath>
#include <iostream>
using namespace std;

int cnt = 0;
int m = 20;
int G[20] = {};
void insertionsort(int A[], int n, int g) {
  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 = j - g;
      cnt++;
    }
    A[j + g] = v;
  }
}
void shellsort(int A[], int n) {
  for (int i = m - 1; i >= 0; i--) {
    if (i == m - 1)
      G[i] = 1;
    else
      G[i] = G[i + 1] * 3 + 1;
  }
  for (int i = 0; i < m; i++) {
    insertionsort(A, n, G[i]);
  }
}
int main() {
  int n;
  cin >> n;
  int A[n];
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  shellsort(A, n);
  int mtrue = 0;
  for (int i = 0; i < m; i++) {
    if (G[i] <= n)
      mtrue++;
  }
  cout << mtrue << endl;
  for (int i = 0; i < m; i++) {
    if (G[i] <= n)
      cout << G[i] << " ";
  }
  cout << endl << cnt << endl;
  for (int i = 0; i < n; i++) {
    cout << A[i] << endl;
  }

  return 0;
}
Problem p02262 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
long long cnt;
int l;
int A[1000000];
int n;
vector<int> G;

void insertionSort(int A[], int n, int g) {
  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 A[], int n) {
  for (int h = 1;;) {
    if (h > n)
      break;
    G.push_back(h);
    h = 3 * h + 1;
  }
  for (int i = G.size() - 1; i >= 0; ++i) {
    insertionSort(A, n, G[i]);
  }
}

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &A[i]);
  }
  cnt = 0;
  shellSort(A, n);
  cout << G.size() << endl;
  for (int i = G.size() - 1; i >= 0; i--) {
    printf("%d", G[i]);
    if (i)
      printf(" ");
  }
  printf("\n");
  printf("%d\n", cnt);
  for (int i = 0; i < n; ++i) {
    printf("%d\n", A[i]);
  }

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
long long cnt;
int l;
int A[1000000];
int n;
vector<int> G;

void insertionSort(int A[], int n, int g) {
  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 A[], int n) {
  for (int h = 1;;) {
    if (h > n)
      break;
    G.push_back(h);
    h = 3 * h + 1;
  }
  for (int i = G.size() - 1; i >= 0; i--) {
    insertionSort(A, n, G[i]);
  }
}

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &A[i]);
  }
  cnt = 0;
  shellSort(A, n);
  cout << G.size() << endl;
  for (int i = G.size() - 1; i >= 0; i--) {
    printf("%d", G[i]);
    if (i)
      printf(" ");
  }
  printf("\n");
  printf("%d\n", cnt);
  for (int i = 0; i < n; ++i) {
    printf("%d\n", A[i]);
  }

  return 0;
}
Problem p02262 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;

long long cnt;
int l, n;
int A[1000000];
vector<int> G;

void insertion(int A[], int n, int g) {
  int i, V, j;
  for (i = g; i < n; i++) {
    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 shellsort(int A[], int n) {
  for (int h = 1;;) {
    if (h > n)
      break;
    G.push_back(h);
    h = 3 * h + 1;
  }
  for (int i = G.size() - 1; i >= 0; i--) {
    insertion(A, n, G[i]);
  }
}

int main() {
  int A[100];
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);
  cnt = 0;
  shellsort(A, n);
  cout << G.size() << endl;
  for (int i = G.size() - 1; i >= 0; i--) {
    printf("%d", G[i]);
    if (i)
      printf(" ");
  }
  printf("\n");
  printf("%d\n", cnt);
  for (int i = 0; i < n; i++)
    printf("%d\n", A[i]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;

long long cnt;
int l, n;
int A[1000000];
vector<int> G;

void insertion(int A[], int n, int g) {
  int i, V, j;
  for (i = g; i < n; i++) {
    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 shellsort(int A[], int n) {
  for (int h = 1;;) {
    if (h > n)
      break;
    G.push_back(h);
    h = 3 * h + 1;
  }
  for (int i = G.size() - 1; i >= 0; i--) {
    insertion(A, n, G[i]);
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);
  cnt = 0;
  shellsort(A, n);
  cout << G.size() << endl;
  for (int i = G.size() - 1; i >= 0; i--) {
    printf("%d", G[i]);
    if (i)
      printf(" ");
  }
  printf("\n");
  printf("%d\n", cnt);
  for (int i = 0; i < n; i++)
    printf("%d\n", A[i]);
  return 0;
}
Problem p02262 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>

int cnt;
int m;
int G[100];

void insertionSort(int *A, int n, int g) {
  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 = j - g;
      cnt++;
    }
    A[j + g] = v;
  }
}
void shellSort(int *A, int n) {
  cnt = 0;
  m = 1;
  while (pow(2, m) < n) {
    m++;
    if (m == 100) {
      break;
    }
  }
  G[m - 1] = 1;
  for (int i = m - 2; i >= 0; i--) {
    G[i] = G[i + 1] * 2;
  }
  for (int i = 0; i < m; i++) {
    insertionSort(A, n, G[i]);
  }
}

int main() {
  int n;
  int A[10000];
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }
  shellSort(A, n);
  printf("%d\n", m);
  for (int i = 0; i < m; i++) {
    printf("%d", G[i]);
    if (i + 1 == m) {
      printf("\n");
    } else {
      printf(" ");
    }
  }
  printf("%d\n", cnt);
  for (int i = 0; i < n; i++) {
    printf("%d\n", A[i]);
  }
}

Fixed submission:
#include <cmath>
#include <cstdio>

int cnt;
int m;
int G[100];

void insertionSort(int *A, int n, int g) {
  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 = j - g;
      cnt++;
    }
    A[j + g] = v;
  }
}
void shellSort(int *A, int n) {
  cnt = 0;
  m = 1;
  while (pow(2, m) < n) {
    m++;
    if (m == 100) {
      break;
    }
  }
  G[m - 1] = 1;
  for (int i = m - 2; i >= 0; i--) {
    G[i] = G[i + 1] * 2;
  }
  for (int i = 0; i < m; i++) {
    insertionSort(A, n, G[i]);
  }
}

int main() {
  int n;
  int A[1000000];
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }
  shellSort(A, n);
  printf("%d\n", m);
  for (int i = 0; i < m; i++) {
    printf("%d", G[i]);
    if (i + 1 == m) {
      printf("\n");
    } else {
      printf(" ");
    }
  }
  printf("%d\n", cnt);
  for (int i = 0; i < n; i++) {
    printf("%d\n", A[i]);
  }
}
Problem p02262 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
int cnt = 0;
vector<int> G;

void insertsort(int A[], int n, int g) {
  for (int i = g; i < n; i++) {
    int temp = A[i], j = i - g;
    while (j >= 0 && A[j] > temp) {
      A[j + g] = A[j];
      j -= g;
      cnt++;
    }
    A[j + g] = temp;
  }
  return;
}

void shellsort(int A[], int n) {
  // generate the G array
  for (int h = 1;;) {
    if (h > n)
      break;
    G.push_back(h);
    h = 3 * h + 1;
  }

  for (int i = G.size() - 1; i >= 0; i++)
    insertsort(A, n, G[i]);

  return;
}

int main() {
  int n, A[1000010];
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);

  shellsort(A, n);

  cout << G.size() << endl;
  for (int i = G.size() - 1; i >= 0; i--) {
    printf("%d", G[i]);
    if (i)
      printf(" ");
  }
  printf("\n%d\n", cnt);
  for (int i = 0; i < n; i++)
    printf("%d\n", A[i]);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
int cnt = 0;
vector<int> G;

void insertsort(int A[], int n, int g) {
  for (int i = g; i < n; i++) {
    int temp = A[i], j = i - g;
    while (j >= 0 && A[j] > temp) {
      A[j + g] = A[j];
      j -= g;
      cnt++;
    }
    A[j + g] = temp;
  }
  return;
}

void shellsort(int A[], int n) {
  // generate the G array
  for (int h = 1;;) {
    if (h > n)
      break;
    G.push_back(h);
    h = 3 * h + 1;
  }

  for (int i = G.size() - 1; i >= 0; i--)
    insertsort(A, n, G[i]);

  return;
}

int main() {
  int n, A[1000010];
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);

  shellsort(A, n);

  cout << G.size() << endl;
  for (int i = G.size() - 1; i >= 0; i--) {
    printf("%d", G[i]);
    if (i)
      printf(" ");
  }
  printf("\n%d\n", cnt);
  for (int i = 0; i < n; i++)
    printf("%d\n", A[i]);
  return 0;
}
Problem p02262 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

int count;

void insertionSort(int A[], int N, int G) {
  for (int i = G; i < N; ++i) {
    int v = A[i];
    int j = i - G;
    while (j >= 0 and A[j] > v) {
      A[j + G] = A[j];
      j = j - G;
      count += 1;
    }
    A[j + G] = v;
  }
  return;
}

void shellSort(int A[], int N) {
  count = 0;
  int G[7];
  int m = 0;

  int temp = N;
  for (int i = 0; temp > 0; ++i) {
    G[i] = temp;
    temp /= 2;
    m = i + 1;
  }

  for (int i = 0; i < m; ++i) {
    insertionSort(A, N, G[i]);
  }

  std::cout << m << std::endl;
  for (int i = 0; i < m - 1; ++i) {
    std::cout << G[i] << " ";
  }
  std::cout << G[m - 1] << std::endl;
  std::cout << count << std::endl;
  for (int i = 0; i < N; ++i) {
    std::cout << A[i] << std::endl;
  }
  return;
}

int main() {

  int N;
  int *A;
  A = new int[1040000];
  std::cin >> N;
  for (int i = 0; i < N; ++i) {
    std::cin >> A[i];
  }

  shellSort(A, N);
  delete[] A;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

int count;

void insertionSort(int A[], int N, int G) {
  for (int i = G; i < N; ++i) {
    int v = A[i];
    int j = i - G;
    while (j >= 0 and A[j] > v) {
      A[j + G] = A[j];
      j = j - G;
      count += 1;
    }
    A[j + G] = v;
  }
  return;
}

void shellSort(int A[], int N) {
  count = 0;
  int G[64];
  int m = 0;

  int temp = N;
  for (int i = 0; temp > 0; ++i) {
    G[i] = temp;
    temp /= 2;
    m = i + 1;
  }

  for (int i = 0; i < m; ++i) {
    insertionSort(A, N, G[i]);
  }

  std::cout << m << std::endl;
  for (int i = 0; i < m - 1; ++i) {
    std::cout << G[i] << " ";
  }
  std::cout << G[m - 1] << std::endl;
  std::cout << count << std::endl;
  for (int i = 0; i < N; ++i) {
    std::cout << A[i] << std::endl;
  }
  return;
}

int main() {

  int N;
  int *A;
  A = new int[1040000];
  std::cin >> N;
  for (int i = 0; i < N; ++i) {
    std::cin >> A[i];
  }

  shellSort(A, N);
  delete[] A;

  return 0;
}
Problem p02262 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
#include <vector>
#define Lens 100005
#define Nil -1
using namespace std;

int N, sum;
int a[Lens];
vector<int> b, G;
void selectionsort(int a[], int g) {
  for (int i = g; i < N; i++) {
    int j = i - g;
    int mini = a[i];
    while (j >= 0 && a[j] > mini) {

      a[j + g] = a[j];
      sum++;
      j -= g;
    }
    a[j + g] = mini;
  }
}

void shell() {
  int h = 1;
  while (h <= N) {
    G.push_back(h);
    h = 3 * h + 1;
  }
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  shell();
  sum = 0;
  cout << G.size() << endl;
  for (int i = G.size() - 1; i >= 0; i--) {

    cout << G[i];
    if (i > 0)
      cout << ' ';
    if (i == 0)
      cout << endl;
  }

  for (int i = G.size() - 1; i >= 0; i--) {
    int g = G[i];
    selectionsort(a, g);
  }
  cout << sum << endl;
  for (int i = 0; i < N; i++) {
    cout << a[i] << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
#include <vector>
#define Lens 1000005
#define Nil -1
using namespace std;

int N, sum;
int a[Lens];
vector<int> b, G;
void selectionsort(int a[], int g) {
  for (int i = g; i < N; i++) {
    int j = i - g;
    int mini = a[i];
    while (j >= 0 && a[j] > mini) {

      a[j + g] = a[j];
      sum++;
      j -= g;
    }
    a[j + g] = mini;
  }
}

void shell() {
  int h = 1;
  while (h <= N) {
    G.push_back(h);
    h = 3 * h + 1;
  }
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  shell();
  sum = 0;
  cout << G.size() << endl;
  for (int i = G.size() - 1; i >= 0; i--) {

    cout << G[i];
    if (i > 0)
      cout << ' ';
    if (i == 0)
      cout << endl;
  }

  for (int i = G.size() - 1; i >= 0; i--) {
    int g = G[i];
    selectionsort(a, g);
  }
  cout << sum << endl;
  for (int i = 0; i < N; i++) {
    cout << a[i] << endl;
  }
  return 0;
}
Problem p02262 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <vector>

#define MAX_N 1000000

using namespace std;

long long A[MAX_N];
vector<int> G;
int cnt = 0;

void printNums(int n) {
  for (int i = 0; i < n; i++) {
    printf("%lld\n", A[i]);
  }
}

void getG(int n) {
  int k = 1;
  while (k < n) {
    G.push_back(k);
    k = 3 * k + 1;
  }
}

void insertSort(int n, int interval) {
  int i, j, pivot;
  for (i = interval; i < n; i++) {
    pivot = A[i];
    j = i - interval;
    while ((j >= 0) && (A[j] > pivot)) {
      A[j + interval] = A[j];
      j -= interval;
      cnt++;
    }
    A[j + interval] = pivot;
  }
}

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

  getG(n);
  printf("%d\n", G.size());
  for (i = G.size() - 1; i > 0; i--) {
    printf("%d ", G[i]);
  }
  printf("%d\n", G[0]);

  for (i = G.size() - 1; i >= 0; i--) {
    insertSort(n, G[i]);
  }
  printf("%d\n", cnt);
  printNums(n);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <vector>

#define MAX_N 1000000

using namespace std;

long long A[MAX_N];
vector<int> G;
int cnt = 0;

void printNums(int n) {
  for (int i = 0; i < n; i++) {
    printf("%lld\n", A[i]);
  }
}

void getG(int n) {
  int k = 1;
  while (k <= n) {
    G.push_back(k);
    k = 3 * k + 1;
  }
}

void insertSort(int n, int interval) {
  int i, j, pivot;
  for (i = interval; i < n; i++) {
    pivot = A[i];
    j = i - interval;
    while ((j >= 0) && (A[j] > pivot)) {
      A[j + interval] = A[j];
      j -= interval;
      cnt++;
    }
    A[j + interval] = pivot;
  }
}

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

  getG(n);
  printf("%d\n", G.size());
  for (i = G.size() - 1; i > 0; i--) {
    printf("%d ", G[i]);
  }
  printf("%d\n", G[0]);

  for (i = G.size() - 1; i >= 0; i--) {
    insertSort(n, G[i]);
  }
  printf("%d\n", cnt);
  printNums(n);
}
Problem p02262 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;

void printa(int A[], int len, string separator) {
  cout << A[0];
  for (int i = 1; i < len; i++)
    cout << separator << A[i];
  cout << endl;
}

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

void shellSort(int A[], int len) {
  int cnt = 0;
  vector<int> G;
  for (int h = 1; h < len; h = 3 * h + 1)
    G.insert(G.begin(), h);
  int m = G.size();

  for (int i = 0; i < m; i++)
    cnt += insertionSort(A, len, G[i]);

  cout << m << endl;
  printa(G.data(), m, " ");
  cout << cnt << endl;
  printa(A, len, "\n");
}

int main() {
  int n, *a;
  cin >> n;
  a = new int[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  shellSort(a, n);
  delete[] a;
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

void printa(int A[], int len, string separator) {
  cout << A[0];
  for (int i = 1; i < len; i++)
    cout << separator << A[i];
  cout << endl;
}

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

void shellSort(int A[], int len) {
  int cnt = 0;
  vector<int> G;
  for (int h = 1; h <= len; h = 3 * h + 1)
    G.insert(G.begin(), h);
  int m = G.size();

  for (int i = 0; i < m; i++)
    cnt += insertionSort(A, len, G[i]);

  cout << m << endl;
  printa(G.data(), m, " ");
  cout << cnt << endl;
  printa(A, len, "\n");
}

int main() {
  int n, *a;
  cin >> n;
  a = new int[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  shellSort(a, n);
  delete[] a;
  return 0;
}
Problem p02262 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
using namespace std;
void insertionSort(int *, int, int);
void shellSort(int *, int);
void write(int *, int, int *, int);
int cnt = 0;

int main() {
  int n, A[100010];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  shellSort(A, n);
}

void insertionSort(int *A, int n, int g) {
  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 = j - g;
      cnt++;
    }
    A[j + g] = v;
  }
}

void shellSort(int *A, int n) {
  cnt = 0;
  int m = 0, G[110];
  G[0] = 1;
  int copy[1000010];
  for (int i = 0; i < n; i++) {
    copy[i] = A[i];
  }

  do {
    G[0] = 3 * G[0] + 1;
    m++;
  } while (G[0] <= n / 9);
  G[0] /= 3;
  for (int i = 1; i < m; i++) {
    G[i] = G[i - 1] / 3;
  }
  for (int i = 0; i < m; i++) {
    insertionSort(A, n, G[i]);
  }
  if (cnt < pow(n, 1.5))
    write(G, m, A, n);
  else {
    // cerr << "!!" << endl;
    cnt = 0;
    for (int i = m; i != -1; i--) {
      if (i == 0)
        G[0] = 3 * G[1] + 1;
      else
        G[i] = G[i - 1];
    }
    for (int i = 0; i <= m; i++) {
      insertionSort(copy, n, G[i]);
    }
    write(G, m + 1, copy, n);
  }
}

void write(int *G, int m, int *A, int n) {
  cout << m << endl;
  for (int i = 0; i < m; i++) {
    cout << G[i];
    if (i == m - 1)
      cout << endl;
    else
      cout << " ";
  }
  cout << cnt << endl;
  for (int i = 0; i < n; i++) {
    cout << A[i] << endl;
  }
}

Fixed submission:
#include <cmath>
#include <iostream>
using namespace std;
void insertionSort(int *, int, int);
void shellSort(int *, int);
void write(int *, int, int *, int);
int cnt = 0;

int main() {
  int n, A[1000010];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  shellSort(A, n);
}

void insertionSort(int *A, int n, int g) {
  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 = j - g;
      cnt++;
    }
    A[j + g] = v;
  }
}

void shellSort(int *A, int n) {
  cnt = 0;
  int m = 0, G[110];
  G[0] = 1;
  int copy[1000010];
  for (int i = 0; i < n; i++) {
    copy[i] = A[i];
  }

  do {
    G[0] = 3 * G[0] + 1;
    m++;
  } while (G[0] <= n / 9);
  G[0] /= 3;
  for (int i = 1; i < m; i++) {
    G[i] = G[i - 1] / 3;
  }
  for (int i = 0; i < m; i++) {
    insertionSort(A, n, G[i]);
  }
  if (cnt < pow(n, 1.5))
    write(G, m, A, n);
  else {
    // cerr << "!!" << endl;
    cnt = 0;
    for (int i = m; i != -1; i--) {
      if (i == 0)
        G[0] = 3 * G[1] + 1;
      else
        G[i] = G[i - 1];
    }
    for (int i = 0; i <= m; i++) {
      insertionSort(copy, n, G[i]);
    }
    write(G, m + 1, copy, n);
  }
}

void write(int *G, int m, int *A, int n) {
  cout << m << endl;
  for (int i = 0; i < m; i++) {
    cout << G[i];
    if (i == m - 1)
      cout << endl;
    else
      cout << " ";
  }
  cout << cnt << endl;
  for (int i = 0; i < n; i++) {
    cout << A[i] << endl;
  }
}