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

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

vector<int> InsertSort(vector<int> A, int i) {
  int v = A[i];
  int j = i - 1;
  while (j >= 0 && A[j] > v) {
    A[j + 1] = A[j];
    j--;
  }
  A[j + 1] = v;
  return A;
}

void Output(vector<int> A) {
  int N = A.size();
  for (int i = 0; i < N - 1; i++)
    cout << A[i] << ' ';
  cout << A[N - 1] << endl;
}

int main() {
  int N;
  cin >> N;
  vector<int> A;
  A.resize(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int i = 0; i < N; i++) {
    A = InsertSort(A, i);
    Output(A);
  }
  return 0;
}

Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

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

vector<int> InsertSort(vector<int> A, int i) {
  int v = A[i];
  int j = i - 1;
  while (j >= 0 && A[j] > v) {
    A[j + 1] = A[j];
    j--;
  }
  A[j + 1] = v;
  return A;
}

void Output(vector<int> A) {
  int n = A.size() - 1;
  for (int i = 0; i < n; i++)
    cout << A[i] << ' ';
  cout << A[n] << endl;
}

int main() {
  int N;
  cin >> N;
  vector<int> A;
  A.resize(N);
  for (int i = 0; i < N; i++)
    cin >> A[N];
  for (int i = 0; i < N; i++)
    Output(InsertSort(A, i));
  return 0;
}
Problem p02255 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
using namespace std;

int main() {
  int n;
  scanf("%d", &n);
  // printf("%d",n);
  int A[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }
  for (int foc = 0; foc < n; foc++) {
    int key = A[foc];
    int j = foc - 1;
    while (j >= 0 && A[j] > key) {
      A[j + 1] = A[j];
      j--;
    }
    A[j + 1] = key;
    // printf("debug\\n");
    for (int i = 0; i < n; i++) {
      printf("%d", A[i]);
      if (i != n - 1)
        printf(" ");
    }
    printf("\\n");
  }
}

Error: -11

Fixed submission:

#include <cstdio>
using namespace std;

int main() {
  int n;
  scanf("%d", &n);
  int A[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", A[i]);
  }
  for (int foc = 0; foc < n; foc++) {
    int key = A[foc];
    int j = foc - 1;
    while (j >= 0 && A[j] > key) {
      A[j + 1] = A[j];
      j--;
    }
    A[j + 1] = key;
    for (int i = 0; i < n; i++) {
      printf("%d", A[i]);
      if (i != n - 1)
        printf(" ");
    }
    printf("\\n");
  }
}
Problem p02255 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;
void print(vector<int> &values) {
  for (int i = 0; i < values.size() - 1; i++)
    cout << values[i] << " ";
  cout << values[values.size() - 1] << endl;
}
void insertionSort(std::vector<int> &numbers) {
  int key;
  for (int i = 1; i < numbers.size(); i++) {
    print(numbers);
    key = numbers[i];
    int j = i - 1;
    while ((j >= 0) and (numbers[j] > key)) {
      numbers[j + 1] = numbers[j];
      j--;
    }
    numbers[j + 1] = key;
  }
}
int main(void) {
  int n;
  std::vector<int> numbers;
  std::cin >> n;
  numbers.resize(n);
  for (int i = 0; i < n; i++)
    std::cin >> numbers[i];
  insertionSort(numbers);
  print(numbers);
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
void print(std::vector<int> &values) {
  for (int x : values) {
    std::cout << x << " ";
  }
  std::cout << std::endl;
}
void insertion_sort(std::vector<int> &numbers) {
  int key;
  int j;
  for (int i = 1; i < numbers.size(); i++) {
    key = numbers[i];

    j = i - 1;
    while ((j >= 0) and (numbers[j] > key)) {
      numbers[j + 1] = numbers[j];
      j--;
    }
    numbers[j + 1] = key;
    print(numbers);
  }
}
int main(void) {
  int n, x;
  std::vector<int> numbers(0);
  std::cin >> n;

  for (int i = 0; i < n; i++) {
    std::cin >> x;
    numbers.push_back(x);
  }
  print(numbers);
  insertion_sort(numbers);
  return 0;
}
Problem p02255 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

void OutputIntVector(const std::vector<int> &Vector);

int main() {
  int N;
  std::cin >> N;
  std::vector<int> A;
  A.resize(N);
  for (int i = 0; i < N; i++) {
    std::cin >> A[i];
  }

  OutputIntVector(A);

  for (int i = 1; i < N; i++) {
    int key = A[i];
    /* insert A[i] into the sorted sequence A[0,...,j-1] */
    int j = i - 1;
    while (j >= 0 && A[j] > key) {
      A[j + 1] = A[j];
      j--;
    }
    A[j + 1] = key;
    OutputIntVector(A);
  }

  return 0;
}

void OutputIntVector(const std::vector<int> &Vector) {
  for (unsigned int i = 0; i < Vector.size(); i++) {
    std::cout << Vector[i] << std::flush;
    if (i < Vector.size() - 1) {
      std::cout << " " << std::flush;
    } else {
      std::cout << std::endl;
    }
  }
}

Error: -11

Fixed submission:

#include <iostream>
#include <vector>

int main() {
  int N;
  std::cin >> N;
  std::vector<int> InputV;
  for (int i = 0; i < N; i++) {
    std::cin >> InputV[i];
  }

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      std::cout << InputV[j];
      if (N - j - 1)
        std::cout << " ";
    }
    std::cout << std::endl;
    for (i = 0; i < N; i++) {
      int key = InputV[i];
      int j = i;
      while (j > 0 && InputV[j] > key) {
        InputV[j] = InputV[j - 1];
        j--;
        InputV[j] = key;
      }
    }
  }
}
Problem p02255 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stdio.h>
using namespace std;

int main() {
  int n;

  cin >> n;
  int *a = new int[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int x = 0; x < n; x++) {
    cout << a[x] << flush;
    if (x != n - 1)
      cout << " " << flush;
  }
  cout << endl;
  for (int i = 1; i < n; i++) {
    int v = a[i];
    int j = i - 1;
    while (j >= 0 && a[j] > v) {
      a[j + 1] = a[j];
      j--;
    }
    a[j + 1] = v;

    for (int x = 0; x < n; x++) {
      cout << a[x] << flush;
      if (x != n - 1)
        cout << " " << flush;
    }
    cout << endl;
  }

  return 0;
}


Error: -11

Fixed submission:

#include <iostream>
#include <stdio.h>
using namespace std;

int main() {
  int n;

  cin >> n;
  int *a = new int[n];
  for (int i = 0; i < n; i++) {
    scanf("%d ", a[i]);
  }
  for (int i = 0; i < n - 1; i++) {
    int v = a[i];
    int j = i - 1;
    while (j >= 0 && a[j] > v) {
      a[j + 1] = a[j];
      j--;
    }
    a[j + 1] = v;
    for (int x = 0; x < n; x++) {
      cout << a[x] << " " << flush;
    }
    cout << endl;
  }
  return 0;
}
Problem p02255 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <limits>
#include <vector>

template <class T>
std::ostream &operator<<(std::ostream &o, const std::vector<T> &v) {
  o << v[0];
  for (unsigned int i = 1; i < v.size(); i++) {
    o << " " << v[i];
  }
  return o;
}

int main() {
  unsigned int N = 0;
  std::cin >> N;

  std::vector<int> A(N);
  for (unsigned int i = 0; i < N; i++) {
    std::cin >> A[i];
  }

  std::cout << A << std::endl;

  for (unsigned int i = 1; i < N; i++) {
    int val = A[i];
    unsigned int cand = i;
    while (cand > 0) {
      if (A[cand - 1] < val) {
        break;
      }
      A[cand] = A[cand - 1];
      cand--;
    }
    A[cand] = val;

    std::cout << A << std::endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <limits>
#include <vector>

template <class T>
std::ostream &operator<<(std::ostream &o, const std::vector<T> &v) {
  o << v[0];
  for (unsigned int i = 1; i < v.size(); i++) {
    o << " " << v[i];
  }
  return o;
}

int main() {
  unsigned int N = 0;
  std::cin >> N;

  std::vector<int> A(N);
  for (int &e : A) {
    std::cin >> e;
  }

  std::cout << A << std::endl;

  for (unsigned int i = 1; i < N; i++) {
    int val = A[i];
    int cand = i;
    while (cand > 0) {
      if (A[cand - 1] < val) {
        break;
      }
      A[cand] = A[cand - 1];
      cand--;
    }
    A[cand] = val;

    std::cout << A << std::endl;
  }

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

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

void fnInput(vector<int> &rvnSort) {
  int nMaxSiz;
  cin >> nMaxSiz;

  rvnSort.resize(nMaxSiz);
  for (int i = 0; i < nMaxSiz; i++)
    cin >> rvnSort[i];
}

void fnTrace(const vector<int> &cnrvnSort) {
  for (int i = 0; i < cnrvnSort.size(); i++) {
    if (i)
      cout << " ";
    cout << cnrvnSort[i];
  }
  cout << endl;
}

void fnInsertionSort(vector<int> &rvnSort) {
  for (int nSorting = 1; nSorting < rvnSort.size(); nSorting++) {
    int nTrg = rvnSort[nSorting];
    int nSorted = nSorting - 1;

    while (nSorted >= 0 && nTrg < rvnSort[nSorted]) {
      rvnSort[nSorted + 1] = rvnSort[nSorted];
      nSorted--;
    }
    rvnSort[nSorted + 1] = nTrg;
    fnTrace(rvnSort);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<int> vnSort;

  fnInput(vnSort);
  fnTrace(vnSort);
  fnInsertionSort(vnSort);

  return 0;
}

Error: -11

Fixed submission:

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

void fnInput(vector<int> &rvnSort) {
  int nMaxSiz;

  cin >> nMaxSiz;
  for (int i = 0; i < nMaxSiz; i++)
    cin >> rvnSort[i];
}

void fnTrace(const vector<int> &cnrvnSort) {
  for (int i = 0; i < cnrvnSort.size(); i++) {
    if (i)
      cout << " ";
    cout << cnrvnSort[i];
  }
  cout << endl;
}

void fnInsertionSort(vector<int> &rvnSort) {
  for (int nSorting = 1; nSorting < rvnSort.size(); nSorting++) {
    int nTrg = rvnSort[nSorting];
    int nSorted = nSorting - 1;

    while (nSorted >= 0 && nTrg < rvnSort[nSorted]) {
      rvnSort[nSorted + 1] = rvnSort[nSorted];
      nSorted--;
    }
    rvnSort[nSorted + 1] = nTrg;
    fnTrace(rvnSort);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<int> vnSort;

  fnInput(vnSort);
  fnTrace(vnSort);
  fnInsertionSort(vnSort);

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

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

int main() {

  int N;
  int A[100];
  int key;
  int j;
  cin >> N;
  if (N <= 0 || N > 100)
    return 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 1; i <= N - 1; i++) {
    for (int m = 0; m < N - 1; m++) {
      cout << A[m] << " ";
    }
    cout << A[N - 1] << endl;
    key = A[i];
    j = i - 1;
    while (j >= 0 && A[j] > key) {
      A[j + 1] = A[j];
      A[j] = key;
      j--;
    }
  }
  for (int m = 0; m < N - 1; m++) {
    cout << A[m] << " ";
  }
  cout << A[N - 1] << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  while (true) {
    int N;
    int A[100];
    int key;
    int j;
    cin >> N;
    if (N < 0 || N > 100)
      break;
    for (int i = 0; i < N; i++) {
      cin >> A[i];
    }
    for (int i = 1; i <= N - 1; i++) {
      for (int m = 0; m < N; m++) {
        cout << A[m] << " ";
      }
      cout << "" << endl;
      key = A[i];
      j = i - 1;
      while (j >= 0 && A[j] > key) {
        A[j + 1] = A[j];
        A[j] = key;
        j--;
      }
    }
    for (int m = 0; m < N; m++) {
      cout << A[m] << " ";
    }
    cout << "" << endl;
  }
  return 0;
}
Problem p02255 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    if (i == n - 1)
      cout << a[i] << endl;
    else
      cout << a[i] << " ";
  }
  for (int i = 1; i < n; i++) {
    int v = a[i];
    int j = i - 1;
    while (j >= 0 && a[j] > v) {
      a[j + 1] = a[j];
      j--;
    }
    a[j + 1] = v;
    for (int i = 0; i < n; i++) {
      if (i == n - 1)
        cout << a[i] << endl;
      else
        cout << a[i] << " ";
    }
  }
}


Error: -6
double free or corruption (out)


Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 1; i < n; i++) {
    int v = a[i];
    int j = i - 1;
    while (j >= 0 && a[j] > v) {
      a[j + 1] = a[j];
      j--;
    }
    a[j] = v;
    for (int i = 0; i < n; i++) {
      if (i == n - 1)
        cout << a[i] << endl;
      else
        cout << a[i] << " ";
    }
  }
}
Problem p02255 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  vector<int> v1(v);
  sort(v1.begin(), v1.end());

  auto show = [&]() {
    for (int i = 0; i < n; i++) {
      string s = (i == n - 1) ? "\\n" : " ";
      cout << v[i] << s;
    }
  };

  for (int i = 1; i < n; i++) {
    show();
    if (v == v1) {
      return 0;
    }
    if (v[i - 1] < v[i]) {
      continue;
    }
    // int temp = v[i];
    for (int j = i; j > 0; j--) {
      if (v[j - 1] > v[j]) {
        swap(v[j - 1], v[j]);
      }
    }
  }
  show();
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  vector<int> v1(v);
  sort(v1.begin(), v1.end());

  auto show = [&]() {
    for (int i = 0; i < n; i++) {
      string s = (i == n - 1) ? "\\n" : " ";
      cout << v[i] << s;
    }
  };
  while (true) {

    for (int i = 1; i < n; i++) {
      show();
      if (v == v1) {
        return 0;
      }
      if (v[i - 1] < v[i]) {
        continue;
      }
      // int temp = v[i];
      for (int j = i; j > 0; j--) {
        if (v[j - 1] > v[j]) {
          swap(v[j - 1], v[j]);
        }
      }
    }
  }
  return 0;
}
Problem p02255 (C++) — buggy submission, verdict: Time Limit Exceeded

/*Insertion sort*/
#include <stdio.h>

int main() {
  int n, i, j;
  scanf("%d", &n);
  int a[n];
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  printf("%d", a[0]);
  for (i = 1; i < n; i++) {
    printf(" %d", a[i]);
  }
  printf("\\n");
  for (i = 0; i < n - 1; i++) {
    j = i + 1;
    while (j > 0 && a[j] < a[j - 1]) {
      a[j - 1] ^= a[j];
      a[j] ^= a[j - 1];
      a[j - 1] ^= a[j];
      j--;
    }
    printf("%d", a[0]);
    for (int k = 1; k < n; k++) {
      printf(" %d", a[k]);
    }
    printf("\\n");
  }
  return 0;
}

Error: TLE

Fixed submission:

/*Insertion sort*/
#include <stdio.h>

int main() {
  int n, i, j;
  while (scanf("%d", &n)) {
    int a[n];
    for (i = 0; i < n; i++) {
      scanf("%d", &a[i]);
    }
    for (i = 0; i < n; i++) {
      printf("%d ", a[i]);
    }
    printf("\\n");
    for (i = 0; i < n - 1; i++) {
      j = i + 1;
      while (j > 0 && a[j] < a[j - 1]) {
        a[j - 1] ^= a[j];
        a[j] ^= a[j - 1];
        a[j - 1] ^= a[j];
        j--;
      }
      for (int k = 0; k < n; k++) {
        printf("%d ", a[k]);
      }
      printf("\\n");
    }
  }
  return 0;
}
Problem p02255 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
#define rep(i, o, u) for (i = o; i <= u; i++)
using namespace std;

int a[101];
int main() {
  int n;
  int i, j;
  cin >> n;
  rep(i, 1, n) cin >> a[i];
  rep(i, 1, n) {
    sort(a + 1, a + 1 + i);
    rep(j, 1, n - 1) cout << a[j] << " ";
    cout << a[j] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#define rep(i, o, u) for (int i = o; i <= u; i++)
using namespace std;

int a[101];
int b[1001];
int main() {
  int n;
  cin >> n;
  rep(i, 1, n) { cin >> a[i]; }
  memset(b, 0, sizeof(b));
  rep(i, 1, n) {
    b[a[i]]++;
    int k = 0;
    rep(j, 1, 1001) {
      int l = b[j];
      while (l > 0) {
        k++;
        if (k != n)
          cout << j << " ";
        else
          cout << j << endl;
        l--;
      }
    }
    if (k < n) {
      rep(j, i + 1, n - 1) cout << a[j] << " ";
      cout << a[n] << endl;
    }
  }
  return 0;
}
Problem p02255 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
void insertionSort(int *, int);

int main() {
  int A[101], N, n;
  scanf("%d", &N);
  for (n = 0; n <= N - 1; n++)
    scanf("%d", &A[n]);
  insertionSort(A, N);
  return 0;
}

void insertionSort(int *a, int n) {
  int x;
  for (x = 0; x <= n - 2; x++)
    printf("%d ", a[x]);
  printf("%d\\n", a[n - 1]);
  int i, j, k;
  for (i = 1; i <= n - 1; i++) {
    j = 0;
    while (j <= i - 1) {
      k = a[i];
      if (a[i] < a[j]) {
        a[i] = a[j];
        a[j] = k;
      }
      j++;
    }

    for (x = 0; x <= n - 2; x++)
      printf("%d ", a[x]);
    printf("%d\\n", a[n - 1]);
  }
}

Error: TLE

Fixed submission:

#include <stdio.h>

void insertionSort(int *, int);

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

  return 0;
}

// insertionSort(A, N) // N??????????´??????????0-?????????????????????A
//  for i ??? 1 ?????? N-1 ?????§
// v = A[i]
//   j = i - 1
//     while j >= 0 ?????? A[j] > v
//   A[j+1] = A[j]
//     j--
//     A[j+1] = v

void insertionSort(int a[], int n) {
  int i, j, v;
  for (i = 1; i <= n - 1; i++) {
    v = a[i];
    j = i - 1;
    while (j >= 0 && a[j] > v) {
      a[j + 1] = a[j];
      j--;
    }
    a[j + 1] = v;
    int x = 0;
    while (x <= n - 2) {
      printf("%d ", a[x]);
    }
    printf("%d\\n", a[n - 1]);
  }
}
Problem p02255 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

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

  for (int i = 0; i < n; i++) {
    int v = k[i];
    int j = i - 1;
    while (j >= 0 && k[j] > v) {
      k[j + 1] = k[j];
      j--;
    }
    k[j + 1] = v;
    for (int m = 0; m < n; m++) {
      cout << k[m] << (m + 1 < n ? " " : "\\n");
      // printf("%d",k[m]);
      // printf(" ");
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

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

  for (int i = 0; i < n; i++) {
    int v = k[i];
    int j = i - 1;
    while (j >= 0 && k[j] > v) {
      k[j + 1] = k[j];
      j--;
    }
    k[j + 1] = v;
    for (int m = 0; m < n; m++) {
      cout << k[m] << (m + 1 < n ? " " : "\\n");
      // printf("%d",k[m]);
      // printf(" ");
    }
  }
  return 0;
}
Problem p02255 (C++) — buggy submission, verdict: Time Limit Exceeded

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

vector<int> a;

void print_a() {
  for (auto e : a) {
    cout << e;
    if (e != a.back())
      cout << " ";
  }
}
int main() {
#ifdef LOCAL
  // 以降 cin の入力元が 'xxx.txt' になる
  std::ifstream in("aizu_code.txt");
  std::cin.rdbuf(in.rdbuf());
#endif

  int n;
  cin >> n;

  a.resize(n);

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

  for (int i = 0; i < n; i++) {
    int v = a[i];
    int j = i - 1;
    while (j >= 0 && a[j] > v) {
      a[j + 1] = a[j];
      j--;
    }
    a[j + 1] = v;
    print_a();
    cout << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  // ??\\??? cin ?????\\????????? 'xxx.txt' ?????????
  std::ifstream in("alds1_1_a.txt");
  std::cin.rdbuf(in.rdbuf());

  int n;
  cin >> n;
  int A[n];
  int j;

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

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

    for (int k = 0; k < n; k++) {
      cout << A[k] << ",";
    }
    cout << endl;
  }

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

#include <iostream>
#include <vector>

using namespace std;

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

  int N;
  cin >> N;

  vector<int> A(N, 0);

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

  for (int i = 1; i <= A.size(); ++i) {
    for (int i = 0; i < N; ++i) {
      cout << A[i];
      if (i != N - 1)
        cout << " ";
    }
    cout << endl;

    int key = A[i];
    int j = i - 1;

    while (j >= 0 && A[j] > key) {
      A[j + 1] = A[j];
      j--;
    }

    A[j + 1] = key;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N;
  vector<int> A;

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

  for (int i = 1; i <= A.size() - 1; ++i) {
    int key = A[i];
    int j = i - 1;

    while (j >= 0 && A[j] > key) {
      A[j + 1] = A[j];
      j--;
    }

    A[j + 1] = key;

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

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

#include <iostream>

using namespace std;

int main() {
  int m, a[1000], j, i;

  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> a[i];
  }
  for (int k = 0; k < m; k++) {
    if (k == 0) {
      cout << a[k] << flush;
    } else {
      cout << " " << a[k] << flush;
    }
  }
  cout << endl;
  for (j = 1; j < m; j++) {
    int key = a[j];
    // cout<<"key:"<<key;
    for (i = j - 1; i >= 0 && a[i] > key; i--) {
      a[i + 1] = a[i];
      // cout<<"key:"<<a[i+1]<<endl;
    }
    // cout<<"key:"<<key;
    a[i + 1] = key;
    // cout<<"key:"<<a[i+1]<<endl;
    for (int n = 0; n < m; n++) {
      if (n > 0)
        cout << " ";
      cout << a[n];
    }
    cout << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int m, a[105], key, i, j;

  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> a[i];
  }
  for (j = 1; j < m; j++) {
    key = a[j];
    for (i = j - 1; i >= 0, a[i] > key; i--) {
      a[i + 1] = a[i];
    }
    a[i + 1] = key;
    for (int i = 0; i < m; i++) {
      if (i > 0)
        cout << " ";
      cout << a[i];
    }
    cout << endl;
  }

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

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

int main(void) {
  int i = 0;
  int k = 0;
  int j = 0;
  int n;
  int input;
  int *num;
  int temp;

  scanf("%d", &n);
  num = (int *)malloc(n * sizeof(int));

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

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

  for (i = 1; i < n; i++) {
    for (k = i; k > 0; k--) {
      if (num[k] < num[k - 1]) {
        temp = num[k - 1];
        num[k - 1] = num[k];
        num[k] = temp;
      } else {
        break;
      }
    }
    for (j = 0; j < n - 1; j++) {
      printf("%d ", num[j]);
    }
    printf("%d", num[n - 1]);
    printf("\\n");
  }
}

Error: -11

Fixed submission:

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

int main(void) {
  int i = 0;
  int k = 0;
  int j = 0;
  int n;
  int input;
  int *num;
  int temp;

  scanf("%d", &n);
  num = (int *)malloc(n * sizeof(int));

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

  for (i = 1; i < n; i++) {
    for (k = i; k >= 0; k++) {
      if (num[k] < num[k - 1]) {
        temp = num[k - 1];
        num[k - 1] = num[k];
        num[k] = temp;
      }
      for (j = 0; j < n; j++) {
        printf("%d ", num[j]);
      }
      printf("\\n");
    }
  }
}