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

#include <cmath>
#include <iostream>

using namespace std;

static const int m_MAX = 100;
static const int n_MAX = 1000000;

int insertionSort(int A[], int n, int g) {
  int i, j, v, 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;
}

void shellSort(int A[], int n) {
  int i, j, m, G[m_MAX], cnt = 0;

  G[0] = 1;
  for (i = 1; G[i - 1] * 3 + 1 < n; i++)
    G[i] = G[i - 1] * 3 + 1;
  m = i;

  cout << m << endl;

  cout << G[m - 1];
  for (i = m - 2; i >= 0; i--)
    cout << ' ' << G[i];
  cout << endl;

  for (i = m - 1; i >= 0; i--)
    cnt += insertionSort(A, n, G[i]);

  cout << cnt << endl;

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

int main() {
  int n, i, A[n_MAX];

  cin >> n;
  for (i = 0; i < n; i++)
    cin >> A[i];

  shellSort(A, n);

  return 0;
}

Error: 0

Fixed submission:

#include <cmath>
#include <iostream>

using namespace std;

static const int m_MAX = 100;
static const int n_MAX = 1000000;

int insertionSort(int A[], int n, int g) {
  int i, j, v, 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;
    }
  }

  // cout << "------------------" << endl;
  // printf("g = %d\\n", g);

  // for (i = 0; i < n; i++)
  // 	cout << A[i] << ' ';
  // cout << endl;

  return cnt;
}

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

  for (i = 0; i * 3 + 1 < n; i++)
    G[i] = i * 3 + 1;

  if (i == 0) {
    m = 1;
    G[0] = 1;
  } else
    m = i;

  cout << m << endl;

  // debug
  cout << G[m - 1];
  for (i = m - 2; i >= 0; i--)
    cout << ' ' << G[i];
  cout << endl;

  for (i = m - 1; i >= 0; i--)
    cnt += insertionSort(A, n, G[i]);

  cout << cnt << endl;

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

int main() {
  int n, i, A[n_MAX];

  cin >> n;
  for (i = 0; i < n; i++)
    cin >> A[i];

  //	cout << "--------------" << endl;
  shellSort(A, n);

  return 0;
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

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

#define MAX 100
long long cnt = 0;
int l;
int n;
int a[1000000];
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++) {
    cin >> 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: 0

Fixed submission:

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

#define MAX 100
long long cnt = 0;
int l;
int n;
int a[1000000];
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 = 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() {

  int n, a[MAX];

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> 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: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n, m, tmp, cnt = 0;
  vector<int> a, spread;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    a.push_back(tmp);
  }
  for (int i = 0, tmp = 1; tmp <= n; i++) { // make spread vector
    spread.insert(spread.begin(), tmp);
    tmp = 3 * tmp + 1;
  }
  m = spread.size();
  cout << m << endl; // output spread amount
  for (int i = 0; i < m - 1; i++)
    cout << spread[i] << " ";
  cout << spread[m - 1] << endl; // output all spread
  for (int k = 0; k < m; k++)
    for (int i = 0; i + spread[k] < n; i++) {
      int end = i + spread[k], first = i % spread[k]; // list first
      // for(int j=1;j<amount;j++){ //inserting sort
      if (a[end] > a[end - spread[k]])
        continue;
      int l;
      for (l = 1; l < (end - first) / spread[k] + 1; l++) {
        if (a[end - (l - 1) * spread[k]] < a[end - l * spread[k]]) {
          swap(a[end - (l - 1) * spread[k]], a[end - l * spread[k]]);
          cnt++;
        } else
          break;
      }
      // cout<<"check vector cnt:"<<cnt<<endl; //debug
      // for(int i=0;i<n-1;i++) cout<<a[i]<<" ";
      // cout<<a[n-1]<<endl<<endl;
    }
  cout << cnt << endl; // output cnt&answer vector
  for (int i = 0; i < n; i++)
    cout << a[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n, m, tmp, cnt = 0;
  vector<int> a, spread;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    a.push_back(tmp);
  }
  for (int i = 0, tmp = 1; tmp <= n; i++) { // make spread vector
    spread.insert(spread.begin(), tmp);
    tmp = 3 * tmp + 1;
  }
  m = spread.size();
  cout << m << endl; // output spread amount
  for (int i = 0; i < m - 1; i++)
    cout << spread[i] << " ";
  cout << spread[m - 1] << endl; // output all spread
  for (int k = 0; k < m; k++)
    for (int i = 0; i + spread[k] < n; i++) {
      int end = i + spread[k], first = i % spread[k]; // list first
      int amount = (end - first) / spread[k] + 1;     // list amount
      for (int j = 1; j < amount; j++) {              // inserting sort
        if (a[first + j * spread[k]] > a[first + (j - 1) * spread[k]])
          continue;
        int l;
        for (l = j - 1; l != -1; l--)
          if (a[first + j * spread[k]] >= a[first + l * spread[k]])
            break;
        l++;
        for (int p = first + j * spread[k]; p > first + l * spread[k];
             p -= spread[k]) {
          swap(a[p], a[p - spread[k]]);
          cnt++;
        }
      }
      // cout<<"check vector cnt:"<<cnt<<endl; //debug
      // for(int i=0;i<n-1;i++) cout<<a[i]<<" ";
      // cout<<a[n-1]<<endl<<endl;
    }
  cout << cnt << endl; // output cnt&answer vector
  for (int i = 0; i < n; i++)
    cout << a[i] << endl;
  return 0;
}
Problem p02262 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n, m, tmp, cnt = 0;
  vector<int> a, spread;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    a.push_back(tmp);
  }
  for (int i = 0, tmp = 1; tmp <= n; i++) { // make spread vector
    spread.insert(spread.begin(), tmp);
    tmp = 3 * tmp + 1;
  }
  m = spread.size();
  cout << m << endl; // output spread amount
  for (int i = 0; i < m - 1; i++)
    cout << spread[i] << " ";
  cout << spread[m - 1] << endl; // output all spread
  for (int k = 0; k < m; k++)
    for (int i = 0; i + spread[k] < n; i++) {
      int end = i + spread[k], first = i % spread[k]; // list first
      // for(int j=1;j<amount;j++){ //inserting sort
      if (a[end] > a[end - spread[k]])
        continue;
      int l;
      for (l = 1; l < (end - first) / spread[k] + 1; l++) {
        if (a[end - (l - 1) * spread[k]] < a[end - l * spread[k]]) {
          swap(a[end - (l - 1) * spread[k]], a[end - l * spread[k]]);
          cnt++;
        } else
          break;
      }
      // cout<<"check vector cnt:"<<cnt<<endl; //debug
      // for(int i=0;i<n-1;i++) cout<<a[i]<<" ";
      // cout<<a[n-1]<<endl<<endl;
    }
  cout << cnt << endl; // output cnt&answer vector
  for (int i = 0; i < n; i++)
    cout << a[i] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n, m, tmp, cnt = 0;
  vector<int> a, spread;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    a.push_back(tmp);
  }
  for (int i = 0, tmp = 1; tmp <= n; i++) { // make spread vector
    spread.insert(spread.begin(), tmp);
    tmp = 3 * tmp + 1;
  }
  m = spread.size();
  cout << m << endl; // output spread amount
  for (int i = 0; i < m - 1; i++)
    cout << spread[i] << " ";
  cout << spread[m - 1] << endl; // output all spread
  for (int k = 0; k < m; k++)
    for (int i = 0; i + spread[k] < n; i++) {
      int end = i + spread[k], first = i % spread[k]; // list first
      int amount = (end - first) / spread[k] + 1;     // list amount
      for (int j = 1; j < amount; j++) {              // inserting sort
        if (a[first + j * spread[k]] > a[first + (j - 1) * spread[k]])
          continue;
        int l;
        for (l = j - 1; l != -1; l--)
          if (a[first + j * spread[k]] >= a[first + l * spread[k]])
            break;
        l++;
        for (int p = first + j * spread[k]; p > first + l * spread[k];
             p -= spread[k]) {
          swap(a[p], a[p - spread[k]]);
          cnt++;
        }
      }
      // cout<<"check vector cnt:"<<cnt<<endl; //debug
      // for(int i=0;i<n-1;i++) cout<<a[i]<<" ";
      // cout<<a[n-1]<<endl<<endl;
    }
  cout << cnt << endl; // output cnt&answer vector
  for (int i = 0; i < n; i++)
    cout << a[i] << endl;
  return 0;
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int N;
int A[1000000];

int cnt;
int m;
vector<int> G;

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

void solve() {
  cnt = m = 0;
  for (int i = max(N / 2, 1); i > 0; i /= 2) {
    m++;
    isort(i);
    G.push_back(i);
  }
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  solve();

  cout << m << endl;
  cout << G[0];
  for (int i = 1; i < (int)G.size(); i++) {
    cout << " " << G[i];
  }
  cout << endl;
  cout << cnt << endl;
  for (int i = 0; i < N; i++) {
    cout << A[i] << endl;
  }
}

Error: WA

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int N;
int A[1000000];

int cnt;
int m;
vector<int> G;

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

void solve() {
  cnt = m = 0;
  for (int i = N / 2; i > 0; i /= 2) {
    m++;
    isort(i);
    G.push_back(i);
  }
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  solve();

  cout << m << endl;
  cout << G[0];
  for (int i = 1; i < (int)G.size(); i++) {
    cout << " " << G[i];
  }
  cout << endl;
  cout << cnt << endl;
  for (int i = 0; i < N; i++) {
    cout << A[i] << endl;
  }
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error


#include <stdio.h>
#include <vector>
using namespace std;

vector<int> G;
int cnt = 0;
void insertionSort(int *p, int n, int g) {
  for (int i = g; i < n; i++) {
    int v = p[i];
    int j = i - g;
    while (j >= 0 && p[j] > v) {
      p[j + g] = p[j];
      j -= g;
      cnt = cnt + 1;
    }
    p[j + g] = v;
  }
}

void shellSort(int *p, 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(p, n, G[i]);
  }
}

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

  shellSort(seq, n);
  printf("%d \\n", G.size());
  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", seq[i]);

  return 0;
}

Error: -11

Fixed submission:

#include <stdio.h>
#include <vector>
using namespace std;

vector<int> G;
int cnt = 0;
void insertionSort(int *p, int n, int g) {
  for (int i = g; i < n; i++) {
    int v = p[i];
    int j = i - g;
    while (j >= 0 && p[j] > v) {
      p[j + g] = p[j];
      j -= g;
      cnt = cnt + 1;
    }
    p[j + g] = v;
  }
}

void shellSort(int *p, 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(p, n, G[i]);
  }
}

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

  shellSort(seq, n);
  printf("%d \\n", G.size());
  for (int i = G.size(); i >= 0; i++) {
    printf("%d", G[i]);
    if (i)
      printf(" ");
  }
  printf("%d\\n", cnt);
  for (int i = 0; i < n; i++)
    printf("%d\\n", seq[i]);

  return 0;
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define outl(x) cout << (x) << '\\n'
using namespace std;
using pii = pair<int, int>;
using ll = long long;
vector<int> G;

ll insertion(int a[], int n, int g) {
  ll cnt = 0;
  for (int i = g; i < n; ++i) {
    int j = i - g;
    int tmp = a[i];

    while (j >= 0 && a[j] > tmp) {
      a[j + g] = a[j];
      j -= g;
      ++cnt;
    }
    a[j + g] = tmp;
  }
  return (cnt);
}

ll shell(int a[], int n) {
  int h = 1;
  ll cnt = 0;
  while (h <= n) {
    G.push_back(h);
    h = 3 * h + 1;
  }

  for (int i = G.size() - 1; i >= 0; --i) {
    cnt += insertion(a, n, G[i]);
  }

  return cnt;
}

signed main() {
  cin.tie(0), ios::sync_with_stdio(false);

  int n;
  int a[1000123];

  cin >> n;
  rep(i, n) cin >> a[i];

  ll cnt = shell(a, n);

  outl(G.size());
  for (int i = G.size() - 1; i >= 0; --i) {
    cout << G[i] << (i - 1 >= 0 ? ' ' : '\\n');
  }
  outl(cnt);

  rep(i, n) { outl(a[i]); }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define outl(x) cout << (x) << '\\n'
using namespace std;
using pii = pair<int, int>;
vector<int> G;

int insertion(int a[], int n, int g) {
  int cnt = 0;
  for (int i = g; i < n; ++i) {
    int j = i - g;
    int tmp = a[i];

    while (j >= 0 && a[j] > tmp) {
      a[j + g] = a[j];
      j -= g;
      ++cnt;
    }
    a[j + g] = tmp;
  }
  return (cnt);
}

int shell(int a[], int n) {
  int h = 1;
  int cnt = 0;
  while (h <= n) {
    G.push_back(h);
    h = 3 * h + 1;
  }

  for (int i = G.size() - 1; i >= 0; --i) {
    cnt += insertion(a, n, G[i]);
  }

  return cnt;
}

signed main() {
  cin.tie(0), ios::sync_with_stdio(false);

  int n;
  int a[102];

  cin >> n;
  rep(i, n) cin >> a[i];

  int cnt = shell(a, n);

  outl(G.size());
  for (int i = G.size() - 1; i >= 0; --i) {
    cout << G[i] << (i - 1 >= 0 ? ' ' : '\\n');
  }
  outl(cnt);

  rep(i, n) { outl(a[i]); }
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define MAX 1000010

using namespace std;

int n, A[MAX], cnt;

void insertionSort(int g) {
  REP(i, g, n) {
    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() {
  cnt = 0;
  /*
  int m = rand() % min(n,100) + 1;
  int *G = new int[m];
  G[0] = 1;
  set<int> used;
  used.insert(1);
  REP(i,1,m){
    int v = rand() % n + 1;
    while( used.find(v) != used.end() ) v = rand() % n + 1;
    used.insert(v);
    G[i] = v;
  }
  sort(G,G+m);
  */
  int m = 1;
  vector<int> G;
  G.push_back(1);
  while (G.back() * 3 + 1 <= n && m < 100) {
    m++;
    G.push_back(G.back() * 3 + 1);
  }
  reverse(G.begin(), G.end());
  rep(i, m) insertionSort(G[i]);
  cout << m << endl;
  rep(i, m) {
    if (i)
      cout << ' ';
    cout << G[i];
  }
  cout << endl;
  cout << cnt << endl;
  rep(i, n) cout << A[i] << endl;
  // delete [] G;
}

int main() {
  srand((unsigned int)time(NULL));
  cin >> n;
  rep(i, n) cin >> A[i];
  shellSort();
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define MAX 1000010

using namespace std;

int n, A[MAX], cnt;

void insertionSort(int g) {
  REP(i, g, n) {
    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() {
  cnt = 0;
  /*
  int m = rand() % min(n,100) + 1;
  int *G = new int[m];
  G[0] = 1;
  set<int> used;
  used.insert(1);
  REP(i,1,m){
    int v = rand() % n + 1;
    while( used.find(v) != used.end() ) v = rand() % n + 1;
    used.insert(v);
    G[i] = v;
  }
  sort(G,G+m);
  */
  int m = 0;
  vector<int> G;
  G.push_back(1);
  while (G.back() <= n && m < 100) {
    m++;
    G.push_back(G.back() * 3 + 1);
  }
  rep(i, m) insertionSort(G[i]);
  assert(cnt <= ceil(pow(n, 1.5)));
  cout << m << endl;
  rep(i, m) {
    if (i)
      cout << ' ';
    cout << G[i];
  }
  cout << endl;
  cout << cnt << endl;
  rep(i, n) cout << A[i] << endl;
  // delete [] G;
}

int main() {
  srand((unsigned int)time(NULL));
  cin >> n;
  rep(i, n) cin >> A[i];
  shellSort();
  return 0;
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
#define maxn 1000005
int a[maxn];
int n; // size of the array
vector<int> G;
int cnt = 0;
void insertionSort(int g) {
  for (int i = g; i < n; i++) {
    int key = a[i];
    int j = i - g;
    while (j >= 0 && a[j] > key) {
      a[j + g] = a[j];
      j = j - g;
      cnt++;
    }
    a[j + g] = key;
  }
}
void makeShellArray() {
  int num = 1;
  for (num;;) {
    if (num > n)
      break;
    G.push_back(num);
    num = 3 * num + 1;
  }
}
void shellSort() {
  int m = G.size();
  for (int i = m - 1; i >= 0; i--)
    insertionSort(G[i]);
}
void print() {
  int m = G.size();
  cout << m << endl;
  for (int i = m - 1; i >= 0; i--) {
    if (i != 0)
      cout << G[i] << " ";
    else
      cout << G[i] << endl;
  }
  cout << cnt << endl;
  for (int i = 0; i < n; i++)
    cout << a[i] << endl;
}
int main() {
  while (cin >> n) {
    for (int i = 0; i < n; i++)
      cin >> a[i];
    makeShellArray();
    shellSort();
    print();
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
#define maxn 100000
int a[maxn];
int n; // size of the array
vector<int> G;
int cnt = 0;
void insertionSort(int g) {
  for (int i = g; i < n; i++) {
    int key = a[i];
    int j = i - g;
    while (j >= 0 && a[j] > key) {
      a[j + g] = a[j];
      j = j - g;
      cnt++;
    }
    a[j + g] = key;
  }
}
void makeShellArray() {
  int num = 1;
  for (num;;) {
    if (num > n)
      break;
    G.push_back(num);
    num = 3 * num + 1;
  }
}
void shellSort() {
  int m = G.size();
  for (int i = m - 1; i >= 0; i--)
    insertionSort(G[i]);
}
void print() {
  int m = G.size();
  cout << m << endl;
  for (int i = m - 1; i >= 0; i--) {
    if (i != 0)
      cout << G[i] << " ";
    else
      cout << G[i] << endl;
  }
  cout << cnt << endl;
  for (int i = 0; i < n; i++)
    cout << a[i] << endl;
}
int main() {
  while (cin >> n) {
    for (int i = 0; i < n; i++)
      cin >> a[i];
    makeShellArray();
    shellSort();
    print();
  }
  return 0;
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

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

void shellSort(int x[], int N);
void insertionSort(int x[], int N, int g);
void trace(int x[], int N);

long long gCount = 0;

int main() {
  int N;

  cin >> N;

  int x[N];

  for (int i = 0; i < N; i++) {
    cin >> x[i];
  }
  shellSort(x, N);
  cout << gCount << endl;
  trace(x, N);

  return 0;
}

void shellSort(int x[], int N) {
  int num = 0, i, h = 1;
  vector<int> g;

  i = 0;
  do {
    g.push_back(h);
    h = h * 3 + 1;
    i++;
  } while (h < N);
  cout << g.size() << endl;
  for (i = g.size() - 1; i >= 0; i--) {
    cout << g[i] << " ";
    insertionSort(x, N, g[i]);
  }
  cout << endl;
}

void insertionSort(int x[], int N, int g) {
  int tmp, i, j;

  for (i = g; i < N; i++) {
    tmp = x[i];
    j = i - g;
    while (j >= 0 && x[j] > tmp) {
      x[j + g] = x[j];
      j = j - g;
      gCount++;
    }
    x[j + g] = tmp;
  }
  // trace(x, N);
}

void trace(int x[], int N) {
  for (int i = 0; i < N; i++) {
    cout << x[i] << endl;
  }
}

Error: WA

Fixed submission:

#include <iostream>
using namespace std;

void shellSort(int x[], int N);
void insertionSort(int x[], int N, int g);
void trace(int x[], int N);

long long gCount = 0;

int main() {
  int N;

  cin >> N;

  int x[N];

  for (int i = 0; i < N; i++) {
    cin >> x[i];
  }
  shellSort(x, N);
  cout << gCount << endl;
  trace(x, N);

  return 0;
}

void shellSort(int x[], int N) {
  int num = (N - 1) / 3 + 1, i;
  int g[num];

  cout << num << endl;
  for (i = 0; i < num - 1; i++) {
    g[i] = 3 * i + 1;
    cout << g[i] << " ";
  }
  g[i] = 3 * i + 1;
  cout << g[i] << endl;
  for (i = 0; i < 3; i++) {
    insertionSort(x, N, g[i]);
  }
}

void insertionSort(int x[], int N, int g) {
  int tmp, i, j;

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

void trace(int x[], int N) {
  for (int i = 0; i < N; i++) {
    cout << x[i] << endl;
  }
}