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

#include <iostream>
#define maxn 36 + 5
using namespace std;

typedef struct Card {
  char Suit;
  char Value;
} card;

void BubbleSort(card *A, int N) {
  for (int i = 0; i < N; i++) {
    for (int j = N - 1; j > i; j--) {
      if (A[j].Value < A[j - 1].Value) {
        card temp = A[j];
        A[j] = A[j - 1];
        A[j - 1] = temp;
      }
    }
  }
}

void SelectionSort(card *A, int N) {
  for (int i = 0; i < N; i++) {
    int minj = i;
    for (int j = i; j < N; j++) {
      if (A[j].Value < A[minj].Value)
        minj = j;
    }
    card temp = A[i];
    A[i] = A[minj];
    A[minj] = temp;
  }
}

void print(card *A, int N) {
  for (int i = 0; i < N; i++) {
    cout << A[i].Suit << A[i].Value;
    if (i != N - 1)
      cout << " ";
    else
      cout << endl;
  }
}

int main() {
  int N;
  card T1[maxn], T2[maxn];
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> T1[i].Suit >> T1[i].Value;
    T2[i] = T1[i];
  }
  BubbleSort(T1, N);
  SelectionSort(T2, N);
  int flag = 1;
  for (int i = 0; i < N; i++) {
    if (T1[i].Suit != T2[i].Suit) {
      flag = 0;
      break;
    }
  }
  print(T1, N);
  cout << "Stable" << endl;
  print(T2, N);
  if (flag)
    cout << "Stable" << endl;
  else
    cout << "Not stable" << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <stdio.h>
#define maxn 36 + 5

typedef struct Card {
  char Suit;
  char Value;
} card;

void BubbleSort(card *A, int N) {
  for (int i = 0; i < N; i++) {
    for (int j = N - 1; j > i; j--) {
      if (A[j].Value < A[j - 1].Value) {
        card temp = A[j];
        A[j] = A[j - 1];
        A[j - 1] = temp;
      }
    }
  }
}

void SelectionSort(card *A, int N) {
  for (int i = 0; i < N; i++) {
    int minj = i;
    for (int j = i; j < N; j++) {
      if (A[j].Value < A[minj].Value)
        minj = j;
    }
    card temp = A[i];
    A[i] = A[minj];
    A[minj] = temp;
  }
}

void print(card *A, int N) {
  for (int i = 0; i < N; i++) {
    printf("%c%d", A[i].Suit, A[i].Value);
    if (i != N - 1)
      printf(" ");
    else
      printf("\\n");
  }
}

int main() {
  int N;
  card T1[maxn], T2[maxn];
  scanf("%d", &N);
  // printf("%d\\n",N);
  for (int i = 0; i < N; i++) {
    scanf("%*c%c%c", T1[i].Suit, T1[i].Value);
    // printf("%c%c\\n", T1[i].Suit, T1[i].Value);
    T2[i] = T1[i];
  }
  BubbleSort(T1, N);
  SelectionSort(T2, N);
  int flag = 1;
  for (int i = 0; i < N; i++) {
    if (T1[i].Suit != T2[i].Suit) {
      flag = 0;
      break;
    }
  }
  print(T1, N);
  printf("Stable\\n");
  print(T2, N);
  if (flag)
    printf("Stable\\n");
  else
    printf("Unstable\\n");
}
Problem p02261 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

typedef struct {
  char suit;
  char value;
} Card;

void bubbleSort(Card A[], int N) {
  for (int i = 0; i < N; i++) {
    for (int j = N - 1; j >= i; j--) {
      if (A[j].value < A[j - 1].value) {
        Card t = A[j];
        A[j] = A[j - 1];
        A[j - 1] = t;
      }
    }
  }
}

void selectionSort(Card A[], int N) {
  for (int i = 0; i < N; i++) {
    int minj = i;
    for (int j = i; j < N; j++) {
      if (A[j].value < A[minj].value)
        minj = j;
    }
    Card t = A[i];
    A[i] = A[minj];
    A[minj] = t;
  }
}

void print(Card A[], int N) {
  for (int i = 0; i < N; i++) {
    if (i > 0)
      cout << " ";
    cout << A[i].suit << A[i].value;
  }
  cout << endl;
}

bool isStable(Card C1[], Card C2[], int N) {
  for (int i = 0; i < N; i++) {
    if (C1[i].suit != C2[i].suit)
      return false;
  }
  return true;
}

int main() {
  Card C1[100], C2[100];
  int N;
  char ch;

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> C1[i].suit >> C1[i].value;
  }

  for (int i = 0; i < N; i++)
    C2[i] = C1[i];

  bubbleSort(C1, N);
  selectionSort(C2, N);

  print(C1, N);
  cout << "Stable" << endl;
  print(C2, N);
  if (isStable(C1, C2, N)) {
    cout << "Stable" << endl;
  } else {
    cout << "Not stable" << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
using namespace std;

typedef struct {
  char suit;
  char value;
} Card;

void bubbleSort(Card A[], int N) {
  for (int i = 0; i < N; i++) {
    for (int j = N - 1; j >= i; j--) {
      if (A[j].value >= A[j - 1].value) {
        Card t = A[j];
        A[j] = A[j - 1];
        A[j - 1] = t;
      }
    }
  }
}

void selectionSort(Card A[], int N) {
  for (int i = 0; i < N; i++) {
    int minj = i;
    for (int j = i; j < N; j++) {
      if (A[j].value < A[minj].value)
        minj = j;
    }
    Card t = A[i];
    A[i] = A[minj];
    A[minj] = t;
  }
}

void print(Card A[], int N) {
  for (int i = 0; i < N; i++) {
    if (i > 0)
      cout << " ";
    cout << A[i].suit << A[i].value;
  }
  cout << endl;
}

bool isStable(Card C1[], Card C2[], int N) {
  for (int i = 0; i < N; i++) {
    if (C1[i].suit != C2[i].suit)
      return false;
  }
  return true;
}

int main() {
  Card C1[100], C2[100];
  int N;
  char ch;

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> C1[i].suit >> C1[i].value;
  }

  for (int i = 0; i < N; i++)
    C2[i] = C1[i];

  bubbleSort(C1, N);
  selectionSort(C2, N);

  print(C1, N);
  cout << "Stable" << endl;
  print(C2, N);
  if (isStable(C1, C2, N)) {
    cout << "Stable" << endl;
  } else {
    cout << "Not stable" << endl;
  }

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

#include <iostream>
using namespace std;

struct Card {
  int num;
  char suit;
};

void bubbleSort(Card c[], int N);
void selectionSort(Card c[], int N);
void swap(Card &x, Card &y);
void showArray(Card c[], int N);
bool Stable(Card x[], Card y[], int N);

int main() {
  int N;
  Card c1[36], c2[36];
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> c1[i].suit >> c1[i].num;
    c2[i] = c1[i];
  }

  bubbleSort(c1, N);
  cout << "Stable" << endl;
  selectionSort(c2, N);
  if (Stable(c1, c2, N))
    cout << "Stable" << endl;
  else
    cout << "Not stable" << endl;
  return 0;
}

void bubbleSort(Card c[], int N) {
  for (int i = 0; i < N; i++)
    for (int j = N - 1; j > i; j--)
      if (c[j].num < c[j - 1].num)
        swap(c[j], c[j - 1]);
  showArray(c, N);
}

void selectionSort(Card c[], int N) {
  int min;
  for (int i = 0; i < N; i++) {
    min = i;
    for (int j = i; j < N; j++)
      if (c[j].num < c[min].num)
        min = j;
    swap(c[i], c[min]);
  }
  showArray(c, N);
}

void swap(Card &x, Card &y) {
  Card temp = x;
  x = y;
  y = temp;
}

void showArray(Card c[], int N) {
  cout << c[0].suit << c[0].num;
  for (int i = 1; i < N; i++)
    cout << ' ' << c[i].suit << c[i].num;
  cout << endl;
}

bool Stable(Card x[], Card y[], int N) {
  for (int i = 0; i < N; i++)
    if (x[i].suit != y[i].suit)
      return false;
  return true;
}

Error: WA

Fixed submission:

#include <iostream>
using namespace std;

struct Card {
  int num;
  char suit;
};

void bubbleSort(Card c[], int N);
void selectionSort(Card c[], int N);
void swap(Card &x, Card &y);
void showArray(Card c[], int N);
bool Stable(Card x[], Card y[], int N);

int main() {
  int N;
  Card c1[36], c2[36];
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> c1[i].suit >> c1[i].num;
  for (int i = 0; i < N; i++)
    c2[i] = c1[i];

  bubbleSort(c1, N);
  cout << "Stable" << endl;
  selectionSort(c2, N);
  if (Stable(c1, c2, N))
    cout << "Stable" << endl;
  else
    cout << "Not stable" << endl;
  return 0;
}

void bubbleSort(Card c[], int N) {
  for (int i = 0; i < N; i++)
    for (int j = N - 1; j >= i; j--)
      if (c[j].num < c[j - 1].num)
        swap(c[j], c[j - 1]);
  showArray(c, N);
}

void selectionSort(Card c[], int N) {
  int min;
  for (int i = 0; i < N; i++) {
    min = i;
    for (int j = i; j < N; j++)
      if (c[j].num < c[min].num)
        min = j;
    swap(c[i], c[min]);
  }
  showArray(c, N);
}

void swap(Card &x, Card &y) {
  Card temp = x;
  x = y;
  y = temp;
}

void showArray(Card c[], int N) {
  cout << c[0].suit << c[0].num;
  for (int i = 1; i < N; i++)
    cout << ' ' << c[i].suit << c[i].num;
  cout << endl;
}

bool Stable(Card x[], Card y[], int N) {
  for (int i = 0; i < N; i++)
    if (x[i].suit != y[i].suit)
      return false;
  return true;
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <vector>

#define REP(i, a, b) for (i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;
int cnt = 0;

void insertionSort(vector<int> &A, int g) {
  int i, j, v, n = A.size();
  REP(i, g, n) {
    v = A[i];
    j = i - g;
    while (j >= 0 && A[j] > v) {
      A[j + g] = A[j];
      j = j - g;
      cnt++;
      // if (cnt > ceil(pow(n,1.5))) break;
    }
    A[j + g] = v;
  }
}
void shellSort(vector<int> &A) {
  int i, cnt = 0, n = A.size();
  vector<int> G;
  for (i = 1; i <= n; i = 3 * i + 1)
    G.push_back(i);
  reverse(G.begin(), G.end());
  int m = G.size();
  printf("%d\\n", m);
  rep(i, m) printf(i < m - 1 ? "%d " : "%d\\n", G[i]);
  rep(i, m) insertionSort(A, G[i]);
}
int main() {
  int n, i, x;
  scanf("%d", &n);
  vector<int> A;
  rep(i, n) scanf("%d", &x), A.push_back(x);
  shellSort(A);
  printf("%d\\n", cnt);
  rep(i, n) printf("%d\\n", A[i]);
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <vector>

#define REP(i, a, b) for (i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;
int cnt = 0;

void insertionSort(vector<int> &A, int g) {
  int i, j, v, n = A.size();
  REP(i, g, n) {
    v = A[i];
    j = i - g;
    while (j >= 0 && A[j] > v) {
      A[j + g] = A[j];
      j = j - g;
      cnt++;
      // if (cnt > ceil(pow(n,1.5))) break;
    }
    A[j + g] = v;
  }
}
void shellSort(vector<int> &A) {
  int i, cnt = 0;
  int m = ceil(((double)A.size() - 1.0) / 3.0);
  printf("%d\\n", m);
  vector<int> G(m);
  for (i = 0; i <= m; i++)
    G[i] = 3 * i + 1;
  reverse(G.begin(), G.end());
  rep(i, m) printf(i < m - 1 ? "%d " : "%d\\n", G[i]);
  rep(i, m) insertionSort(A, G[i]);
}
int main() {
  int n, i, x;
  scanf("%d", &n);
  vector<int> A;
  rep(i, n) scanf("%d", &x), A.push_back(x);
  shellSort(A);
  printf("%d\\n", cnt);
  rep(i, n) printf("%d\\n", A[i]);
}
Problem p02262 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#include <string.h>

struct NUM {
  int cnt;
  int m;
  int G[100];
};

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

int dev3(int N) {
  int i = 1;
  while (i < N) {
    if (3 * i + 1 > N)
      break;
    i = 3 * i + 1;
  }
  return i;
}

struct NUM ShellSort(int A[], int n) {
  struct NUM num;
  int cnt = 0;
  int m = 0;
  int G[100];
  for (int i = dev3(n); i > 0; i = (i - 1) / 3) {
    G[m] = i;
    m++;
  }
  for (int i = 0; i < m; i++) {
    cnt += InsertionSort(A, n, G[i]);
  }
  num.cnt = cnt;
  num.m = m;
  for (int i = 0; i < m; i++)
    num.G[i] = G[i];
  return num;
}

int main() {
  int N, A[1000000];
  struct NUM num;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &A[i]);
  }
  num = ShellSort(A, N);
  printf("%d\\n", num.m);
  for (int i = 0; i < num.m; i++) {
    if (i == num.m - 1) {
      printf("%d\\n", num.G[i]);
      break;
    }
    printf("%d ", num.G[i]);
  }
  printf("%d\\n", num.cnt);
  for (int i = 0; i < N; i++) {
    printf("%d\\n", A[i]);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <stdio.h>
#include <string.h>

struct NUM {
  int cnt;
  int m;
  int G[100];
};

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

int dev3(int N) {
  int i = 1;
  while (i <= N) {
    i *= 3;
  }
  return i / 3;
}

struct NUM ShellSort(int A[], int n) {
  struct NUM num;
  int cnt = 0;
  int m = 0;
  int G[100];
  for (int i = dev3(n) + 1; i > 0; i = (i - 1) / 3 + 1) {
    G[m] = i;
    m++;
  }
  for (int i = 0; i < m; i++) {
    cnt += InsertionSort(A, n, G[i]);
  }
  num.cnt = cnt;
  num.m = m;
  for (int i = 0; i < m; i++)
    num.G[i] = G[i];
  return num;
}

int main() {
  int N, A[1000000];
  struct NUM num;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &A[i]);
  }
  num = ShellSort(A, N);
  printf("%d\\n", num.m);
  for (int i = 0; i < num.m; i++) {
    if (i == num.m - 1) {
      printf("%d\\n", num.G[i]);
      break;
    }
    printf("%d ", num.G[i]);
  }
  printf("%d\\n", num.cnt);
  for (int i = 0; i < N; i++) {
    printf("%d\\n", A[i]);
  }
  return 0;
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <string.h>

struct NUM {
  int cnt;
  int m;
  int G[100];
};

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

struct NUM ShellSort(int A[], int n) {
  struct NUM num;
  int cnt = 0;
  int m = 0;
  int G[100];
  for (int i = n; i > 0; i = i / 2) {
    G[m] = i;
    m++;
  }
  for (int i = 0; i < m; i++) {
    cnt += InsertionSort(A, n, G[i]);
  }
  num.cnt = cnt;
  num.m = m;
  for (int i = 0; i < m; i++)
    num.G[i] = G[i];
  return num;
}

int main() {
  int N, A[1000000];
  struct NUM num;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &A[i]);
  }
  num = ShellSort(A, N);
  printf("%d\\n", num.m);
  for (int i = 0; i < num.m; i++) {
    if (i == num.m - 1) {
      printf("%d\\n", num.G[i]);
      break;
    }
    printf("%d ", num.G[i]);
  }
  printf("%d\\n", num.cnt);
  for (int i = 0; i < N; i++) {
    printf("%d\\n", A[i]);
  }
  return 0;
}


Error: WA

Fixed submission:

#include <stdio.h>
#include <string.h>

struct NUM {
  int cnt;
  int m;
  int G[100];
};

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

struct NUM ShellSort(int A[], int n) {
  struct NUM num;
  int cnt = 0;
  int m = 0;
  int G[100];
  for (int i = n; i > 0; i = i / 2) {
    G[m] = i;
    m++;
  }
  for (int i = 0; i < m; i++) {
    cnt += InsertionSort(A, n, G[i]);
  }
  num.cnt = cnt;
  num.m = m;
  for (int i = 0; i < m; i++)
    num.G[i] = G[i];
  return num;
}

int main() {
  int N, A[1000];
  struct NUM num;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &A[i]);
  }
  num = ShellSort(A, N);
  printf("%d\\n", num.m);
  for (int i = 0; i < num.m; i++) {
    if (i == num.m - 1) {
      printf("%d\\n", num.G[i]);
      break;
    }
    printf("%d ", num.G[i]);
  }
  printf("%d\\n", num.cnt);
  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 <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e6 + 5;

int m[MAXN];
long long cnt;

void insertion(int m[], int dis, int n) {
  for (int i = dis; i < n; i++) {
    for (int j = i; j >= dis; j -= dis) {
      if (m[j] >= m[j - dis])
        break;
      else {
        int temp = m[j - dis];
        m[j - dis] = m[j];
        m[j] = temp;
        cnt++;
      }
    }
  }
}

void shell(int m[], int n) {
  vector<int> v;
  int temp = 1;
  while (temp < n) {
    v.push_back(temp);
    temp = temp * 3 + 1;
  }
  printf("%d\\n", v.size());
  printf("%d", v[v.size() - 1]);
  insertion(m, v[v.size() - 1], n);
  for (int i = v.size() - 2; i >= 0; i--) {
    printf(" %d", v[i]);
    insertion(m, v[i], n);
  }
  cout << endl << cnt << endl;
}

int main() {
  int n;
  cnt = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", m + i);
  if (n == 1) {
    printf("1\\n1\\n0\\n%d\\n", m[0]);
    return 0;
  }
  shell(m, n);
  for (int i = 0; i < n; i++)
    printf("%d\\n", m[i]);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e6 + 5;

int m[MAXN];
long long cnt;

void insertion(int m[], int dis, int n) {
  for (int i = dis; i < n; i++) {
    for (int j = i; j >= 0; j -= dis) {
      if (m[j] >= m[j - dis])
        break;
      else {
        int temp = m[j - dis];
        m[j - dis] = m[j];
        m[j] = temp;
        cnt++;
      }
    }
  }
}

void shell(int m[], int n) {
  vector<int> v;
  int temp = 1;
  while (temp < n) {
    v.push_back(temp);
    temp = temp * 3 + 1;
  }
  printf("%d\\n", v.size());
  printf("%d", v[v.size() - 1]);
  insertion(m, v[v.size() - 1], n);
  for (int i = v.size() - 2; i >= 0; i--) {
    printf(" %d", v[i]);
    insertion(m, v[i], n);
  }
  cout << endl << cnt << endl;
}

int main() {
  int n;
  cnt = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", m + i);
  shell(m, n);
  for (int i = 0; i < n; i++)
    printf("%d\\n", m[i]);
  return 0;
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

class cShell {
  int m_n;
  int cnt;
  vector<int> vA;
  vector<int> vg;

  void insertionSort(int g);

public:
  cShell(int n, vector<int> &v) {
    vA = v;
    m_n = n;
    cnt = 0;
  }

  void shellSort();

  int getm() { return vg.size(); }

  string getGapToStr();

  vector<int> getData() { return vA; }

  int getCnt() { return cnt; }
};

void cShell::insertionSort(int g) {
  for (int i = g; i < m_n; ++i) {
    int v = vA[i];
    int j = i - g;
    while (j >= 0 && vA[j] > v) {
      vA[j + g] = vA[j];
      j = j - g;
      cnt++;
    }
    vA[j + g] = v;
  }
}

void cShell::shellSort() {
  int cnt = 0;
  int m = vA.size();
  if (m / 2 > 0) {
    while (m /= 2) {
      vg.push_back(m);
    }
  } else {
    vg.push_back(1);
  }
  for (int i = 0; i < vg.size(); ++i) {
    insertionSort(vg[i]);
  }
}

string cShell::getGapToStr() {
  ostringstream oss;
  for (int i = 0; i < vg.size() - 1; ++i) {
    oss << vg[i] << " ";
  }
  oss << vg[vg.size() - 1];
  return oss.str();
}

int main() {
  int n;
  cin >> n;
  cin.ignore();

  vector<int> va;
  for (int i = 0; i < n; ++i) {
    int t;
    cin >> t;
    va.push_back(t);
  }

  cShell cs(n, va);
  cs.shellSort();

  cout << cs.getm() << endl;
  cout << cs.getGapToStr() << endl;
  cout << cs.getCnt() << endl;
  va = cs.getData();
  for (int i = 0; i < va.size(); ++i) {
    cout << va[i] << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

class cShell {
  int m_n;
  int cnt;
  vector<int> vA;
  vector<int> vg;

  void insertionSort(int g);

public:
  cShell(int n, vector<int> &v) {
    vA = v;
    m_n = n;
    cnt = 0;
  }

  void shellSort();

  int getm() { return vg.size(); }

  string getGapToStr();

  vector<int> getData() { return vA; }

  int getCnt() { return cnt; }
};

void cShell::insertionSort(int g) {
  for (int i = g; i < m_n; ++i) {
    int v = vA[i];
    int j = i - g;
    while (j >= 0 && vA[j] > v) {
      vA[j + g] = vA[j];
      j = j - g;
      cnt++;
    }
    vA[j + g] = v;
  }
}

void cShell::shellSort() {
  int cnt = 0;
  int m = vA.size();
  while (m /= 2) {
    vg.push_back(m);
  }
  for (int i = 0; i < vg.size(); ++i) {
    insertionSort(vg[i]);
  }
}

string cShell::getGapToStr() {
  ostringstream oss;
  for (int i = 0; i < vg.size() - 1; ++i) {
    oss << vg[i] << " ";
  }
  oss << vg[vg.size() - 1];
  return oss.str();
}

int main() {
  int n;
  cin >> n;
  cin.ignore();

  vector<int> va;
  for (int i = 0; i < n; ++i) {
    int t;
    cin >> t;
    va.push_back(t);
  }

  cShell cs(n, va);
  cs.shellSort();

  cout << cs.getm() << endl;
  cout << cs.getGapToStr() << endl;
  cout << cs.getCnt() << endl;
  va = cs.getData();
  for (int i = 0; i < va.size(); ++i) {
    cout << va[i] << endl;
  }

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

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

// g数组
vector<int> G;
long long cnt;
int A[1000000 + 10];

// 间隔为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 i = G.size() - 1; i >= 0; i--) {
    insertionSort(A, n, G[i]);
  }
}

// 生成g数组
void getArray(int n) {
  for (int h = 1; h <= n;) {
    G.push_back(h);
    h = 3 * h + 1;
  }
}

int main() {

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

  int len = G.size();
  printf("%d\\n", len);
  for (int i = len - 1; i >= 0; i--)
    printf("%d%c", G[i], i == 0 ? '\\n' : ' ');
  printf("%lld\\n", cnt);
  for (int i = 0; i < n; i++)
    printf("%d\\n", A[i]);
  return 0;
}


Error: -11

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