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

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

using namespace std;

long long cnt;
int i, A[10000], 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; h <= n; h = 3 * h + 1)
    G.push_back(h);
  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--)
    cout << G[i] << ' ';
  cout << G[0] << endl;
  cout << cnt << endl;
  for (int i = 0; i < n; i++)
    cout << A[i] << endl;
  return 0;
}


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

using namespace std;

long long cnt;
int i, A[1000000], 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; h <= n; h = 3 * h + 1)
    G.push_back(h);
  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--)
    cout << G[i] << ' ';
  cout << G[0] << endl;
  cout << cnt << endl;
  for (int i = 0; i < n; i++)
    cout << A[i] << endl;
  return 0;
}
Problem p02262 (C++)

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

int main() {
  long n = 0, g = 0, v = 0, cnt = 0, m = 0;

  cin >> n;

  vector<long> a(n);

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

  cnt = 0;
  if (n == 1)
    m = 1;
  else
    m = (int)(log(n) / log(3));

  vector<long> G(m);

  for (long k = 0; k < m; k++) {
    G[k] = ((long)pow(3, m - k) - 1) / 2;
    g = G[k];

    for (long i = g; i < n; i++) {

      long j = 0;

      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 << m << endl;

  for (long i = 0; i < m - 1; i++)
    cout << G[i] << " ";
  cout << G[m - 1] << endl;

  cout << cnt << endl;

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

  return 0;
}

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

int main() {
  long n = 0, g = 0, v = 0, cnt = 0, m = 0;

  cin >> n;

  vector<long> a(n);

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

  cnt = 0;
  if (n < 3)
    m = 1;
  else
    m = (int)(log(n) / log(3));

  vector<long> G(m);

  for (long k = 0; k < m; k++) {
    G[k] = ((long)pow(3, m - k) - 1) / 2;
    g = G[k];

    for (long i = g; i < n; i++) {

      long j = 0;

      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 << m << endl;

  for (long i = 0; i < m - 1; i++)
    cout << G[i] << " ";
  cout << G[m - 1] << endl;

  cout << cnt << endl;

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

  return 0;
}
Problem p02262 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
using namespace std;
long d = 0, z = 0, b[1000000];
void ChentrucTiep(long long a[], long n, long h) {
  int x, k;
  for (int i = h; i < n; i++) {
    x = a[i];
    k = i - h;
    while (k >= 0 && a[k] > x) {
      a[k + h] = a[k];
      k -= h;
      d++;
    }
    a[k + h] = x;
  }
}
void shellSort(long long a[], long n) {

  for (int i = 1; i <= n;) {
    b[z] = i;
    z += 1;
    i = i * 3 + 1;
  }
  for (int x = z - 1; x >= 0; x--)
    ChentrucTiep(a, n, b[x]);
}

int main() {
  long n;
  long long a[100000];
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  shellSort(a, n);
  cout << z << endl;
  for (int i = z - 1; i >= 0; i--) {
    cout << b[i];
    if (i)
      cout << " ";
  }
  cout << endl << d << endl;
  for (int i = 0; i < n; i++) {
    cout << a[i] << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
using namespace std;
long d = 0, z = 0, b[1000000];
void ChentrucTiep(long long a[], long n, long h) {
  int x, k;
  for (int i = h; i < n; i++) {
    x = a[i];
    k = i - h;
    while (k >= 0 && a[k] > x) {
      a[k + h] = a[k];
      k -= h;
      d++;
    }
    a[k + h] = x;
  }
}
void shellSort(long long a[], long n) {

  for (int i = 1; i <= n;) {
    b[z] = i;
    z += 1;
    i = i * 3 + 1;
  }
  for (int x = z - 1; x >= 0; x--)
    ChentrucTiep(a, n, b[x]);
}

int main() {
  long n;
  long long a[1000000];
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  shellSort(a, n);
  cout << z << endl;
  for (int i = z - 1; i >= 0; i--) {
    cout << b[i];
    if (i)
      cout << " ";
  }
  cout << endl << d << endl;
  for (int i = 0; i < n; i++) {
    cout << a[i] << endl;
  }

  return 0;
}
Problem p02262 (C++)

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

static long long cnt;
static int l;
static int A[100000];
static int n;

vector<int> G;

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

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

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

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

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

  return 0;
}


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

long long cnt;
int l;
int A[100000000];
int n;

vector<int> G;

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

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

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

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

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

  return 0;
}
Problem p02262 (C++)

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

using namespace std;

int insertion_sort(int *seq, int length, int g) {
  int temp, count = 0;
  int v, j;
  for (int i = g; i < length; i++) {
    v = seq[i];
    j = i - g;
    while (j >= 0 && seq[j] > v) {
      seq[j + g] = seq[j];
      j = j - g;
      count++;
    }
    seq[j + g] = v;
  }
  return count;
}

int shellSort(int *A, int n) {
  int cnt = 0, m;
  int i = 1;
  int G[1000000];

  for (int i = 1, j = 0; i < n; j++, i *= 2) {
    G[j] = i;
    m = j;
  }

  cout << m + 1 << endl;
  for (int i = m; i >= 0; i--) {
    cnt += insertion_sort(A, n, G[i]);
    cout << G[i] << " ";
  }
  cout << endl;
  return cnt;
}

int main() {
  int N, count;
  int seq[100];
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> seq[i];

  count = shellSort(seq, N);
  cout << count << endl;
  for (int k = 0; k < N; k++)
    cout << seq[k] << endl;
}


Fixed submission:
#include <iostream>

using namespace std;

int insertion_sort(int *seq, int length, int g) {
  int temp, count = 0;
  int v, j;
  for (int i = g; i < length; i++) {
    v = seq[i];
    j = i - g;
    while (j >= 0 && seq[j] > v) {
      seq[j + g] = seq[j];
      j = j - g;
      count++;
    }
    seq[j + g] = v;
  }
  return count;
}

int shellSort(int *A, int n) {
  int cnt = 0, m;
  int i = 1;
  int G[1000000];

  for (int i = 1, j = 0; i < n; j++, i *= 2) {
    G[j] = i;
    m = j;
  }

  cout << m + 1 << endl;
  for (int i = m; i >= 0; i--) {
    cnt += insertion_sort(A, n, G[i]);
    cout << G[i] << " ";
  }
  cout << endl;
  return cnt;
}

int main() {
  int N, count;
  int seq[1000000];
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> seq[i];

  count = shellSort(seq, N);
  cout << count << endl;
  for (int k = 0; k < N; k++)
    cout << seq[k] << endl;
}
Problem p02262 (C++)

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

using namespace std;

#define BUF_SIZE 1024

int count = 0;

void insertionSort(int array[], int n, int g) {
  int v, j;
  for (int i = g; i < n; i++) {
    v = array[i];
    j = i - g;
    while (j >= 0 && array[j] > v) {
      array[j + g] = array[j]; //??°??????????????§???????????????
      j = j - g;
      count++;
    }
    array[j + g] = v;
  }
}

void shellSort(int array[], int n) {
  int m = 0;
  int s = 1;

  while ((n / s) > 0) {
    s *= 2;
    m++; // 2^(mult-1)?????§?????????????????¨???????¨?
  }

  int G[20];

  for (int k = 0, t = 1; k < m; k++) {
    G[k] = n / t;
    t *= 2;
  }

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

  printf("%d\n", m);
  for (int i = 0; i < m; i++) {
    if (i != m - 1) {
      printf("%d ", G[i]);
    } else {
      printf("%d\n", G[i]);
    }
  }
  printf("%d\n", count);
  for (int i = 0; i < n; i++) {
    printf("%d\n", array[i]);
  }
}

int main(int argc, char *argv[]) {
  char buf[BUF_SIZE];
  int n = atoi(fgets(buf, BUF_SIZE, stdin));
  int array[1000];

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

  shellSort(array, n);
}

Fixed submission:
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <string.h>

using namespace std;

#define BUF_SIZE 1024

int count = 0;

void insertionSort(int array[], int n, int g) {
  int v, j;
  for (int i = g; i < n; i++) {
    v = array[i];
    j = i - g;
    while (j >= 0 && array[j] > v) {
      array[j + g] = array[j]; //??°??????????????§???????????????
      j = j - g;
      count++;
    }
    array[j + g] = v;
  }
}

void shellSort(int array[], int n) {
  int m = 0;
  int s = 1;

  while ((n / s) > 0) {
    s *= 2;
    m++; // 2^(mult-1)?????§?????????????????¨???????¨?
  }

  int G[20];

  for (int k = 0, t = 1; k < m; k++) {
    G[k] = n / t;
    t *= 2;
  }

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

  printf("%d\n", m);
  for (int i = 0; i < m; i++) {
    if (i != m - 1) {
      printf("%d ", G[i]);
    } else {
      printf("%d\n", G[i]);
    }
  }
  printf("%d\n", count);
  for (int i = 0; i < n; i++) {
    printf("%d\n", array[i]);
  }
}

int main(int argc, char *argv[]) {
  char buf[BUF_SIZE];
  int n = atoi(fgets(buf, BUF_SIZE, stdin));
  int array[1000000];

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

  shellSort(array, n);
}
Problem p02262 (C++)

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

using namespace std;
long long cnt = 0;
void insertionSort(int numbers[], int n, int g) {
  for (int i = g; i < n; i++) {
    int v = numbers[i];
    int j = i - g;
    while (j >= 0 && numbers[j] > v) {
      numbers[j + g] = numbers[j];
      j = j - g;
      cnt++;
    }
    numbers[j + g] = v;
  }
}
void shellSort(int numbers[], int n) {
  // int m=2;
  // 生成数列G int G[2]={4,1}
  vector<int> G;
  for (int i = 1; i <= n;) {
    G.push_back(i);
    i = i * 3 + 1;
  }
  for (int i = G.size() - 1; i >= 0; i--) {

    insertionSort(numbers, n, G[i]);
  }
  cout << G.size() << endl;
  for (int i = G.size() - 1; i > 0; i--) {
    cout << G[i] << " ";
  }
  cout << G[0];
  cout << endl;
}
int main() {
  int n;
  cin >> n;
  int numbers[100];

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

  shellSort(numbers, n);
  cout << cnt << endl;
  for (int i = 0; i < n; i++) {
    cout << numbers[i] << endl;
  }
}


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

using namespace std;
long long cnt = 0;
void insertionSort(int numbers[], int n, int g) {
  for (int i = g; i < n; i++) {
    int v = numbers[i];
    int j = i - g;
    while (j >= 0 && numbers[j] > v) {
      numbers[j + g] = numbers[j];
      j = j - g;
      cnt++;
    }
    numbers[j + g] = v;
  }
}
void shellSort(int numbers[], int n) {
  // int m=2;
  // 生成数列G int G[2]={4,1}
  vector<int> G;
  for (int i = 1; i <= n;) {
    G.push_back(i);
    i = i * 3 + 1;
  }
  for (int i = G.size() - 1; i >= 0; i--) {

    insertionSort(numbers, n, G[i]);
  }
  cout << G.size() << endl;
  for (int i = G.size() - 1; i > 0; i--) {
    cout << G[i] << " ";
  }
  cout << G[0];
  cout << endl;
}
int main() {
  int n;
  cin >> n;
  int numbers[1000000];

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

  shellSort(numbers, n);
  cout << cnt << endl;
  for (int i = 0; i < n; i++) {
    cout << numbers[i] << endl;
  }
}
Problem p02262 (C++)

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

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

// 間隔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) {
  // 数列 G = { 1, 4, 13, 40, 121, 364, 1093, ... } を生成
  for (int h = 1;;) {
    G.push_back(h);
    h = 3 * h + 1;
  }

  for (int i = G.size() - 1; i >= 0; i--) { // 逆順に G[i] = g を指定
    insertionSort(A, n, G[i]);
  }
}

int main() {
  cin >> n;
  // より速い入力scanf関数を使用
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);
  cnt = 0;

  shellSort(A, n);

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

  return 0;
}

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

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

// 間隔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) {
  // 数列 G = { 1, 4, 13, 40, 121, 364, 1093, ... } を生成
  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--) { // 逆順に G[i] = g を指定
    insertionSort(A, n, G[i]);
  }
}

int main() {
  cin >> n;
  // より速い入力scanf関数を使用
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);
  cnt = 0;

  shellSort(A, n);

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

  return 0;
}
Problem p02262 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
long long cnt;
int G[100];
int m;

void insertionSort(int A[], int n, int g) {
  int v, i, j;

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

void print(int m, int G[], int A[], int n) {
  printf("%d", m);
  printf("\n");
  for (int i = 0; i < m; i++) {
    if (i > 0)
      printf(" ");
    printf("%d", G[i]);
  }
  printf("\n");
  printf("%d", cnt);
  printf("\n");
  for (int i = 0; i < n; i++) {
    printf("%d", A[i]);
    printf("\n");
  }
}

void shellSort(int A[], int n) {
  int i;
  int g = 0;
  m = -1;

  while (m < 100 && g <= n) {
    g = 3 * g + 1;
    m++;
  }
  g = 0;
  for (i = m - 1; i >= 0; i--) {
    g = 3 * g + 1;
    G[i] = g;
  }

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

int main() {
  int A[100000], n;
  cnt = 0;

  scanf("%d", &n);

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

  shellSort(A, n);

  print(m, G, A, n);

  return 0;
}


Fixed submission:
#include <stdio.h>
long long cnt;
int G[100];
int m;

void insertionSort(int A[], int n, int g) {
  int v, i, j;

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

void print(int m, int G[], int A[], int n) {
  printf("%d", m);
  printf("\n");
  for (int i = 0; i < m; i++) {
    if (i > 0)
      printf(" ");
    printf("%d", G[i]);
  }
  printf("\n");
  printf("%d", cnt);
  printf("\n");
  for (int i = 0; i < n; i++) {
    printf("%d", A[i]);
    printf("\n");
  }
}

void shellSort(int A[], int n) {
  int i;
  int g = 0;
  m = -1;

  while (m < 100 && g <= n) {
    g = 3 * g + 1;
    m++;
  }
  g = 0;
  for (i = m - 1; i >= 0; i--) {
    g = 3 * g + 1;
    G[i] = g;
  }

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

int main() {
  int A[1000000], n;
  cnt = 0;

  scanf("%d", &n);

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

  shellSort(A, n);

  print(m, G, A, n);

  return 0;
}
Problem p02262 (C++)

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

template <typename T> void insertion_sort(vector<T> &A, int g);

template <typename T> void shell_sort(vector<T> &A);

int cnt = 0;

int main() {
  int n = 0;
  cin >> n;
  vector<int> A(n);
  for (auto &ai : A)
    cin >> ai;
  shell_sort(A);
  return 0;
}

template <typename T> void insertion_sort(vector<T> &A, int g) {
  for (int i = g; i != A.size(); ++i) {
    auto v = A[i];
    auto j = i - g;
    while (j >= 0 and A[j] > v) {
      A[j + g] = A[j];
      j -= g;
      ++cnt;
    }
    A[j + g] = v;
  }
}

template <typename T> void shell_sort(vector<T> &A) {
  cnt = 0;
  vector<int> G;
  for (int gi = 1; gi < A.size(); gi = 3 * gi + 1)
    G.push_back(gi);
  reverse(G.begin(), G.end());
  for (auto gi : G)
    insertion_sort(A, gi);

  cout << G.size() << endl;
  cout << G[0];
  for (auto it = G.begin() + 1; it != G.end(); ++it)
    cout << ' ' << *it;
  cout << endl;
  cout << cnt << endl;
  for (auto ai : A)
    cout << ai << endl;
}

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

template <typename T> void insertion_sort(vector<T> &A, int g);

template <typename T> void shell_sort(vector<T> &A);

int cnt = 0;

int main() {
  int n = 0;
  cin >> n;
  vector<int> A(n);
  for (auto &ai : A)
    cin >> ai;
  shell_sort(A);
  return 0;
}

template <typename T> void insertion_sort(vector<T> &A, int g) {
  for (int i = g; i != A.size(); ++i) {
    auto v = A[i];
    auto j = i - g;
    while (j >= 0 and A[j] > v) {
      A[j + g] = A[j];
      j -= g;
      ++cnt;
    }
    A[j + g] = v;
  }
}

template <typename T> void shell_sort(vector<T> &A) {
  cnt = 0;
  vector<int> G;
  for (int gi = 1; gi <= A.size(); gi = 3 * gi + 1)
    G.push_back(gi);
  reverse(G.begin(), G.end());
  for (auto gi : G)
    insertion_sort(A, gi);

  cout << G.size() << endl;
  cout << G[0];
  for (auto it = G.begin() + 1; it != G.end(); ++it)
    cout << ' ' << *it;
  cout << endl;
  cout << cnt << endl;
  for (auto ai : A)
    cout << ai << endl;
}
Problem p02262 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;

long InsertionSort(vector<int> &A, int n, int g) {
  long 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;
}

void ShellSort(vector<int> &A, int n) {
  vector<int> G; // = {1, 4, 13, 40, 121...}
  // g_n+1 = 3g_n +
  // 1?????°????????¨????????¨O(N^1.25)??¨???????????¨????????¶????????£????????¬???????????????
  for (int i = 1; i <= n; i = 3 * i + 1) {
    G.push_back(i);
  }
  cout << G.size() << endl; // output m
  //?????????????????????????????\?????????
  long cnt = 0;
  for (auto itr = G.crbegin(); itr != G.crend(); ++itr) {
    if (itr != G.crbegin())
      cout << " "; // output[13 4 1]
    cout << *itr;
    cnt += InsertionSort(A, n, *itr);
  }
  cout << endl;
  cout << cnt << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n; // input
  vector<int> A(n);
  for (auto &a : A)
    cin >> a; // input
  ShellSort(A, n);
  for (auto &a : A)
    cout << a << endl; // output
  system("pause");
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;

long InsertionSort(vector<int> &A, int n, int g) {
  long 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;
}

void ShellSort(vector<int> &A, int n) {
  vector<int> G; // = {1, 4, 13, 40, 121...}
  // g_n+1 = 3g_n +
  // 1?????°????????¨????????¨O(N^1.25)??¨???????????¨????????¶????????£????????¬???????????????
  for (int i = 1; i <= n; i = 3 * i + 1) {
    G.push_back(i);
  }
  cout << G.size() << endl; // output m
  //?????????????????????????????\?????????
  long cnt = 0;
  for (auto itr = G.crbegin(); itr != G.crend(); ++itr) {
    if (itr != G.crbegin())
      cout << " "; // output[13 4 1]
    cout << *itr;
    cnt += InsertionSort(A, n, *itr);
  }
  cout << endl;
  cout << cnt << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n; // input
  vector<int> A(n);
  for (auto &a : A)
    cin >> a; // input
  ShellSort(A, n);
  for (auto &a : A)
    cout << a << endl; // output
  // system("pause");
}
Problem p02262 (C++)

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

using namespace std;

template <typename T> void cout_seq(T seq[], int n) {
  for (int i = 0; i < n; i++) {
    if (i) {
      cout << " ";
    }
    cout << seq[i];
  }
  cout << endl;
}

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 = j - g;
      cnt++;
    }
    A[j + g] = v;
  }
  return cnt;
}

int shell_sort(int A[], int n) {
  int cnt = 0;
  int G[n];
  int v = n - 1;
  int m = 0;
  for (int i = 0;; i++) {
    G[i] = v;
    if (v == 1) {
      m = i + 1;
      break;
    }
    v = v / 2;
  }
  cout << m << endl;
  cout_seq(G, m);
  for (int i = 0; i < m; i++) {
    cnt += insertion_sort(A, n, G[i]);
  }
  return cnt;
}

int main() {
  int n;
  cin >> n;
  int seq[n];
  for (int i = 0; i < n; i++)
    cin >> seq[i];
  int count = 0;
  count = shell_sort(seq, n);
  cout << count << endl;
  for (auto a : seq) {
    cout << a << endl;
  }
}

Fixed submission:
#include <iostream>

using namespace std;

template <typename T> void cout_seq(T seq[], int n) {
  for (int i = 0; i < n; i++) {
    if (i) {
      cout << " ";
    }
    cout << seq[i];
  }
  cout << endl;
}

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 = j - g;
      cnt++;
    }
    A[j + g] = v;
  }
  return cnt;
}

int shell_sort(int A[], int n) {
  int cnt = 0;
  int G[n];
  int v = (n > 1) ? n - 1 : 1;
  int m = 0;
  for (int i = 0;; i++) {
    G[i] = v;
    if (v == 1) {
      m = i + 1;
      break;
    }
    v = v / 2;
  }
  cout << m << endl;
  cout_seq(G, m);
  for (int i = 0; i < m; i++) {
    cnt += insertion_sort(A, n, G[i]);
  }
  return cnt;
}

int main() {
  int n;
  cin >> n;
  int seq[n];
  for (int i = 0; i < n; i++)
    cin >> seq[i];
  int count = 0;
  count = shell_sort(seq, n);
  cout << count << endl;
  for (auto a : seq) {
    cout << a << endl;
  }
}
Problem p02262 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli>> mat;
typedef vector<vector<bool>> matb;
typedef vector<string> vst;
typedef pair<lli, lli> pll;
typedef pair<double, double> pdd;

lli n;
vll a;
vll g;
lli ans = 0;

int main() {
  cin >> n;
  a = vll(n);
  for (lli i = 0; i < n; i++)
    cin >> a[i];
  for (lli i = 1; i < n; i = 3 * i + 1) {
    g.push_back(i);
  }
  for (lli k = g.size() - 1; k >= 0; k--) {
    for (lli i = 0; i < n; i++) {
      for (lli j = i - g[k]; j >= 0 && a[j] > a[j + g[k]]; j -= g[k]) {
        swap(a[j], a[j + g[k]]);
        ans++;
      }
    }
  }
  cout << g.size() << endl;
  cout << g[g.size() - 1];
  for (lli i = g.size() - 2; i >= 0; i--) {
    cout << " " << g[i];
  }
  cout << endl;
  cout << ans << endl;
  for (lli i = 0; i < n; i++)
    cout << a[i] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli>> mat;
typedef vector<vector<bool>> matb;
typedef vector<string> vst;
typedef pair<lli, lli> pll;
typedef pair<double, double> pdd;

lli n;
vll a;
vll g;
lli ans = 0;

int main() {
  cin >> n;
  a = vll(n);
  for (lli i = 0; i < n; i++)
    cin >> a[i];
  for (lli i = 1; i <= n; i = 3 * i + 1) {
    g.push_back(i);
  }
  for (lli k = g.size() - 1; k >= 0; k--) {
    for (lli i = 0; i < n; i++) {
      for (lli j = i - g[k]; j >= 0 && a[j] > a[j + g[k]]; j -= g[k]) {
        swap(a[j], a[j + g[k]]);
        ans++;
      }
    }
  }
  cout << g.size() << endl;
  cout << g[g.size() - 1];
  for (lli i = g.size() - 2; i >= 0; i--) {
    cout << " " << g[i];
  }
  cout << endl;
  cout << ans << endl;
  for (lli i = 0; i < n; i++)
    cout << a[i] << endl;
  return 0;
}
Problem p02262 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
int n, a[1111111], c, g[20];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  g[0] = 1;
  c = 0;
  while (true) {
    g[c + 1] = g[c] * 3 + 1;
    if (g[++c] >= n)
      break;
  }
  int ret = 0;
  for (int i = c - 1; i >= 0; i--) {
    for (int j = g[i]; j < n; j--) {
      int v = a[j], w = j - g[i];
      while (w >= 0 && a[w] > v) {
        a[w + g[i]] = a[w];
        w -= g[i];
        ret++;
      }
      a[w + g[i]] = v;
    }
  }
  cout << c << endl;
  for (int i = c - 1; i >= 0; i--) {
    if (i)
      cout << g[i] << ' ';
    else
      cout << g[i] << endl;
  }
  cout << ret << endl;
  for (int i = 0; i < n; i++) {
    cout << a[i] << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int n, a[1111111], c, g[20];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  g[0] = 1;
  c = 0;
  while (true) {
    g[c + 1] = g[c] * 3 + 1;
    if (g[++c] >= n)
      break;
  }
  int ret = 0;
  for (int i = c - 1; i >= 0; i--) {
    for (int j = g[i]; j < n; j++) {
      int v = a[j], w = j - g[i];
      while (w >= 0 && a[w] > v) {
        a[w + g[i]] = a[w];
        w -= g[i];
        ret++;
      }
      a[w + g[i]] = v;
    }
  }
  cout << c << endl;
  for (int i = c - 1; i >= 0; i--) {
    if (i)
      cout << g[i] << ' ';
    else
      cout << g[i] << endl;
  }
  cout << ret << endl;
  for (int i = 0; i < n; i++) {
    cout << a[i] << endl;
  }
  return 0;
}
Problem p02262 (C++)

Buggy submission (status: 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[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;
}

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[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;
}
Problem p02262 (C++)

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

unsigned insertion_sort(unsigned a[], unsigned n, unsigned gap = 1) {
  unsigned count = 0;
  for (unsigned i = gap; i < n; i++) {
    unsigned v = a[i];
    int j = i - gap;
    while (j >= 0 && a[j] > v) {
      a[j + gap] = a[j];
      j -= gap;
      count++;
    }
    a[j + gap] = v;
  }
  return count;
}

std::vector<unsigned> make_gap_table(unsigned n) {
  std::vector<unsigned> gaps;
  unsigned h = 1;
  unsigned i = 1;
  while (h <= n) {
    gaps.push_back(h);
    h = pow(4, i) + 3 * pow(2, i - 1) + 1;
    i++;
  }
  return gaps;
}

void shell_sort(unsigned a[], unsigned n) {
  auto gaps = make_gap_table(n);
  unsigned count = 0;
  for (int i = gaps.size() - 1; i >= 0; i--) {
    std::clog << gaps[i] << '\n';
    count += insertion_sort(a, n, gaps[i]);
  }

  std::cout << gaps.size() << '\n';

  for (int i = gaps.size() - 1; i; i--) {
    std::cout << gaps[i] << ' ';
  }
  std::cout << gaps[0] << '\n';

  std::cout << count << '\n';
}

int main() {
  std::ios::sync_with_stdio(false);
  unsigned n, buf;
  std::cin >> n;
  std::vector<unsigned> a(n);
  for (unsigned i = 0; i < n; i++) {
    std::cin >> buf;
    a[i] = buf;
  }
  shell_sort(a.data(), a.size());
  for (auto &v : a) {
    std::cout << v << '\n';
  }
}

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

unsigned insertion_sort(unsigned a[], unsigned n, unsigned gap = 1) {
  unsigned count = 0;
  for (unsigned i = gap; i < n; i++) {
    unsigned v = a[i];
    int j = i - gap;
    while (j >= 0 && a[j] > v) {
      a[j + gap] = a[j];
      j -= gap;
      count++;
    }
    a[j + gap] = v;
  }
  return count;
}

std::vector<unsigned> make_gap_table(unsigned n) {
  std::vector<unsigned> gaps;
  unsigned h = 1;
  unsigned i = 1;
  while (h <= n) {
    gaps.push_back(h);
    h = pow(4, i) + 3 * pow(2, i - 1) + 1;
    i++;
  }
  return gaps;
}

void shell_sort(unsigned a[], unsigned n) {
  auto gaps = make_gap_table(n);
  unsigned count = 0;
  for (int i = gaps.size() - 1; i >= 0; i--) {
    count += insertion_sort(a, n, gaps[i]);
  }

  std::cout << gaps.size() << '\n';

  for (int i = gaps.size() - 1; i; i--) {
    std::cout << gaps[i] << ' ';
  }
  std::cout << gaps[0] << '\n';

  std::cout << count << '\n';
}

int main() {
  std::ios::sync_with_stdio(false);
  unsigned n, buf;
  std::cin >> n;
  std::vector<unsigned> a(n);
  for (unsigned i = 0; i < n; i++) {
    std::cin >> buf;
    a[i] = buf;
  }
  shell_sort(a.data(), a.size());
  for (auto &v : a) {
    std::cout << v << '\n';
  }
}
Problem p02262 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

unsigned gaps[] = {1, 8, 23, 77, 281, 1073, 4193, 16577, 65921, 262913};
// generated from pow(4,i)+3*pow(2,i-1)+1 (prefixed with 1)

unsigned insertion_sort(unsigned a[], unsigned n, unsigned gap) {
  unsigned count = 0;
  for (unsigned i = gap; i < n; i++) {
    unsigned v = a[i];
    int j;
    while ((j = i - gap) >= 0 && a[j] > v) {
      a[j + gap] = a[j];
      j -= gap;
      count++;
    }
    a[j + gap] = v;
  }
  return count;
}

void shell_sort(unsigned a[], unsigned n) {
  unsigned gap_num = 0;
  while (gaps[++gap_num] < n)
    ;
  gap_num--;

  unsigned count = 0;
  for (int i = gap_num; i >= 0; i--)
    count += insertion_sort(a, n, gaps[i]);

  std::cout << (gap_num + 1) << '\n';

  for (int i = gap_num; i; i--)
    std::cout << gaps[i] << ' ';
  std::cout << gaps[0] << '\n';

  std::cout << count << '\n';
}

int main() {
  std::ios::sync_with_stdio(false);
  unsigned n;
  std::cin >> n;
  auto a = new unsigned[n];
  for (unsigned i = 0; i < n; i++)
    std::cin >> a[i];
  shell_sort(a, n);
  for (unsigned i = 0; i < n; i++)
    std::cout << a[i] << '\n';
  delete[] (a);
}

Fixed submission:
#include <iostream>

unsigned gaps[] = {1, 8, 23, 77, 281, 1073, 4193, 16577, 65921, 262913};
// generated from pow(4,i)+3*pow(2,i-1)+1 (prefixed with 1)

unsigned insertion_sort(unsigned a[], unsigned n, unsigned gap) {
  unsigned count = 0;
  for (unsigned i = gap; i < n; i++) {
    unsigned v = a[i];
    int j = i - gap;
    while (j >= 0 && a[j] > v) {
      a[j + gap] = a[j];
      j -= gap;
      count++;
    }
    a[j + gap] = v;
  }
  return count;
}

void shell_sort(unsigned a[], unsigned n) {
  unsigned gap_num = 0;
  while (gaps[++gap_num] < n)
    ;
  gap_num--;

  unsigned count = 0;
  for (int i = gap_num; i >= 0; i--)
    count += insertion_sort(a, n, gaps[i]);

  std::cout << (gap_num + 1) << '\n';

  for (int i = gap_num; i; i--)
    std::cout << gaps[i] << ' ';
  std::cout << gaps[0] << '\n';

  std::cout << count << '\n';
}

int main() {
  std::ios::sync_with_stdio(false);
  unsigned n;
  std::cin >> n;
  auto a = new unsigned[n];
  for (unsigned i = 0; i < n; i++)
    std::cin >> a[i];
  shell_sort(a, n);
  for (unsigned i = 0; i < n; i++)
    std::cout << a[i] << '\n';
  delete[] (a);
}
Problem p02262 (C++)

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

unsigned gaps[] = {1,    8,     23,    77,     281,    1073,
                   4193, 16577, 65921, 262913, 1050113};
// generated from pow(4,i)+3*pow(2,i-1)+1 (prefixed with 1)

unsigned insertion_sort(unsigned a[], unsigned n, unsigned gap) {
  unsigned count = 0;
  for (unsigned i = gap; i < n; i++) {
    unsigned v = a[i];
    int j = i - gap;
    while (j >= 0 && a[j] > v) {
      a[j + gap] = a[j];
      j -= gap;
      count++;
    }
    a[j + gap] = v;
  }
  return count;
}

void shell_sort(unsigned a[], unsigned n) {
  int i;
  unsigned gap_num = 11;
  while (gaps[--gap_num] >= n)
    ;

  unsigned count = 0;
  for (i = gap_num; i >= 0; i--)
    count += insertion_sort(a, n, gaps[i]);

  cout << (gap_num + 1) << '\n';

  for (i = gap_num; i; i--)
    cout << gaps[i] << ' ';
  cout << gaps[0] << '\n';

  cout << count << '\n';
}

int main() {
  std::ios::sync_with_stdio(false);
  unsigned n, i;
  cin >> n;
  auto a = new unsigned[n];
  for (i = 0; i < n; i++)
    cin >> a[i];
  shell_sort(a, n);
  for (i = 0; i < n; i++)
    cout << a[i] << '\n';
  delete[] (a);
}

Fixed submission:
#include <iostream>
using std::cin;
using std::cout;

unsigned gaps[] = {1,    8,     23,    77,     281,    1073,
                   4193, 16577, 65921, 262913, 1050113};
// generated from pow(4,i)+3*pow(2,i-1)+1 (prefixed with 1)

unsigned insertion_sort(unsigned a[], unsigned n, unsigned gap) {
  unsigned count = 0;
  for (unsigned i = gap; i < n; i++) {
    unsigned v = a[i];
    int j = i - gap;
    while (j >= 0 && a[j] > v) {
      a[j + gap] = a[j];
      j -= gap;
      count++;
    }
    a[j + gap] = v;
  }
  return count;
}

void shell_sort(unsigned a[], unsigned n) {
  int i;
  unsigned gap_num = 11;
  while (gaps[--gap_num] > n)
    ;

  unsigned count = 0;
  for (i = gap_num; i >= 0; i--)
    count += insertion_sort(a, n, gaps[i]);

  cout << (gap_num + 1) << '\n';

  for (i = gap_num; i; i--)
    cout << gaps[i] << ' ';
  cout << gaps[0] << '\n';

  cout << count << '\n';
}

int main() {
  std::ios::sync_with_stdio(false);
  unsigned n, i;
  cin >> n;
  auto a = new unsigned[n];
  for (i = 0; i < n; i++)
    cin >> a[i];
  shell_sort(a, n);
  for (i = 0; i < n; i++)
    cout << a[i] << '\n';
  delete[] (a);
}
Problem p02262 (C++)

Buggy submission (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}
Problem p02262 (C++)

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

typedef long long ll;
typedef unsigned long long ull;

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

  int m = 0;
  int g = 0;
  int cnt = 0;
  vector<int> G;
  while (3 * g + 1 < n)
    g = 3 * g + 1;
  for (; g > 0; g /= 3) {
    G.push_back(g);
    m++;
  }
  for (int i = 0; i < m; i++) {
    for (int j = G[i]; j < n; j++) {
      int v = A[j];
      int k = j - G[i];
      while (k >= 0 && A[k] > v) {
        A[k + G[i]] = A[k];
        k -= G[i];
        cnt++;
      }
      A[k + G[i]] = v;
    }
  }

  cout << m << endl;
  cout << G[0];
  for (int i = 1; i < m; i++)
    cout << ' ' << G[i];
  cout << endl;
  cout << cnt << endl;
  for (int i = 0; i < n; i++)
    cout << A[i] << endl;

  return 0;
}


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

typedef long long ll;
typedef unsigned long long ull;

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

  int m = 0;
  int g = 0;
  int cnt = 0;
  vector<int> G;
  while (3 * g + 1 <= n)
    g = 3 * g + 1;
  for (; g > 0; g /= 3) {
    G.push_back(g);
    m++;
  }
  for (int i = 0; i < m; i++) {
    for (int j = G[i]; j < n; j++) {
      int v = A[j];
      int k = j - G[i];
      while (k >= 0 && A[k] > v) {
        A[k + G[i]] = A[k];
        k -= G[i];
        cnt++;
      }
      A[k + G[i]] = v;
    }
  }

  cout << m << endl;
  cout << G[0];
  for (int i = 1; i < m; i++)
    cout << ' ' << G[i];
  cout << endl;
  cout << cnt << endl;
  for (int i = 0; i < n; i++)
    cout << A[i] << endl;

  return 0;
}
Problem p02262 (C++)

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

using namespace std;

int g_count = 0;
const int MAX_N = 10000;
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;
      g_count++;
    }
    a[j + g] = v;
  }
}

int shellSort(int *a, int n) {

  for (int i = 1;;) {
    if (i > n) {
      break;
    }
    g.push_back(i);
    i = 3 * i + 1;
  }

  for (int i = g.size() - 1; i >= 0; i--) {
    insertionSort(a, n, g[i]);
  }
  return g_count;
}

int main() {
  int n;
  int a[MAX_N];
  cin >> n;

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

  int count = shellSort(a, n);

  cout << g.size() << endl;
  for (int i = g.size() - 1; i >= 0; i--) {
    cout << g[i] << " ";
  }
  cout << endl;
  cout << count << endl;
  for (int i = 0; i < n; i++) {
    cout << a[i] << endl;
  }
}

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

using namespace std;

long long g_count = 0;
const int MAX_N = 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;
      g_count++;
    }
    a[j + g] = v;
  }
}

int shellSort(int *a, int n) {

  for (int i = 1;;) {
    if (i > n) {
      break;
    }
    g.push_back(i);
    i = 3 * i + 1;
  }

  for (int i = g.size() - 1; i >= 0; i--) {
    insertionSort(a, n, g[i]);
  }
  return g_count;
}

int main() {
  int n;
  int a[MAX_N];
  cin >> n;

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

  int count = shellSort(a, n);

  cout << g.size() << endl;
  for (int i = g.size() - 1; i >= 0; i--) {
    cout << g[i] << " ";
  }
  cout << endl;
  cout << count << endl;
  for (int i = 0; i < n; i++) {
    cout << a[i] << endl;
  }
}
Problem p02262 (C++)

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

unsigned gaps[] = {1, 8, 23, 77, 281, 1073, 4193, 16577, 65921, 262913};
// generated from pow(4,i)+3*pow(2,i-1)+1 (prefixed with 1)

unsigned insertion_sort(unsigned a[], unsigned n, unsigned gap = 1) {
  unsigned count = 0;
  for (unsigned i = gap; i < n; i++) {
    unsigned v = a[i];
    int j = i - gap;
    while (j >= 0 && a[j] > v) {
      a[j + gap] = a[j];
      j -= gap;
      count++;
    }
    a[j + gap] = v;
  }
  return count;
}

void shell_sort(unsigned a[], unsigned n) {
  unsigned gap_num = 0;
  while (gaps[++gap_num] < n)
    ;
  gap_num--;

  unsigned count = 0;
  for (int i = gap_num; i >= 0; i--)
    count += insertion_sort(a, n, gaps[i]);

  std::cout << (gap_num + 1) << '\n';

  for (int i = gap_num; i; i--)
    std::cerr << gaps[i] << ' ';
  std::cerr << gaps[0] << '\n';

  std::cout << count << '\n';
}

int main() {
  std::ios::sync_with_stdio(false);
  unsigned n;
  std::cin >> n;
  std::vector<unsigned> a(n);
  for (unsigned i = 0; i < n; i++)
    std::cin >> a[i];
  shell_sort(a.data(), n);
  for (auto &v : a)
    std::cout << v << '\n';
}

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

unsigned gaps[] = {1, 8, 23, 77, 281, 1073, 4193, 16577, 65921, 262913};
// generated from pow(4,i)+3*pow(2,i-1)+1 (prefixed with 1)

unsigned insertion_sort(unsigned a[], unsigned n, unsigned gap = 1) {
  unsigned count = 0;
  for (unsigned i = gap; i < n; i++) {
    unsigned v = a[i];
    int j = i - gap;
    while (j >= 0 && a[j] > v) {
      a[j + gap] = a[j];
      j -= gap;
      count++;
    }
    a[j + gap] = v;
  }
  return count;
}

void shell_sort(unsigned a[], unsigned n) {
  unsigned gap_num = 0;
  while (gaps[++gap_num] < n)
    ;
  gap_num--;

  unsigned count = 0;
  for (int i = gap_num; i >= 0; i--)
    count += insertion_sort(a, n, gaps[i]);

  std::cout << (gap_num + 1) << '\n';

  for (int i = gap_num; i; i--)
    std::cout << gaps[i] << ' ';
  std::cout << gaps[0] << '\n';

  std::cout << count << '\n';
}

int main() {
  std::ios::sync_with_stdio(false);
  unsigned n;
  std::cin >> n;
  std::vector<unsigned> a(n);
  for (unsigned i = 0; i < n; i++)
    std::cin >> a[i];
  shell_sort(a.data(), n);
  for (auto &v : a)
    std::cout << v << '\n';
}
Problem p02262 (C++)

Buggy submission (status: 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[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;
}

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