alexjercan/bugnet huggingface dataset:
Problem p02262 (C++) — buggy submission, verdict: Runtime Error


#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
*/


Error: 0

Fixed submission:


#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
*/
Problem p02262 (C++) — buggy submission, verdict: 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[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;
  }
}

Error: WA

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[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;
  }
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stdio.h>
#include <utility>
using namespace std;

int shellSort(int a[], int n);
int insertionSort(int a[], int n, int g);

int main(void) {
  int n;
  cin >> n;

  int *a = new int[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];

  shellSort(a, n);

  delete[] a;

  return 0;
}

int shellSort(int a[], int n) {
  int *g = new int[n];
  g[0] = 1;
  int m, i = 1;
  while (1) {
    g[i] = g[i - 1] * 3 + 1;
    if (g[i] > n) {
      m = i;
      break;
    }
    i++;
  }

  cout << m << endl;

  int cnt = 0;
  for (int i = m - 1; i >= 0; i--) {
    cout << g[i];
    if (i > 0)
      cout << " ";
    else
      cout << endl;

    cnt += insertionSort(a, n, g[i]);
  }

  delete[] g;

  cout << cnt << endl;

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

  return cnt;
}

int insertionSort(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 = j - g;
      cnt++;
    }
    a[j + g] = v;
  }
  return cnt;
}

Error: 0

Fixed submission:

#include <iostream>
#include <stdio.h>
#include <utility>
using namespace std;

int shellSort(int a[], int n);
int insertionSort(int a[], int n, int g);

int main(void) {
  int n, a[100];
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  shellSort(a, n);

  return 0;
}

int shellSort(int a[], int n) {
  int *g = new int[n];
  g[0] = 1;
  int m, i = 1;
  while (1) {
    g[i] = g[i - 1] * 3 + 1;
    if (g[i] > n) {
      m = i;
      break;
    }
    i++;
  }

  cout << m << endl;

  int cnt = 0;
  for (int i = m - 1; i >= 0; i--) {
    cout << g[i];
    if (i > 0)
      cout << " ";
    else
      cout << endl;

    cnt += insertionSort(a, n, g[i]);
  }

  delete[] g;

  cout << cnt << endl;

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

  return cnt;
}

int insertionSort(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 = j - g;
      cnt++;
    }
    a[j + g] = v;
  }
  return cnt;
}
Problem p02262 (C++) — buggy submission, verdict: 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 < 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;
}

Error: TLE

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 < 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;
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

long long cnt;
int i;
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;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

long long cnt;
int i;
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;;) {
    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, verdict: 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[1000010];
  read(&n, a);
  shell_sort(a, n);
  cout << cnt << endl;
  print_array(a, n);
  return 0;
}

Error: WA

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[10000];
  read(&n, a);
  shell_sort(a, n);
  cout << cnt << endl;
  print_array(a, n);
  return 0;
}
Problem p02262 (C++) — buggy submission, verdict: 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;
  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;
}

Error: 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;
  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;
}
Problem p02262 (C++) — buggy submission, verdict: 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);
}

Error: 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, verdict: 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 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]);
  }
}


Error: 0

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 (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, verdict: Runtime Error

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <stack>
#include <vector>
#define BIG 1000000000
#define BIGNUM 1000000
#define SIZE 1000
using namespace std;

int cnt = 0;

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

void shellSort(int *A, int n) {
  cnt = 0;
  stack<int> s;
  s.push(1);
  while (3 * s.top() + 1 < n) {
    s.push(3 * s.top() + 1);
  }

  int m = s.size();
  printf("%d\\n", m);

  for (int i = 0; i < m; i++) {
    insertionSort(A, n, s.top());
    if (i > 0)
      printf(" ");
    printf("%d", s.top());
    s.pop();
  }
  printf("\\n%d\\n", cnt);
}

int main() {
  int inputN;
  int A[1000000];

  scanf("%d", &inputN);
  for (int i = 0; i < inputN; i++) {
    scanf("%d", &A[i]);
  }
  shellSort(A, inputN);
  for (int i = 0; i < inputN; i++) {
    if (i > 0)
      printf("\\n");
    printf("%d", A[i]);
  }
  return 0;
}

Error: WA

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <stack>
#include <vector>
#define BIG 10e9
#define SIZE 1000
using namespace std;

int cnt = 0;

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

void shellSort(int *A, int n) {
  cnt = 0;
  stack<int> s;
  s.push(1);
  while (3 * s.top() + 1 < n) {
    s.push(3 * s.top() + 1);
  }

  int m = s.size();
  printf("%d\\n", m);

  for (int i = 0; i < m; i++) {
    insertionSort(A, n, s.top());
    if (i > 0)
      printf(" ");
    printf("%d", s.top());
    s.pop();
  }
  printf("\\n%d\\n", cnt);
}

int main() {
  int inputN;
  int A[SIZE];

  scanf("%d", &inputN);
  for (int i = 0; i < inputN; i++) {
    scanf("%d", &A[i]);
  }
  shellSort(A, inputN);
  for (int i = 0; i < inputN; i++) {
    if (i > 0)
      printf("\\n");
    printf("%d", A[i]);
  }
  return 0;
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iostream>
using namespace std;
#include <vector>

#define ArrayNum 1000000

vector<int> g;

int insertionSort(int a[], int n, int g) {
  int i, j, cnt = 0, v;

  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;
}

void shellSort(int a[], int n) {
  int i, cnt = 0, m;

  for (int h = 1;;) {
    if (h > n)
      break;
    g.push_back(h);
    h = 3 * h + 1;
  }
  m = g.size();
  for (i = m - 1; i >= 0; i--)
    cnt = cnt + insertionSort(a, n, g[i]);

  cout << m << "\\n";
  for (i = m - 1; i > 0; i--)
    cout << g[i] << " ";
  cout << g[0] << "\\n";
  cout << cnt << "\\n";
  for (i = 0; i < n; i++)
    cout << a[i] << "\\n";
}

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


Error: 0

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;
#include <vector>

#define ArrayNum 100

vector<int> g;

int insertionSort(int a[], int n, int g) {
  int i, j, cnt = 0, v;

  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;
}

void shellSort(int a[], int n) {
  int i, cnt = 0, m;

  for (int h = 1;;) {
    if (h > n)
      break;
    g.push_back(h);
    h = 3 * h + 1;
  }
  m = g.size();
  for (i = m - 1; i >= 0; i--)
    cnt = cnt + insertionSort(a, n, g[i]);

  cout << m << "\\n";
  for (i = m - 1; i > 0; i--)
    cout << g[i] << " ";
  cout << g[0] << "\\n";
  cout << cnt << "\\n";
  for (i = 0; i < n; i++)
    cout << a[i] << "\\n";
}

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