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

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

long long ShellSort(int elem[], int num) {
  int i, j, k;
  int h;
  int target;
  long long cnt = 0;
  vector<int> vect;

  for (h = 1;;) {
    if (h <= num) {
      vect.push_back(h);
    } else {
      break;
    }
    h = 3 * h + 1;
  }

  h = vect.size() - 1;
  cout << h + 1 << endl;
  for (i = h; i >= 0; i--) {
    int g = vect[i];
    if (i != h) {
      cout << " ";
    }
    cout << g;
    for (j = g; j < num; j++) {
      target = elem[j];
      for (k = j - g; k >= 0; k -= g) {
        if (elem[k] > target) {
          elem[k + g] = elem[k];
          ++cnt;
        } else {
          break;
        }
      }
      elem[k + g] = target;
    }
  }
  cout << endl;

  return cnt;
}

int main(void) {
  int i;
  long long cnt;
  int num;
  int elem[1000000];

  scanf("%d", &num);
  for (i = 0; i < num; i++) {
    scanf("%d", &elem[i]);
  }

  cnt = ShellSort(elem, num);

  cout << cnt << endl;
  for (i = 0; i < num; i++) {
    cout << elem[i] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

long long ShellSort(int elem[], int num) {
  int i, j, k;
  int h;
  int target;
  long long cnt = 0;
  vector<int> vect;

  for (h = 1;;) {
    if (h <= num) {
      vect.push_back(h);
    } else {
      break;
    }
    h = 3 * h + 1;
  }

  h = vect.size() - 1;
  cout << h + 1 << endl;
  for (i = h; i >= 0; i--) {
    int g = vect[i];
    if (i != h) {
      cout << " ";
    }
    cout << g;
    for (j = g; j < num; j++) {
      target = elem[j];
      for (k = j - g; k >= 0; k -= g) {
        if (elem[k] > target) {
          elem[k + g] = elem[k];
          ++cnt;
        } else {
          break;
        }
      }
      elem[k + g] = target;
    }
  }
  cout << endl;

  return cnt;
}

int main(void) {
  int i;
  long long cnt;
  int num;
  int elem[100000];

  scanf("%d", &num);
  for (i = 0; i < num; i++) {
    scanf("%d", &elem[i]);
  }

  cnt = ShellSort(elem, num);

  cout << cnt << endl;
  for (i = 0; i < num; i++) {
    cout << elem[i] << endl;
  }

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

#include <iostream>
using namespace std;

int x[10000000];
int G[20];
int v;
int w;
int cnt = 0;
int m;
int n;

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

void sort2(int x[], int n) {
  cnt = 0;
  for (int h = 0; h < 20; h++) {
    if (G[h] > n) {
      m = h;
      break;
    }
  }
  cout << m << endl;
  for (int h = m - 1; h >= 0; h--) {
    cout << G[h];
    if (h) {
      cout << ' ';
    }
    sort1(x, n, G[h]);
  }
  cout << endl;
}

int main() {
  G[0] = 1;
  for (int i = 1; i < 20; i++) {
    G[i] = G[i - 1] * 3 + 1;
  }
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x[i];
  }
  sort2(x, n);
  cout << cnt << endl;
  for (int i = 0; i < n; i++) {
    cout << x[i] << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;

int x[10000000];
int G[20];
int v;
int w;
int cnt = 0;
int m;
int n;

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

void sort2(int x[], int n) {
  cnt = 0;
  for (int h = 0; h < 20; h++) {
    if (h > n) {
      m = h;
      break;
    }
  }
  cout << m << endl;
  for (int h = m - 1; h >= 0; h++) {
    cout << G[h];
    if (h) {
      cout << ' ';
    }
    sort1(x, n, G[h]);
  }
  cout << endl;
}

int main() {
  G[0] = 1;
  for (int i = 1; i < 20; i++) {
    G[i] = G[i - 1] * 3 + 1;
  }
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x[i];
  }
  sort2(x, n);
  cout << cnt << endl;
  for (int i = 0; i < n; i++) {
    cout << x[i] << endl;
  }
  return 0;
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
int cnt;
vector<int> a;
void insertionsort(vector<int> &a, int n, int g) {
  int v, i, j, k;
  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<<"insert:"<<&a<<endl;
}
vector<int> shellsort(vector<int> &a, int n) {
  cnt = 0;
  int m, i;
  vector<int> g;
  for (i = 1; i < n + 1; i = i * 3 + 1) {
    g.push_back(i);
    // cout<<g[i]<<endl;
  }
  m = g.size();
  for (int i = m - 1; i >= 0; i--) {
    insertionsort(a, n, g[i]);
  }
  // cout<<"@"<<m<<endl;
  //  cout<<"shell:"<<&a<<endl;
  return g;
}

int main() {
  int m, n, tmp, i;
  vector<int> a;
  vector<int> g;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> tmp;
    a.push_back(tmp);
  }
  // cout<<"main:"<<&a<<endl;
  g = shellsort(a, n);
  m = g.size();
  cout << m << endl;
  if (m > 1) {
    for (i = m - 1; i > 0; i--) {
      cout << g[i] << " ";
    }
  }
  // cout<<"finished:"<<i<<endl;
  cout << g[0] << endl;
  cout << cnt << endl;
  for (i = 0; i < n; i++)
    cout << a[i] << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
int cnt;
vector<int> a;
void insertionsort(vector<int> &a, int n, int g) {
  int v, i, j, k;
  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<<"insert:"<<&a<<endl;
}
vector<int> shellsort(vector<int> &a, int n) {
  cnt = 0;
  int m, i;
  vector<int> g;
  for (i = 1; i < n; i = i * 3 + 1) {
    g.push_back(i);
    // cout<<g[i]<<endl;
  }
  m = g.size() - 1;
  for (int i = m; i >= 0; i--) {
    insertionsort(a, n, g[i]);
  }
  // cout<<"@"<<m<<endl;
  //  cout<<"shell:"<<&a<<endl;
  return g;
}

int main() {
  int m, n, tmp, i;
  vector<int> a;
  vector<int> g;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> tmp;
    a.push_back(tmp);
  }
  // cout<<"main:"<<&a<<endl;
  g = shellsort(a, n);
  m = g.size();
  cout << m << endl;
  for (i = m - 1; i > 0; i--) {
    cout << g[i] << " ";
  }
  cout << g[0] << endl;
  // cout<<"finished"<<endl;
  cout << cnt << endl;
  for (i = 0; i < n; i++)
    cout << a[i] << endl;

  return 0;
}
Problem p02262 (C++) — buggy submission, verdict: Time Limit Exceeded

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

void insertionSort(long int A[], long int n, int g, int &count) {
  int j, v;
  for (int i = g; i < n; i++) {
    j = i - g;
    v = A[i];
    while (1) {
      if (A[j] <= v) {
        for (int k = i; k >= j + g; k -= g) {
          // count++;
          A[k] = A[k - g];
        }
        A[j + g] = v;
        break;
      }
      j -= g;
      count++;
      if (j < 0) {
        for (int k = i; k >= j + g + g; k -= g) {
          A[k] = A[k - g];
        }
        A[j + g] = v;
        break;
      }
    }
  }
}

void shellSort(long int A[], long int n) {
  int count = 0;
  int n_copy;
  int m = 0;
  int G[100];
  n_copy = n;
  G[0] = 1;
  for (int i = 1; i < 100; i++) {
    G[i] = (G[i - 1] * 3) + 1;
    m++;
    if (G[i] > n) {
      break;
    }
  }
  cout << m << endl;
  for (int i = 0; i < m; i++) {
    cout << G[m - 1 - i] << " ";
    insertionSort(A, n_copy, G[m - 1 - i], count);
  }
  cout << endl;
  cout << count << endl;
}

int main() {
  long int n;
  long int A[1000000];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  shellSort(A, n);
  for (int i = 0; i < n; i++) {
    cout << A[i] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

void insertionSort(int A[], int n, int g, int &count) {
  int j, v;
  for (int i = g; i < n; i++) {
    j = i - g;
    v = A[i];
    while (1) {
      if (A[j] < v) {
        for (int k = i; k >= j + g; k -= g) {
          count++;
          A[k] = A[k - g];
        }
        A[j + g] = v;
        break;
      }
      j -= g;
      if (j < 0) {
        for (int k = i; k >= j + g + g; k -= g) {
          count++;
          A[k] = A[k - g];
        }
        A[j + g] = v;
        break;
      }
    }
  }
}

void shellSort(int A[], int n) {
  int count = 0;
  int n_copy;
  int m = 0;
  int G[100];
  int length;
  n_copy = n;
  length = log2(n);
  for (int i = 0; i < 100; i++) {
    G[i] = n / 3 * 2;
    n = n / 3 * 2;
    m++;
    if (n == 1) {
      break;
    }
  }
  cout << m << endl;
  for (int i = 0; i < m; i++) {
    cout << G[i] << " ";
    insertionSort(A, n_copy, G[i], count);
  }
  cout << endl;
  cout << count << endl;
  ;
}

int main() {
  int n;
  int A[1000000];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  shellSort(A, n);
  for (int i = 0; i < n; i++) {
    cout << A[i] << endl;
  }
  return 0;
}
Problem p02262 (C++) — buggy submission, verdict: Time Limit Exceeded

///
// File:  alds1_2_d.cpp
// Author: ymiyamoto
//
// Created on Wed Oct  4 23:56:00 2017
//

#include <cstdint>
#include <iostream>
#include <vector>

using namespace std;

static vector<uint32_t> array;
static vector<uint32_t> G;

uint64_t insersion_sort(int64_t dist) {
  uint64_t count = 0;
  for (uint32_t i = dist; i < array.size(); i++) {
    int32_t val = array[i];
    int32_t j = i - dist;
    while (j >= 0 && array[j] > val) {
      swap(array[j + dist], array[j]);
      count++;
      j -= dist;
    }
    array[j + dist] = val;
  }

  return count;
}

uint64_t shell_sort() {
  uint32_t i = 1;
  while (i <= array.size()) {
    G.push_back(i);
    i = 3 * i + 1;
  }

  uint64_t count = 0;

  for (int32_t j = G.size() - 1; j >= 0; j--) {
    count += insersion_sort(G[j]);
  }

  return count;
}

int32_t main() {
  uint32_t n;
  cin >> n;

  for (uint32_t i = 0; i < n; i++) {
    uint32_t val;
    cin >> val;
    array.push_back(val);
  }

  uint64_t count = shell_sort();
  cout << G.size() << endl;

  for (int32_t i = G.size() - 1; i >= 0; i--) {
    if (i != G.size() - 1)
      cout << " ";
    cout << G[i];
  }

  cout << endl << count << endl;

  for (uint32_t i = 0; i < array.size(); i++) {
    cout << array[i] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

///
// File:  alds1_2_d.cpp
// Author: ymiyamoto
//
// Created on Wed Oct  4 23:56:00 2017
//

#include <cstdint>
#include <iostream>
#include <vector>

using namespace std;

static vector<uint32_t> array;
static vector<uint32_t> G;

uint64_t insersion_sort(int64_t dist) {
  uint64_t count = 0;
  for (uint32_t i = dist; i < array.size(); i++) {
    for (int32_t j = i; j >= dist; j -= dist) {
      if (array[j - dist] > array[j]) {
        swap(array[j - dist], array[j]);
        count++;
      }
    }
  }

  return count;
}

uint64_t shell_sort() {
  uint32_t i = 1;
  while (i <= array.size()) {
    G.push_back(i);
    i = 3 * i + 1;
  }

  uint64_t count = 0;

  for (int32_t j = G.size() - 1; j >= 0; j--) {
    count += insersion_sort(G[j]);
  }

  return count;
}

int32_t main() {
  uint32_t n;
  cin >> n;

  for (uint32_t i = 0; i < n; i++) {
    uint32_t val;
    cin >> val;
    array.push_back(val);
  }

  uint64_t count = shell_sort();
  cout << G.size() << endl;

  for (int32_t i = G.size() - 1; i >= 0; i--) {
    if (i != G.size() - 1)
      cout << " ";
    cout << G[i];
  }

  cout << endl << count << endl;

  for (uint32_t i = 0; i < array.size(); i++) {
    cout << array[i] << endl;
  }

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

Error: WA

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

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

#include <iostream>
using namespace std;

int cnt;

void insertionSort(int *a, int n, int g) {

  int i, j, 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;
  }
}

void shellsort(int *a, int n) {
  cnt = 0;
  int i, p;
  int m;
  int G[15];

  p = 1;
  for (i = 1; i < 20; i++) {
    p = p * 3;
    if (p - 1 > 2 * n)
      break;
  }
  if (i > 1)
    i = i - 1;
  m = i;

  p = 1;
  for (i = 0; i < m; i++) {
    p = p * 3;
    G[m - 1 - i] = (p - 1) / 2;
  }
  cout << m << endl;
  for (i = 0; i < m - 1; i++)
    cout << G[i] << " ";
  cout << G[m - 1] << endl;

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

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

  shellsort(A, n);
  cout << cnt << endl;
  for (i = 0; i < n; i++)
    cout << A[i] << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int cnt;

void insertionSort(int *a, int n, int g) {

  int i, j, 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;
  }
}

void shellsort(int *a, int n) {
  cnt = 0;
  int i, p;
  int m;
  int G[15];

  p = 1;
  for (i = 1; i < 20; i++) {
    p = p * 3;
    if (p - 1 > 2 * n)
      break;
  }
  if (i > 1)
    i = i - 1;
  m = i;

  p = 1;
  for (i = 0; i < m; i++) {
    p = p * 3;
    G[m - 1 - i] = (p - 1) / 2;
  }
  cout << m << endl;
  for (i = 0; i < m - 1; i++)
    cout << G[i] << " ";
  cout << G[m - 1] << endl;

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

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

  // insertionSort(A, n, 1);
  shellsort(A, n);
  cout << cnt << endl;
  for (i = 0; i < n; i++)
    cout << A[i] << endl;

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

#include <iostream>
#include <stdio.h>

using namespace std;

int cnt = 0;
int A[1000000];

void print(int N, int *A) {

  for (int i = 0; i < N; i++) {
    cout << A[i];
    if (i == N - 1) {
      // do nothing
    } else {
      cout << endl;
    }
  }
}

void inserSort(int N, int *A, int B) {
  int v;
  int j;

  for (int i = B; i < N; i++) {
    v = A[i];
    j = i - B;

    while (j >= 0 && A[j] > v) {
      A[j + B] = A[j];
      j = j - B;
      cnt++;
    }

    A[j + B] = v;
  }
}

void shellSort(int N, int *A) {
  int j = 0, i = 0, h = 0, B[10000];

  while (true) {
    B[i] = 3 * h + 1;
    h = B[i];
    if ((3 * h + 1) > N) {
      break;
    }
    i++;
  }
  cout << (i + 1) << endl;
  for (int j = i; j >= 0; j--) {
    printf("%d", B[j]);
    inserSort(N, A, B[j]);
    if (j != 0) {
      cout << " ";
    } else {
      cout << endl;
    }
  }
  printf("%d", cnt);
  cout << endl;
  print(N, A);
}

int main() {
  int N, i = 0;

  cin >> N;

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

  return 0;
}


Error: WA

Fixed submission:

#include <iostream>
#include <stdio.h>

using namespace std;

int cnt = 0;
void print(int N, int *A) {

  for (int i = 0; i < N; i++) {
    cout << A[i];
    if (i == N - 1) {
      // do nothing
    } else {
      cout << endl;
    }
  }
}

void inserSort(int N, int *A, int B) {
  int v;
  int j;

  for (int i = B; i < N; i++) {
    v = A[i];
    j = i - B;

    while (j >= 0 && A[j] > v) {
      A[j + B] = A[j];
      j = j - B;
      cnt++;
    }

    A[j + B] = v;
  }
}

void shellSort(int N, int *A) {
  int j = 0, i = 0, h = 0, B[10000];

  while (true) {
    B[i] = 3 * h + 1;
    h = B[i];
    if ((3 * h + 1) > N) {
      break;
    }
    i++;
  }
  cout << (i + 1) << endl;
  for (int j = i; j >= 0; j--) {
    printf("%d", B[j]);
    inserSort(N, A, B[j]);
    if (j != 0) {
      cout << " ";
    } else {
      cout << endl;
    }
  }
  printf("%d", cnt);
  cout << endl;
  print(N, A);
}

int main() {
  int N, A[10000], i = 0;

  cin >> N;

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

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

#include <iostream>
#include <utility>

using namespace std;

void output(int n, int elements[]) {
  for (int i = 0; i < n; i++)
    cout << elements[i] << '\\n';
}

int insertion_sort(int n, int elements[], int g) {
  int cnt = 0;
  for (int i = g; i < n; i++) {
    int key = elements[i], j = i - g;
    while (0 <= j && key < elements[j]) {
      elements[j + g] = elements[j];
      j -= g;
      cnt++;
    }
    elements[j + g] = key;
  }
  return cnt;
}

void shell_sort(int n, int elements[]) {
  int cnt = 0, m = 0, g[13] = {1,    4,    13,    40,    121,    364,   1093,
                               3280, 9841, 29524, 88573, 265720, 797161};

  for (; g[m] <= n && m < 13; m++)
    ;
  cout << m << '\\n';
  for (int i = m - 1; 0 <= i; i--) {
    cout << g[i] << (i == 0 ? "" : " ");
    cnt += insertion_sort(n, elements, g[i]);
  }
  cout << '\\n';
  cout << cnt << '\\n';
}

int main() {
  std::ios::sync_with_stdio(false);

  int n;
  cin >> n;
  int *elements = new int[n];

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

  shell_sort(n, elements);
  output(n, elements);
  delete[] elements;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <utility>

using namespace std;

void output(int n, int elements[]) {
  for (int i = 0; i < n; i++)
    cout << elements[i] << '\\n';
}

int insertion_sort(int n, int elements[], int g) {
  int cnt = 0;
  for (int i = g; i < n; i++) {
    int key = elements[i], j = i - g;
    while (0 <= j && key < elements[j]) {
      elements[j + g] = elements[j];
      j -= g;
      cnt++;
    }
    elements[j + g] = key;
  }
  return cnt;
}

void shell_sort(int n, int elements[]) {
  int cnt = 0, m = 0, g[13] = {1,    4,    13,    40,    121,    364,   1093,
                               3280, 9841, 29524, 88573, 265720, 797161};

  for (; g[m] <= n; m++)
    ;
  cout << m << '\\n';
  for (int i = m - 1; 0 <= i; i--) {
    cout << g[i] << (i == 0 ? "" : " ");
    cnt += insertion_sort(n, elements, g[i]);
  }
  cout << '\\n';
  cout << cnt << '\\n';
}

int main() {
  std::ios::sync_with_stdio(false);

  int n;
  cin >> n;
  int *elements = new int[n];

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

  shell_sort(n, elements);
  output(n, elements);
  delete[] elements;
  return 0;
}