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

#include <algorithm>
#include <stdio.h>

int m = 0;
int count = 0;
int G[1000000];

void trace(int a[], int size) {
  for (int i = 0; i < size; ++i) {
    if (i > 0) {
      printf(" ");
    }
    printf("%d", a[i]);
  }
  printf("\\n");
}

void InsertionSort(int A[], int N, 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;
      count++;
    }
    A[j + g] = key;
  }
}

void ShellSort(int A[], int N) {
  int g = 0;
  while (true) {
    g = 3 * g + 1;
    if (g > N) {
      break;
    }
    G[m] = g;
    ++m;
  }
  for (int i = m - 1; i >= 0; --i) {
    InsertionSort(A, N, G[i]);
  }
}

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

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

  ShellSort(A, N);

  printf("%d\\n", m);
  for (int i = m - 1; i >= 0; --i) {
    printf("%d", G[i]);
    if (i > 0)
      printf(" ");
  }
  printf("\\n");
  printf("%d\\n", count);
  for (int i = 0; i < N; i++) {
    printf("%d\\n", A[i]);
  }
  return 0;
}

Error: 0

Fixed submission:


#include <algorithm>
#include <stdio.h>

int m = 0;
int count = 0;
int G[1000000];

void trace(int a[], int size) {
  for (int i = 0; i < size; ++i) {
    if (i > 0) {
      printf(" ");
    }
    printf("%d", a[i]);
  }
  printf("\\n");
}

void InsertionSort(int A[], int N, 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;
      count++;
    }
    A[j + g] = key;
  }
}

void ShellSort(int A[], int N) {
  int k = N / 3;
  while (k >= 0) {
    int g = 3 * k + 1;
    if (g > N) {
      --k;
      continue;
    }
    G[m] = g;
    ++m;
    --k;
  }
  for (int i = 0; i < m; ++i) {
    InsertionSort(A, N, G[i]);
  }
}

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

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

  ShellSort(A, N);

  printf("%d\\n", m);
  trace(G, m);
  printf("%d\\n", count);
  for (int i = 0; i < N; i++) {
    printf("%d\\n", A[i]);
  }
  return 0;
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

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

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

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

void shellSort(int A[], int n) {
  for (int h = 1;;) {
    if (h > n)
      break;
    G.push_back(h);
    h = 3 * h + 1;
  }

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

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

  shellSort(A, n);

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

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

  return 0;
}


Error: WA

Fixed submission:

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

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

void insertionSort(int A[], int n, int g) {
  for (int i = g; i < n - 1; 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) {
  int h;
  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]);
  }
}

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

  shellSort(A, n);

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

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

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

#include <bits/stdc++.h>
using namespace std;
vector<int> A;
int cnt;
void insertionSort(int n, int g) {
  int j, v;
  for (int i = g; i < n; i++) {
    v = A[i];
    j = i - g;
    while (j >= 0 && A[j] > v) {
      A[j + g] = A[j];
      j = j - g;
      cnt++;
    }
    A[j + g] = v;
  }
}
void shellSort(int n) {
  cnt = 0;
  int siz = 0;
  vector<int> G;
  while (1) {
    siz = siz * 3 + 1;
    if (siz >= n && siz != 1)
      break;
    G.push_back(siz);
  }
  cout << G.size() << endl;
  for (int i = G.size() - 1; i >= 0; i--) {
    insertionSort(n, G[i]);
    cout << G[i] << ((i != 0) ? " " : "\\n");
  }
}

int main(void) {
  int n, t;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> t;
    A.push_back(t);
  }
  shellSort(n);
  cout << cnt << endl;
  for (int i : A) {
    cout << i << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
vector<int> A;
int cnt;
void insertionSort(int n, int g) {
  int j, v;
  for (int i = g; i < n; i++) {
    v = A[i];
    j = i - g;
    while (j >= 0 && A[j] > v) {
      A[j + g] = A[j];
      j = j - g;
      cnt++;
    }
    A[j + g] = v;
  }
}
void shellSort(int n) {
  cnt = 0;
  int siz = 0;
  vector<int> G;
  while (1) {
    siz = siz * 3 + 1;
    if (siz >= n)
      break;
    G.push_back(siz);
  }
  cout << G.size() << endl;
  for (int i = G.size() - 1; i >= 0; i--) {
    insertionSort(n, G[i]);
    cout << G[i] << ((i != 0) ? " " : "\\n");
  }
}

int main(void) {
  int n, t;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> t;
    A.push_back(t);
  }
  shellSort(n);
  cout << cnt << endl;
  for (int i : A) {
    cout << i << endl;
  }
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int m;
vector<int> G;
int insertionSort(int A[], int n, int g) {
  int cnt = 0;
  for (int i = g; i < n; ++i) {
    int v = A[i];
    int j = i - g;
    while (j >= 0 && A[j] > v) {
      A[j + g] = A[j];
      j -= g;
      cnt++;
    }
    A[j + g] = v;
  }
  return cnt;
}
int shellSort(int A[], int N) {
  int cnt = 0;
  int x = 3;
  int y = 1;
  while (y <= N) {
    G.push_back(y);
    x *= 3;
    y = (x - 1) / 2;
  }
  reverse(G.begin(), G.end());
  m = G.size();
  for (int i = 0; i < m; ++i) {
    cnt += insertionSort(A, N, G[i]);
  }
  return cnt;
}
int main() {
  int N;
  cin >> N;
  int A[1145149];
  for (int i = 0; i < N; ++i)
    cin >> A[i];
  int res = shellSort(A, N);
  cout << m << endl;
  for (int i = 0; i < m; ++i) {
    if (i)
      cout << " ";
    cout << G[i];
    if (i == m - 1)
      cout << endl;
  }
  cout << res << endl;
  for (int i = 0; i < N; ++i) {
    cout << A[i] << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int m;
vector<int> G;
int insertionSort(int A[], int n, int g) {
  int cnt = 0;
  for (int i = g; i < n; ++i) {
    int v = A[i];
    int j = i - g;
    while (j >= 0 && A[j] > v) {
      A[j + g] = A[j];
      j -= g;
      cnt++;
    }
    A[j + g] = v;
  }
  return cnt;
}
int shellSort(int A[], int N) {
  int cnt = 0;
  int x = 3;
  int y = 1;
  while (y <= N) {
    G.push_back(y);
    x *= 3;
    y = (x - 1) / 2;
  }
  reverse(G.begin(), G.end());
  m = G.size();
  for (int i = 0; i < m; ++i) {
    cnt += insertionSort(A, N, G[i]);
  }
  return cnt;
}
int main() {
  int N;
  cin >> N;
  int A[114514];
  for (int i = 0; i < N; ++i)
    cin >> A[i];
  int res = shellSort(A, N);
  cout << m << endl;
  for (int i = 0; i < m; ++i) {
    if (i)
      cout << " ";
    cout << G[i];
    if (i == m - 1)
      cout << endl;
  }
  cout << res << endl;
  for (int i = 0; i < N; ++i) {
    cout << A[i] << endl;
  }
  return 0;
}
Problem p02262 (C++) — buggy submission, verdict: 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);
}

Error: WA

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

#include <stdio.h>
#include <stdlib.h>

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 i, count = 0;
  for (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 = 0;
  while (gaps[++gap_num] < n)
    ;
  gap_num--;

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

  printf("%u\\n", (gap_num + 1));

  for (i = gap_num; i; i--)
    printf("%u ", gaps[i]);
  printf("%u\\n", gaps[0]);

  printf("%u\\n", count);
}

int main() {
  unsigned n, i;
  scanf("%u", &n);
  unsigned *a = (unsigned *)malloc(sizeof(unsigned) * n);
  for (i = 0; i < n; i++)
    scanf("%u", &a[i]);
  shell_sort(a, n);
  for (i = 0; i < n; i++)
    printf("%u\\n", a[i]);
  free(a);
  return 0;
}

Error: WA

Fixed submission:

#include <stdio.h>
#include <stdlib.h>

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]);

  printf("%d\\n", (gap_num + 1));

  for (int i = gap_num; i; i--)
    printf("%d ", gaps[i]);
  printf("%d\\n", gaps[0]);

  printf("%d\\n", count);
}

int main() {
  unsigned n;
  scanf("%u", &n);
  unsigned *a = (unsigned *)malloc(sizeof(unsigned) * n);
  for (unsigned i = 0; i < n; i++)
    scanf("%u", &a[i]);
  shell_sort(a, n);
  for (unsigned i = 0; i < n; i++)
    printf("%d\\n", a[i]);
  free(a);
  return 0;
}
Problem p02262 (C++) — buggy submission, verdict: 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 = 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);
}

Error: TLE

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

Error: WA

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

#include <algorithm>
#include <iostream>
#include <list>
#include <string>
#include <vector>

using namespace std;

template <typename T> void WriteLine_Container(const T &container);
void InsertionSort(vector<int> &v, int n, int g, int &cnt);
void ShellSort(vector<int> &v, int n);

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

  ShellSort(A, A.size());

  for_each(A.begin(), A.end(), [](int x) { cout << x << endl; });

  return 0;
}

template <typename T> void WriteLine_Container(T &container) {

  auto it = container.begin();
  while (true) {
    cout << *it;
    it++;
    if (it == container.end())
      break;
    else
      cout << " ";
  }
  cout << endl;
}

void InsertionSort(vector<int> &v, int n, int g, int &cnt) {
  for (int i = g; i < n; i++) {
    int temp = v[i];
    int j = i - g;
    while (j >= 0 && v[j] > temp) {
      v[j + g] = v[j];
      j = j - g;
      cnt++;
    }
    v[j + g] = temp;
  }
}

void ShellSort(vector<int> &v, int n) {
  int cnt = 0;

  list<int> li;
  li.push_front(1);

  for (int i = 4; i < v.size(); i = i * 3 + 1) {
    li.push_front(i);
  }
  int m = li.size();

  cout << m << endl;

  WriteLine_Container(li);

  for_each(li.begin(), li.end(), [&](int x) { InsertionSort(v, n, x, cnt); });

  cout << cnt << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <list>
#include <string>
#include <vector>

using namespace std;

template <typename T> void WriteLine_Container(const T &container);
void InsertionSort(vector<int> &v, int n, int g, int &cnt);
void ShellSort(vector<int> &v, int n);

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

  ShellSort(A, A.size());

  for_each(A.begin(), A.end(), [](int x) { cout << x << endl; });

  return 0;
}

template <typename T> void WriteLine_Container(T &container) {

  auto it = container.begin();
  while (true) {
    cout << *it;
    it++;
    if (it != container.end())
      cout << " ";
  }
  cout << endl;
}

void InsertionSort(vector<int> &v, int n, int g, int &cnt) {
  for (int i = g; i < n; i++) {
    int temp = v[i];
    int j = i - g;
    while (j >= 0 && v[j] > temp) {
      v[j + g] = v[j];
      j = j - g;
      cnt++;
    }
    v[j + g] = temp;
  }
}

void ShellSort(vector<int> &v, int n) {
  int cnt = 0;

  list<int> li;
  li.push_front(1);

  for (int i = 4; i < v.size(); i = i * 3 + 1) {
    li.push_front(i);
  }
  int m = li.size();

  cout << m << endl;

  WriteLine_Container(li);

  for_each(li.begin(), li.end(), [&](int x) { InsertionSort(v, n, x, cnt); });

  cout << cnt << endl;
}
Problem p02262 (C++) — buggy submission, verdict: 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--) {
    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';
  }
}

Error: WA

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--) {
    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';
  }
}