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

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int n;
string a[36], b[36];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i], b[i] = a[i];
  for (int i = 0; i < n; i++) {
    for (int j = n - 1; j > i; j--) {
      if (b[j - 1][1] > b[j][1])
        swap(b[j - 1], b[j]);
    }
  }
  for (int i = 0; i < n; i++)
    cout << (i ? " " : "") << b[i];
  cout << endl;
  cout << "Stable" << endl;
  for (int i = 0; i < n; i++) {
    int r = i;
    for (int j = i + 1; j < n; j++) {
      if (a[j][1] < a[r][1])
        r = j;
    }
    swap(a[i], a[r]);
  }
  for (int i = 0; i < n; i++)
    cout << (i ? " " : "") << a[i];
  cout << endl;
  cout << (equal(a, a + n, b) ? "Stable" : "Not stable") << endl;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int n;
string a[36], b[36];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i], b[i] = a[i];
  for (int i = 0; i < n; i++) {
    for (int j = n - 1; j > i; j++) {
      if (b[i][1] > b[j][1])
        swap(b[i], b[j]);
    }
  }
  for (int i = 0; i < n; i++)
    cout << (i ? " " : "") << b[i];
  cout << endl;
  cout << "Stable" << endl;
  for (int i = 0; i < n; i++) {
    int r = i;
    for (int j = i + 1; j < n; j++) {
      if (a[j][1] < a[r][1])
        r = j;
    }
    swap(a[i], a[r]);
  }
  for (int i = 0; i < n; i++)
    cout << (i ? " " : "") << a[i];
  cout << endl;
  cout << (equal(a, a + n, b) ? "Stable" : "Not stable") << endl;
}
Problem p02261 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define rep(i, a) for (i = 0; i < a; i++)
using namespace std;

class Card {
public:
  string suit;
  int value;
  void InputCard(string T);
};

void Card::InputCard(string T) {
  suit = T[0];
  value = T[1] - '0';
}

void print(Card card[], int n) {
  int i;
  rep(i, n) {
    if (i > 0) {
      cout << " ";
    }
    cout << card[i].suit << card[i].value;
  }
  cout << endl;
}

void bubblesort(Card card[], int n) {
  int i;
  rep(i, n) {
    for (int j = n - 1; j > i; j--) {
      if (card[j].value < card[j - 1].value) {
        swap(card[j], card[j - 1]);
      }
    }
  }
}

void selectionsort(Card card[], int n) {
  int i;
  rep(i, n) {
    int minj = i;
    for (int j = i; j < n; j++) {
      if (card[j].value < card[minj].value) {
        minj = j;
      }
    }
    swap(card[i], card[minj]);
  }
}

bool isStable(Card card1[], Card card2[], int n) {
  int i;
  rep(i, n) {
    if (card1[i].suit != card2[i].suit) {
      return false;
    }
  }
  return true;
}

int main(void) {
  int i, n;
  string T;
  cin >> n;
  Card *Card1 = new Card[n];
  Card *Card2 = new Card[n];
  rep(i, n) {
    cin >> T;
    Card1[i].InputCard(T);
    Card2[i].InputCard(T);
  }

  bubblesort(Card1, n);
  selectionsort(Card2, n);

  print(Card1, n);
  cout << "Stable" << endl;
  print(Card2, n);

  if (isStable(Card1, Card2, n)) {
    cout << "Stable" << endl;
  } else {
    cout << "Not stable" << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#define rep(i, a) for (i = 0; i < a; i++)
using namespace std;

class Card {
public:
  string suit;
  int value;
  void InputCard(string T);
};

void Card::InputCard(string T) {
  suit = T[0];
  value = T[1] - '0';
}

void print(Card card[], int n) {
  int i;
  rep(i, n) {
    if (i > 0) {
      cout << " ";
    }
    cout << card[i].suit << card[i].value;
  }
  cout << endl;
}

void bubblesort(Card card[], int n) {
  int i;
  rep(i, n) {
    for (int j = n; j > i; j--) {
      if (card[j].value < card[j - 1].value) {
        swap(card[j], card[j - 1]);
      }
    }
  }
}

void selectionsort(Card card[], int n) {
  int i;
  rep(i, n) {
    int minj = i;
    for (int j = i; j < n; j++) {
      if (card[j].value < card[j - 1].value) {
        minj = j;
      }
    }
    swap(card[i], card[minj]);
  }
}

bool isStable(Card card1[], Card card2[], int n) {
  int i;
  rep(i, n) {
    if (card1[i].suit != card2[i].suit) {
      return false;
    }
  }
  return true;
}

int main(void) {
  int i, n;
  string T;
  cin >> n;
  Card *Card1 = new Card[n];
  Card *Card2 = new Card[n];
  rep(i, n) {
    cin >> T;
    Card1[i].InputCard(T);
    Card2[i].InputCard(T);
  }

  bubblesort(Card1, n);
  selectionsort(Card2, n);

  print(Card1, n);
  cout << "Stable" << endl;
  print(Card2, n);

  if (isStable(Card1, Card2, 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 {
  char suit, value;
};

static const int MAX = 36;

void bubbleSort(struct Card card[], int n) {
  for (int i = 0; i < n - 1; i++) {
    for (int j = n - 1; j >= i + 1; j--) {
      if (card[j].value < card[j - 1].value) {
        swap(card[j], card[j - 1]);
      }
    }
  }
}

void selectionSort(struct Card card[], int n) {
  for (int i = 0; i < n - 1; i++) {
    int minj = i;
    for (int j = i; j < n; j++) {
      if (card[minj].value > card[j].value)
        minj = j;
    }
    swap(card[minj], card[i]);
  }
}

bool isStable(struct Card c1[], Card c2[], int n) {
  for (int i = 0; i < n; i++) {
    if (c1[i].suit != c2[i].suit)
      return false;
  }
  return true;
}

void output(struct Card c[], int n) {
  for (int i = 0; i < n; i++) {
    if (i > 0)
      cout << " ";
    cout << c[i].suit << c[i].value;
  }
  cout << endl;
}

int main() {
  Card c1[MAX], c2[MAX];
  int n;
  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]; // copied

  bubbleSort(c1, n);
  output(c1, n);
  cout << "Stable" << endl;

  selectionSort(c2, n);
  output(c2, n);
  if (isStable(c1, c2, n)) {
    cout << "Stable" << endl;
  } else {
    cout << "Not stable" << endl;
  }
  return 0;
}


Error: -6
free(): invalid pointer


Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>

using namespace std;
static const int MAX = 36;

void trace(string a[], int n) {
  for (int i = 0; i < n; i++) {
    if (i > 0)
      cout << " ";
    cout << a[i];
  }
  cout << endl;
}

void bubbleSort(string r[], int n) {
  for (int i = 0; i < n - 1; i++) {
    for (int j = n - 1; j >= i + 1; j--) {
      if (r[j][1] < r[j - 1][1]) {
        swap(r[j], r[j - 1]);
      }
    }
  }
}

void selectionSort(string r[], int n) {
  for (int i = 0; i < n - 1; i++) {
    int minj = i;
    for (int j = i; j < n; j++) {
      if (r[minj][1] > r[j][1])
        minj = j;
    }
    swap(r[minj], r[i]);
  }
}

bool isStable(string in[], string out[], int n) {
  for (int i = 0; i < n - 1; i++) {
    for (int j = 1; j < n - 1; j++) {
      for (int a = 0; a < n - 1; a++) {
        for (int b = a + 1; b < n - 1; b++) {
          if (in[i][1] == in[j][1]) {
            if (in[i] == out[b] && in[j] == out[a])
              return false;
          }
        }
      }
    }
  }
  return true;
}

// stable sort
int main() {
  int n;
  cin >> n;
  string r[MAX], r1[MAX], r2[MAX];
  for (int i = 0; i < n; i++)
    cin >> r[i];

  memcpy(r1, r, sizeof(r));
  memcpy(r2, r, sizeof(r));

  bubbleSort(r1, n); // stable
  trace(r1, n);
  if (isStable(r, r1, n)) {
    cout << "Stable" << endl;
  } else {
    cout << "Not stable" << endl;
  }

  selectionSort(r2, n); // not stable
  trace(r2, n);
  if (isStable(r, r2, n)) {
    cout << "Stable" << endl;
  } else {
    cout << "Not stable" << endl;
  }

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

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

struct Card {
  char suit;
  int value;
};

void BubbleSort(struct Card c[], int n) {
  for (int i = 0; i < n; ++i) {
    for (int j = n - 1; i < j; --j) {
      if (c[j].value < c[j - 1].value) {
        Card tmp = c[j];
        c[j] = c[j - 1];
        c[j - 1] = tmp;
      }
    }
  }
}

void SelectionSort(struct Card c[], int n) {
  int minj;
  for (int i = 0; i < n - 1; ++i) {
    minj = i;
    for (int j = i; j < n; ++j) {
      if (c[j].value < c[minj].value) {
        minj = j;
      }
    }
    Card tmp = c[i];
    c[i] = c[minj];
    c[minj] = tmp;
  }
}
bool isStable(struct Card c1[], Card c2[], int n) {
  for (int i = 0; i < n; ++i) {
    if (c1[i].suit != c2[i].suit)
      return false;
  }
  return true;
}
void printarr(struct Card c[], int n) {
  for (int i = 0; i < n; ++i) {
    cout << c[i].suit << c[i].value << ((i == n - 1) ? '\\n' : ' ');
  }
}
int main() {
  int n;
  cin >> n;
  Card c1[n], c2[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);
  printarr(c1, n);
  cout << "Stable" << endl;
  printarr(c2, n);
  if (isStable(c1, c2, n)) {
    cout << "Stable" << endl;
  } else {
    cout << "Not stable" << endl;
  }
}


Error: WA

Fixed submission:

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

struct Card {
  char suit;
  int value;
};

void BubbleSort(Card c[], int n) {
  Card tmp;
  for (int i = 0; i < n; ++i) {
    for (int j = n - 1; i < j; --j) {
      if (c[j].value < c[j - 1].value) {
        tmp = c[j];
        c[j] = c[j - 1];
        c[j - 1] = tmp;
      }
    }
  }
}

void SelectionSort(Card c[], int n) {
  Card tmp;
  int minj;
  for (int i = 0; i < n - 1; ++i) {
    for (int j = i; j < n; ++j) {
      if (c[j].value < c[minj].value) {
        minj = j;
      }
    }
    tmp = c[i];
    c[i] = c[minj];
    c[minj] = tmp;
  }
}
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;
}
void printarr(Card c[], int n) {
  for (int i = 0; i < n; ++i) {
    cout << c[i].suit << c[i].value << ((i == n - 1) ? '\\n' : ' ');
  }
}
int main() {
  int n;
  cin >> n;
  Card c1[n], c2[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);
  printarr(c1, n);
  cout << "Stable" << endl;
  SelectionSort(c2, n);
  printarr(c2, n);
  if (isStable(c1, c2, n)) {
    cout << "Stable" << endl;
  } else {
    cout << "Not stable" << endl;
  }
}
Problem p02261 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

struct Card {
  char suit, value;
};

void bubble(struct Card A[], long N) {
  for (long i = 0; i < N; i++) {
    for (long j = N - 1; j >= i; j--) {
      if (A[j].value < A[j - 1].value)
        swap(A[j], A[j - 1]);
    }
  }
}

void selection(struct Card A[], long N) {
  for (long i = 0; i < N; i++) {
    long minj = i;
    for (long j = i; j < N; j++)
      if (A[j].value < A[minj].value)
        minj = j;
    swap(A[i], A[minj]);
  }
}

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

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

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

  long N;
  cin >> N;

  for (long i = 0; i < N; i++)
    cin >> C1[i].suit >> C1[i].value;
  for (long i = 0; i < N; i++)
    C2[i] = C1[i];

  bubble(C1, N);
  selection(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: -11

Fixed submission:

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

struct Trump {
  string suit;
  string num;
};

void BubbleSort(Trump trump[], int n) {
  for (int i = 0; i < n - 1; i++) {
    for (int j = n - 1; j >= i; j--) {
      if (trump[j].num < trump[j - 1].num) {
        swap(trump[j], trump[j - 1]);
      }
    }
  }
}

void SelectionSort(Trump trump[], int n) {
  for (int i = 0; i < n; i++) {
    int minj;
    minj = i;
    for (int j = i; j < n; j++) {
      if (trump[j].num < trump[minj].num) {
        minj = j;
      }
    }
    swap(trump[i], trump[minj]);
  }
}

int main() {
  int n;
  Trump a[36], b[36];
  string c;
  cin >> n;

  // cin
  for (int i = 0; i < n; ++i) {
    cin >> c;
    a[i].suit = c[0];
    a[i].num = c[1];
  }
  for (int j = 0; j < n; ++j) {
    b[j] = a[j];
  }

  // sort
  BubbleSort(a, n);
  SelectionSort(b, n);

  // cout
  for (int i = 0; i < n - 1; ++i) {
    cout << a[i].suit << a[i].num << " ";
  }
  cout << a[n].suit << a[n].num << endl;
  cout << "Stable" << endl;

  for (int i = 0; i < n; ++i) {
    cout << b[i].suit << b[i].num << " ";
  }
  cout << b[n].suit << b[n].num << endl;

  // check "is stable"
  int check;
  for (int i = 0; i < n; ++i) {
    if (a[i].suit != b[i].suit || a[i].num != b[i].num)
      check++;
  }
  if (check == 0) {
    cout << "Stable" << endl;
  } else {
    cout << "Not stable" << endl;
  }

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

/*http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_2_C&lang=jp*/
#include <iostream>
#include <string.h>

#define MAX 36

using namespace std;

struct card {
  char suit;
  int value;
};

void bubbleSort(struct card *a, int n) {
  for (int i = 0; i < n - 1; i++) {
    for (int j = n - 1; i < j; j--) {
      if (a[j].value < a[j - 1].value) {
        swap(a[j], a[j - 1]);
      }
    }
  }
}

void selectionSort(struct card *a, int n) {
  for (int i = 0; i < n - 1; i++) {
    int minj = i;
    for (int j = i + 1; j < n; j++) {
      if (a[j].value < a[minj].value) {
        minj = j;
      }
    }
    swap(a[i], a[minj]);
  }
}

void show(struct 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;
    }
  }
}

void isStable(struct card *a, struct card *b, int n) {
  for (int i = 0; a != nullptr && i < n; i++) {
    if (a[i].suit != b[i].suit) {
      cout << "Not stable" << endl;
      return;
    }
  }
  cout << "Stable" << endl;
}

void isStable(struct card *b) { isStable(nullptr, b, 0); }

int main() {
  int n;
  struct card a[MAX], b[MAX], s[MAX];

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i].suit >> a[i].value;
  }
  memcpy(b, a, sizeof(a));
  bubbleSort(b, n);
  show(b, n);
  isStable(b);
  memcpy(s, a, sizeof(a));
  selectionSort(s, n);
  show(s, n);
  isStable(b, s, n);
  return 0;
}

Error: -6
free(): invalid pointer


Fixed submission:

/*http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_2_C&lang=jp*/
#include <iostream>
#include <string.h>

#define MAX 36

using namespace std;

void bubbleSort(string *a, int n) {
  for (int i = 0; i < n - 1; i++) {
    for (int j = n - 1; i < j; j--) {
      if (atoi(&a[j][1]) < atoi(&a[j - 1][1])) {
        swap(a[j], a[j - 1]);
      }
    }
  }
}

void selectionSort(string *a, int n) {
  for (int i = 0; i < n - 1; i++) {
    int minj = i;
    for (int j = i + 1; j < n; j++) {
      if (atoi(&a[j][1]) < atoi(&a[minj][1])) {
        minj = j;
      }
    }
    swap(a[i], a[minj]);
  }
}

void show(string *a, int n) {
  for (int i = 0; i < n; i++) {
    cout << a[i];
    if (i < n - 1) {
      cout << " ";
    } else {
      cout << endl;
    }
  }
}

void isStable(string *a, string *b, int n) {
  for (int i = 0; a != nullptr && i < n; i++) {
    if (a[i] != b[i]) {
      cout << "Not stable" << endl;
      return;
    }
  }
  cout << "Stable" << endl;
}

void isStable(string *b) { isStable(nullptr, b, 0); }

int main() {
  int n;
  string a[MAX], b[MAX], s[MAX];

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  memcpy(b, a, sizeof(a));
  bubbleSort(b, n);
  show(b, n);
  isStable(b);
  memcpy(s, a, sizeof(a));
  selectionSort(s, n);
  show(s, n);
  isStable(b, s, n);
  return 0;
}
Problem p02261 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
  string card[36], card2[36], tem;
  int n, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> card[i];
    card2[i] = card[i];
  }
  // bubblesort
  for (int i = 0; i <= n - 1; ++i) {
    for (int j = n - 1; j >= i + 1; j--) {
      if (card[j][1] - '0' < card[j - 1][1] - '0') {
        // tem=card[j];
        // card[j]=card[j-1];
        // card[j-1]=tem;
        swap(card[j], card[j - 1]);
      }
    }
  }
  for (int i = 0; i <= n - 1; i++) {
    if (i != n - 1)
      cout << card[i] << " ";
    else
      cout << card[i] << endl;
  }
  cout << "Stable" << endl;
  // selectionsort
  int minj = 0;
  for (int i = 0; i <= n - 1; i++) {
    minj = i;
    for (int j = i; j <= n - 1; j++) {
      if (card2[j][1] - '0' < card2[minj][1] - '0') {
        minj = j;
      }
    }
    if (i != minj) {
      swap(card2[i], card2[minj]);
    }
  }
  // stable or not stable
  for (int i = 0; i < n; i++) {
    if (i != n - 1)
      cout << card2[i] << " ";
    else
      cout << card2[i] << endl;
    if (card[i] == card2[i]) {
      ans++;
    }
  }
  ans == n ? cout << "Stable" << endl : cout << "Not stable" << endl;
  string a;
  cin >> a;
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
  string card[36], card2[36], tem;
  int n, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> card[i];
    // card2[i]=card[i];
  }
  // bubblesort
  for (int i = 0; i <= n - 1; i++) {
    for (int j = n - 1; j <= i + 1; j--) {
      if (card[j][1] - '0' < card[j - 1][1] - '0') {
        tem = card[j];
        card[j] = card[j - 1];
        card[j - 1] = tem;
        // swap(card[j],card[j-1]);
      }
    }
  }
  for (int i = 0; i <= n - 1; i++) {
    if (i != n - 1)
      cout << card[i] << " ";
    else
      cout << card[i] << endl;
  }
  cout << "Stable" << endl;
  // selectionsort
  int minj = 0;
  for (int i = 0; i <= n - 1; i++) {
    minj = i;
    for (int j = i; j <= n - 1; j++) {
      if (card2[j][1] - '0' < card2[minj][1] - '0') {
        minj = j;
        swap(card2[i], card2[minj]);
      }
    }
  }
  // stable or not stable
  for (int i = 0; i < n; i++) {
    if (i != n - 1)
      cout << card2[i] << " ";
    else
      cout << card2[i] << endl;
    if (card[i] == card2[i]) {
      ans++;
    }
  }
  ans == n ? cout << "Stable" << endl : cout << "Not stable" << endl;
  return 0;
}
Problem p02261 (C++) — buggy submission, verdict: Runtime Error

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

void bubbleSort(int *C, char *S, int N) {
  for (int i = 0; i < N; i++) {
    for (int j = N - 1; j > i; j--) {
      if (C[j] < C[j - 1]) {
        int tn = C[j];
        C[j] = C[j - 1];
        C[j - 1] = tn;

        char ts = S[j];
        S[j] = S[j - 1];
        S[j - 1] = ts;
      }
    }
  }
}

void selectionSort(int *C, char *S, int N) {
  for (int i = 0; i < N; i++) {
    int minj = i;
    for (int j = i; j < N; j++) {
      if (C[j] < C[minj]) {
        minj = j;
      }
    }
    int tn = C[i];
    C[i] = C[minj];
    C[minj] = tn;

    char ts = S[i];
    S[i] = S[minj];
    S[minj] = ts;
  }
}

void outputArray(int *C, char *S, int N) {
  printf("%c%d", S[0], C[0]);
  for (int i = 1; i < N; i++)
    printf(" %c%d", S[i], C[i]);
  printf("\\n");
}

int checkCorrect(char *S1, char *S2, int N) {
  int check = 1;
  for (int i = 0; i < N; i++) {
    if (S1[i] != S2[i])
      check = 0;
  }
  return check;
}

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

  char s1[n], s2[n];
  int c1[n], c2[n];
  for (int i = 0; i < n; i++) {
    cin >> s1[i] >> c1[i];
    c2[i] = c1[i];
    s2[i] = s1[i];
  }

  bubbleSort(c1, s1, n);
  outputArray(c1, s1, n);
  printf("Stable\\n");

  selectionSort(c2, s2, n);
  outputArray(c2, s2, n);
  int check = checkCorrect(s1, s2, n);
  if (check)
    printf("Stable\\n");
  else
    printf("Not stable\\n");

  return 0;
}


Error: -11

Fixed submission:

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

void bubbleSort(int *C, char *S, int N) {
  for (int i = 0; i < N; i++) {
    for (int j = N - 1; j > i; j--) {
      if (C[j] < C[j - 1]) {
        int tn = C[j];
        C[j] = C[j - 1];
        C[j - 1] = tn;

        char ts = S[j];
        S[j] = S[j - 1];
        S[j - 1] = ts;
      }
    }
  }
}

void selectionSort(int *C, char *S, int N) {
  for (int i = 0; i < N; i++) {
    int minj = i;
    for (int j = i; j < N; j++) {
      if (C[j] < C[minj]) {
        minj = j;
      }
    }
    int tn = C[i];
    C[i] = C[minj];
    C[minj] = tn;

    char ts = S[i];
    S[i] = S[minj];
    S[minj] = ts;
  }
}

void outputArray(int *C, char *S, int N) {
  printf("%s%d", S[0], C[0]);
  for (int i = 1; i < N; i++)
    printf(" %s%d", S[i], C[i]);
  printf("\\n");
}

int checkCorrect(char *S1, char *S2, int N) {
  int check = 1;
  for (int i = 0; i < N; i++) {
    if (S1[i] != S2[i])
      check = 0;
  }
  return check;
}

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

  char s1[n], s2[n];
  int c1[n], c2[n];
  for (int i = 0; i < n; i++) {
    cin >> s1[i] >> c1[i];
    c2[i] = c1[i];
    s2[i] = s1[i];
  }

  bubbleSort(c1, s1, n);
  outputArray(c1, s1, n);
  printf("Stable\\n");

  selectionSort(c2, s2, n);
  outputArray(c2, s2, n);
  int check = checkCorrect(s1, s2, n);
  if (check)
    printf("Stable");
  else
    printf("Not stable");

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

#include <iostream>

using namespace std;

struct Card {

  char mark;
  int value;
};

void bubbleSort(struct Card C[], int n) {

  int i, j;
  Card v;

  for (i = 0; i < n; i++) {

    for (j = n - 1; j >= i + 1; j--) {

      if (C[j].value < C[j - 1].value) {

        v = C[j];
        C[j] = C[j - 1];
        C[j - 1] = v;
      }
    }
  }
}

void selectionSort(struct Card C[], int n) {

  int i, j, minj;
  Card v;

  for (i = 0; i < n; i++) {

    minj = i;

    for (j = i; j < n; j++) {

      if (C[j].value < C[minj].value) {

        minj = j;
      }
    }

    v = C[i];
    C[i] = C[minj];
    C[minj] = v;
  }
}

void print(struct Card C[], int n) {

  int i;

  for (i = 0; i < n; i++) {

    if (i > 0) {

      cout << " ";
    }

    cout << C[i].mark << C[i].value;
  }

  cout << endl;
}

bool isStable(struct Card C1[], struct Card C2[], int n) {

  int i;

  for (i = 0; i < n; i++) {

    if (C1[i].mark != C2[i].mark) {

      return false;
    }
  }

  return true;
}

int main() {

  Card C1[100], C2[100];
  int i, n;

  cin >> n;

  for (i = 0; i < n; i++) {

    cin >> C1[i].mark >> C1[i].value;
  }

  for (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: -11

Fixed submission:

#include <iostream>

using namespace std;

struct Card {

  char mark, value;
};

void bubbleSort(struct Card C[], int n) {

  int i, j;
  Card v;

  for (i = 0; i < n; i++) {

    for (j = n - 1; j >= i + 1; j++) {

      if (C[j].value < C[j - 1].value) {

        v = C[j];
        C[j] = C[j - 1];
        C[j - 1] = v;
      }
    }
  }
}

void selectionSort(struct Card C[], int n) {

  int i, j, minj;
  Card v;

  for (i = 0; i < n; i++) {

    minj = i;

    for (j = i; j < n; j++) {

      if (C[j].value < C[minj].value) {

        minj = j;
      }
    }

    v = C[i];
    C[i] = C[minj];
    C[minj] = v;
  }
}

void print(struct Card C[], int n) {

  int i;

  for (i = 0; i < n; i++) {

    if (i > 0) {

      cout << " ";
    }

    cout << C[i].mark << C[i].value;
  }

  cout << endl;
}

bool isStable(struct Card C1[], struct Card C2[], int n) {

  int i;

  for (i = 0; i < n; i++) {

    if (C1[i].mark != C2[i].mark) {

      return false;
    }
  }

  return true;
}

int main() {

  Card C1[100], C2[100];
  int i, n;

  cin >> n;

  for (i = 0; i < n; i++) {

    cin >> C1[i].mark >> C2[i].value;
  }

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