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

#include "iostream"

using namespace std;

void sonyu(int num, int g, int *cnt, int a[1000000]) {

  for (int i = 1; i < num; i++) {

    int v = a[i];
    int j = i - g;

    while (j >= 0 && a[j] > v) {
      a[j + g] = a[j];
      j -= g;
      *cnt += 1;
    }
    a[j + g] = v;
  }
}

int main() {

  int a[1000000];
  int cnum;
  int num;
  int cnt = 0;

  cin >> num;

  for (int i = 0; i < num; i++) {
    cin >> a[i];
  }

  int kaisu = 1;
  int v[100];
  v[0] = 1;
  for (int i = 1;; i++) {
    v[i] = v[i - 1] * 3 + 1;
    if (v[i] > num)
      break;
    kaisu++;
  }

  cout << kaisu << endl;

  for (int i = kaisu - 1; i >= 0; i--) {
    sonyu(num, v[i], &cnt, a);
    cout << v[i];
    if (i != 0)
      cout << " ";
  }
  cout << endl;

  cout << cnt << endl;

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

  int p = 0;
  return 0;
}

Error: 0

Fixed submission:

#include "iostream"

using namespace std;

void sonyu(int num, int g, int *cnt, int a[100000]) {

  for (int i = 1; i < num; i++) {

    int v = a[i];
    int j = i - g;

    while (j >= 0 && a[j] > v) {
      a[j + g] = a[j];
      j -= g;
      *cnt += 1;
    }
    a[j + g] = v;
  }
}

int main() {

  int a[100000];
  int cnum;
  int num;
  int cnt = 0;

  cin >> num;

  for (int i = 0; i < num; i++) {
    cin >> a[i];
  }

  int kaisu = 1;
  int v[100];
  v[0] = 1;
  for (int i = 1;; i++) {
    v[i] = v[i - 1] * 3 + 1;
    if (v[i] > num)
      break;
    kaisu++;
  }

  cout << kaisu << endl;

  for (int i = kaisu - 1; i >= 0; i--) {
    sonyu(num, v[i], &cnt, a);
    cout << v[i];
    if (i != 0)
      cout << " ";
  }
  cout << endl;

  cout << cnt << endl;

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

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

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

#include <bits/stdc++.h>
using namespace std;

int cnt = 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) {
  vector<int> G;
  int h = 1;
  while (h <= n) {
    G.push_back(h);
    h = 3 * h + 1;
  }

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

  // print info
  cout << G.size() << endl;
  if (G.size() > 0) {
    for (int i = G.size() - 1; i > 0; i--)
      cout << G[i] << " ";
    cout << G[0] << endl;
  }
}

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

  shellSort(a, n);

  cout << cnt << endl;

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

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int cnt = 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) {
  vector<int> G;
  int h = 1;
  while (h < n) {
    G.push_back(h);
    h = 3 * h + 1;
  }

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

  // print info
  cout << G.size() << endl;
  for (int i = G.size() - 1; i > 0; i--)
    cout << G[i] << " ";
  cout << G[0] << endl;
}

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

  shellSort(a, n);

  cout << cnt << endl;

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

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

#include <cmath>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_m 100
using namespace std;

int main() {
  int A[1000001]; // 問題文の要求通りの要素数に設定するとオーバーフローする
  int num;        // 入力数

  // 配列を0で初期化
  for (int i = 0; i < 1000001; i++) {
    A[i] = 0;
  }

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

  void shellSort(int A[], int N);
  shellSort(A, num);

  return 0;
}

// 0?g-1までの余りで分類したg個の数列を別々にソートする
/*----通常のinsertionSortの1がgになっただけ--------*/
int insertionSort(int A[], int N, int g) {
  int tmp;
  int j;
  int cnt = 0; // 要素の移動回数
  for (int i = g; i < N; i++) {
    tmp = A[i];
    j = i - g;
    while (j >= 0 && A[j] > tmp) {
      A[j + g] = A[j];
      j = j - g;
      cnt++;
    }
    A[j + g] = tmp;
  }
  return cnt;
}
/*-----------------------------------------------*/

void shellSort(int A[], int N) {
  int cnt = 0;
  int int_LogN = (int)(log((double)N) / log(2.0));
  int m = (int)pow((double)2, (double)int_LogN);
  int tmp_m;
  int G[MAX_m + 1];
  int ttlcnt = 0; // total count

  cout << int_LogN + 1 << endl;

  tmp_m = m;
  // まとめる
  for (int i = 0; i < int_LogN + 1; i++) { // int_logN==2→2^2,2^1,2^0と変化
    G[i] = tmp_m;
    tmp_m = tmp_m / 2;
    cout << G[i] << " ";
  }
  cout << endl;

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

  cout << ttlcnt << endl;

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

Error: WA

Fixed submission:

#include <cmath>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_m 100
using namespace std;

int main() {
  int A[10001]; // 問題文の要求通りの要素数に設定するとオーバーフローする
  int num;      // 入力数

  // 配列を0で初期化
  for (int i = 0; i < 10001; i++) {
    A[i] = 0;
  }

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

  void shellSort(int A[], int N);
  shellSort(A, num);

  return 0;
}

// 0?g-1までの余りで分類したg個の数列を別々にソートする
/*----通常のinsertionSortの1がgになっただけ--------*/
int insertionSort(int A[], int N, int g) {
  int tmp;
  int j;
  int cnt = 0; // 要素の移動回数
  for (int i = g; i < N; i++) {
    tmp = A[i];
    j = i - g;
    while (j >= 0 && A[j] > tmp) {
      A[j + g] = A[j];
      j = j - g;
      cnt++;
    }
    A[j + g] = tmp;
  }
  return cnt;
}
/*-----------------------------------------------*/

void shellSort(int A[], int N) {
  int cnt = 0;
  int int_LogN = (int)(log((double)N) / log(2.0));
  int m = (int)pow((double)2, (double)int_LogN);
  int tmp_m;
  int G[MAX_m + 1];
  int ttlcnt = 0; // total count

  cout << int_LogN + 1 << endl;

  tmp_m = m;
  // まとめる
  for (int i = 0; i < int_LogN + 1; i++) { // int_logN==2→2^2,2^1,2^0と変化
    G[i] = tmp_m;
    tmp_m = tmp_m / 2;
    cout << G[i] << " ";
  }
  cout << endl;

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

  cout << ttlcnt << endl;

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

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

int m = 0, cnt = 0, G[100];

void shellSort(int a[], int n) {
  int i, j;
  cnt = 0;
  G[0] = 1;
  while (1) {
    G[m + 1] = 3 * G[m] + 1;
    if (G[m + 1] >= n)
      break;
    else
      m++;
  }
  m++;
  for (i = m - 1; i >= 0; i--) {
    int k, j, tmp;
    for (k = G[i]; k < n; k++) {
      tmp = a[k];
      j = k - G[i];
      while (j >= 0 && a[j] > tmp) {
        a[j + G[i]] = a[j];
        j -= G[i];
        cnt++;
      }
      a[j + G[i]] = tmp;
    }
  }
}

int main() {
  int i, j, n, a[1000000];
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  shellSort(a, n);
  printf("%d\\n", m);
  for (i = m - 1; i >= 0; i--) {
    printf("%d", G[i]);
    if (i != 0)
      printf(" ");
    else
      printf("\\n");
  }

  printf("%d\\n", cnt);

  for (i = 0; i < n; i++) {
    printf("%d\\n", a[i]);
  }

  return 0;
}

Error: 0

Fixed submission:

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

int m = 0, cnt = 0, G[100];

void shellSort(int a[], int n) {
  int i, j;
  cnt = 0;
  G[0] = 1;
  while (1) {
    G[m + 1] = 3 * G[m] + 1;
    if (G[m + 1] >= n)
      break;
    else
      m++;
  }
  m++;
  for (i = m - 1; i >= 0; i--) {
    int k, j, tmp;
    for (k = G[i]; k < n; k++) {
      tmp = a[k];
      j = k - G[i];
      while (j >= 0 && a[j] > tmp) {
        a[j + G[i]] = a[j];
        j -= G[i];
        cnt++;
      }
      a[j + G[i]] = tmp;
    }
  }
}

int main() {
  int i, j, n, a[100000];
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  shellSort(a, n);
  printf("%d\\n", m);
  for (i = m - 1; i >= 0; i--) {
    printf("%d", G[i]);
    if (i != 0)
      printf(" ");
    else
      printf("\\n");
  }

  printf("%d\\n", cnt);

  for (i = 0; i < n; i++) {
    printf("%d\\n", a[i]);
  }

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

#include <iostream>
#include <vector>

using namespace std;

long count = 0;
vector<int> G;
int exInsertionSort(int A[], int N, int g) {
  if (g < 1)
    return 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;
      count++;
    }

    A[j + g] = v;
  }
  return 1;
}

void shellSort(int A[], int N) {
  for (int h = 1; h <= N; h = 3 * h + 1) {
    G.push_back(h);
  }

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

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

  cin >> N;

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

  shellSort(A, N);

  cout << G.size() << endl;

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

  cout << endl;

  cout << count << endl;

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

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int count = 0;
vector<int> G;
int exInsertionSort(int A[], int N, int g) {
  if (g < 1)
    return 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;
      count++;
    }

    A[j + g] = v;
  }
  return 1;
}

void shellSort(int A[], int N) {
  for (int h = 1; h <= N; h = 3 * h + 1) {
    G.push_back(h);
  }

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

int main() {
  int N;
  int A[100000];

  cin >> N;

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

  shellSort(A, N);

  cout << G.size() << endl;

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

  cout << endl;

  cout << count << endl;

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

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

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

int main() {
  int n, cnt;
  vector<int> nums;
  auto insertionSort = [&nums, &n, &cnt](int d) {
    for (int i = d; i < n; ++i) {
      int top = nums[i];
      int j;
      for (j = i - d; j >= 0 && nums[j] > top; j -= d) {
        nums[j + d] = nums[j];
        ++cnt;
      }
      nums[j + d] = top;
    }
  };
  auto shellSort = [&nums, &n, insertionSort]() {
    int maxid = log(2 * n + 1) / log(3) - 1;
    cout << maxid + 1 << endl;
    for (int id = maxid; id >= 0; --id) {
      unsigned int d = (pow(3, id + 1) - 1) / 2;
      cout << d << (id == 0 ? "\\n" : " ");
      insertionSort(d);
    }
  };
  cin >> n;
  nums = vector<int>(n);
  for (int &c : nums)
    cin >> c;
  cnt = 0;
  shellSort();
  cout << cnt << endl;
  for (auto &&c : nums)
    cout << c << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int n, cnt;
  vector<int> nums;
  auto insertionSort = [&nums, &n, &cnt](int d) {
    for (int i = d; i < n; ++i) {
      int top = nums[i];
      int j;
      for (j = i - d; i >= 0 && nums[j] > top; j -= d) {
        nums[j + d] = nums[j];
        ++cnt;
      }
      nums[j + d] = top;
    }
  };
  auto shellSort = [&nums, &n, insertionSort]() {
    int maxid = log(2 * n + 1) / log(3) - 1;
    cout << maxid + 1 << endl;
    for (int id = maxid; id >= 0; --id) {
      unsigned int d = (pow(3, id + 1) - 1) / 2;
      cout << d << (id == 0 ? "\\n" : " ");
      insertionSort(d);
    }
  };
  cin >> n;
  nums = vector<int>(n);
  for (int &c : nums)
    cin >> c;
  cnt = 0;
  shellSort();
  cout << cnt << endl;
  for (auto &&c : nums)
    cout << c << endl;
  return 0;
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

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

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

void shell_sort(int A[], int N) {
  int cnt = 0;
  int m = 1;
  // h_i+1 = 3 * h_i + 1
  int G[13] = {1,    4,    13,    40,    121,    364,   1093,
               3280, 9841, 29524, 88573, 265720, 797161};

  bool first = false;
  for (int i = 12; i >= 0; i--) {
    if (G[i] < N) {
      if (!first) {
        first = true;
        m = i + 1;
      }
      insertion_sort(A, N, G[i], &cnt);
    }
  }
  cout << m << endl;
  for (int i = m - 1; i >= 0; i--) {
    cout << G[i] << (i == 0 ? "" : " ");
  }
  cout << endl;

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

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int N;
  int A[1000000];

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

Error: 0

Fixed submission:

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

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

void shell_sort(int A[], int N) {
  int cnt = 0;
  int m = 1;
  // h_i+1 = 3 * h_i + 1
  int G[13] = {1,    4,    13,    40,    121,    364,   1093,
               3280, 9841, 29524, 88573, 265720, 797161};

  bool first = false;
  for (int i = 12; i >= 0; i--) {
    if (G[i] < N) {
      if (!first) {
        first = true;
        m = i + 1;
      }
      insertion_sort(A, N, G[i], &cnt);
    }
  }
  cout << m << endl;
  for (int i = m - 1; i >= 0; i--) {
    cout << G[i] << (i == 0 ? "" : " ");
  }
  cout << endl;

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

int main(void) {
  int N;
  int A[100000];

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  shell_sort(A, N);
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

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


Error: 0

Fixed submission:

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

Error: WA

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


Error: -11

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