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

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

using namespace std;

typedef vector<string> card_list;

bool comp(string a, string b) {
  int x = static_cast<int>(a[1]) - 48;
  int y = static_cast<int>(b[1]) - 48;
  return x < y;
}

int bubbleSort(card_list &A) {
  int N = A.size();
  int c = 0;
  for (int i = 0; i < N; ++i) {
    for (int j = N - 1; j > i; --j) {
      if (comp(A[j], A[j - 1])) {
        swap(A[j], A[j - 1]);
        c++;
      }
    }
  }
  return c;
}

int selectionSort(card_list &A) {
  int N = A.size();
  int c = 0;
  for (int i = 0; i < N; ++i) {
    int min = i;
    for (int j = i; j < N; ++j) {
      if (comp(A[j], A[min])) {
        min = j;
      }
    }
    if (i != min) {
      swap(A[i], A[min]);
      c++;
    }
  }
  return c;
}

void print_card_list(card_list &cards) {
  card_list::iterator it = cards.begin();
  for (; it != cards.end() - 1; ++it) {
    cout << *it << " ";
  }
  cout << *it << endl;
}

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

  int a[n];
  card_list A;
  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;
    A.push_back(s);
  }
  card_list B = A;

  bubbleSort(A);
  print_card_list(A);
  cout << "Stable" << endl;

  selectionSort(B);
  print_card_list(B);
  if (equal(A.begin(), A.end(), B.begin())) {
    cout << "Stable";
  } else {
    cout << "Not stable";
  }
  cout << endl;

  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

typedef vector<string> card_list;

bool comp(string a, string b) {
  int x = static_cast<int>(a[1]) - 48;
  int y = static_cast<int>(b[1]) - 48;
  return x < y;
}

int bubbleSort(card_list &A) {
  int N = A.size();
  int c = 0;
  for (int i = 0; i < N; ++i) {
    for (int j = N - 1; j > i; --j) {
      if (comp(A[j], A[j - 1])) {
        swap(A[j], A[j - 1]);
        c++;
      }
    }
  }
  return c;
}

int selectionSort(card_list &A) {
  int N = A.size();
  int c = 0;
  for (int i = 0; i < N; ++i) {
    int min = i;
    for (int j = i; j < N; ++j) {
      if (comp(A[j], A[min])) {
        min = j;
      }
    }
    if (i != min) {
      swap(A[i], A[min]);
      c++;
    }
  }
  return c;
}

void print_card_list(card_list &cards) {
  card_list::iterator it = cards.begin();
  for (; it != cards.end() - 1; ++it) {
    cout << *it << " ";
  }
  cout << *it << endl;
}

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

  int a[n];
  card_list A;
  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;
    A.push_back(s);
  }
  card_list B = A;

  bubbleSort(A);
  print_card_list(A);
  cout << "Stable" << endl;

  selectionSort(B);
  print_card_list(B);
  if (equal(A.begin(), A.end(), B.end())) {
    cout << "Stable";
  } else {
    cout << "Not stable";
  }
  cout << endl;

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

#include <iostream>
using namespace std;

struct Card {
  char suit, value;
};

void bubble(struct 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 selection(struct 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(struct 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(struct Card C1[], struct Card C2[], int N) {
  for (int i = 0; i < N; i++) {
    if (C1[i].suit != C2[i].suit)
      return false;
  }
  return true;
}

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

  bubble(C1, N);
  selection(C2, N);

  print(C1, N);
  cout << "Stable" << endl;
  print(C2, N);
  cout << (isStable(C1, C2, N) ? "Stable" : "Not stable") << endl;
}


Error: -11

Fixed submission:

#include <cstring>
#include <iostream>

using namespace std;

struct Card {
  char symbol;
  int number;
};

void outCards(Card C[], int N) {
  for (int i = 0; i < N; i++) {
    if (i)
      cout << " ";
    cout << C[i].symbol << C[i].number;
  }
  cout << endl;
}

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

void BubbleSort(Card C[], int N) {
  Card temp;
  for (int i = 0; i < N; i++) {
    for (int j = N; j >= i; j++) {
      if (C[j].number < C[j - 1].number) {
        temp = C[j];
        C[j] = C[j - 1];
        C[j - 1] = temp;
      }
    }
  }
  outCards(C, N);
  cout << "Stable" << endl;
}

void SelectionSort(Card C[], int N) {
  Card temp;
  for (int i = 0; i < N; i++) {
    int minj = i;
    for (int j = i; j < N; j++) {
      if (C[j].number < C[minj].number)
        minj = j;
    }
    temp = C[i];
    C[i] = C[minj];
    C[minj] = temp;
  }
  outCards(C, N);
}

int main() {
  int N;
  cin >> N;
  Card C1[N], C2[N];
  string s;
  for (int i = 0; i < N; i++) {
    cin >> s;
    C1[i].symbol = s[0];
    C1[i].number = s[1] - '0';
    C2[i].symbol = s[0];
    C2[i].number = s[1] - '0';
  }
  BubbleSort(C1, N);
  SelectionSort(C2, N);

  cout << (isStable(C1, C2, N) ? "Stable" : "Not Stable") << endl;

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

#include <iostream>
using namespace std;

struct Card {
  char suit, value;
};

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

void selection(struct Card a[], int n) {
  int minj;
  Card hoge;
  for (int i = 0; i < n; i++) {
    minj = i;
    for (int j = i; j < n; j++) {
      if (a[j].value < a[minj].value)
        minj = j;
    }
    hoge = a[i];
    a[i] = a[minj];
    a[minj] = hoge;
  }
}
void print(struct 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 stable(struct Card c1[], struct 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[40], c2[40];
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> c1[i].suit >> c1[i].value;
    c2[i] = c1[i];
  }
  bubble(c1, n);
  selection(c2, n);
  print(c1, n);
  cout << "Stable" << endl;
  print(c2, n);
  if (stable(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 suit, value;
};

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

void selection(struct Card a[], int n) {
  int minj;
  Card hoge;
  for (int i = 0; i < n; i++) {
    minj = i;
    for (int j = i; j < n; j++) {
      if (a[j].value < a[minj].value)
        minj = j;
    }
    hoge = a[i];
    a[i] = a[minj];
    a[minj] = hoge;
  }
}
void print(struct 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 stable(struct Card c1[], struct 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[40], c2[40];
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> c1[i].suit >> c1[i].value;
    c2[i] = c1[i];
  }
  bubble(c1, n);
  selection(c2, n);
  print(c1, n);
  cout << "Stable" << endl;
  print(c2, n);
  if (stable(c1, c2, n))
    cout << "Stable" << endl;
  else
    cout << "Not stable" << endl;

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

#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
struct Card {
  char suit, value;
};

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

void SelectionSort(struct Card C[], int n) {
  Card tmp;
  for (int i = 0; i < n; i++) {
    int min = i;
    for (int j = i; j < n; j++) {
      if (C[j].value < C[min].value) {
        min = j;
      }
    }
    tmp = C[i];
    C[i] = C[min];
    C[min] = tmp;
  }
}

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

bool isStable(struct Card C1[], struct 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 <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
struct Card {
  char suit, value;
};

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

void SelectionSort(struct Card C[], int n) {
  Card tmp;
  for (int i = 0; i < n; i++) {
    int min = i;
    for (int j = i; j < n; j++) {
      if (C[j].value < C[min].value) {
        min = j;
      }
    }
    tmp = C[i];
    C[i] = C[min];
    C[min] = tmp;
  }
}

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

bool isStable(struct Card C1[], struct 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

/*
 * ITP1_6_B.cpp
 *
 *  Created on: Apr 20, 2018
 *      Author: 13743
 */

#include <cmath>
#include <iostream>

using namespace std;

struct card {
  char suit;
  int value;
};

void swap(card *a, int i, int j) {
  card tmp = a[i];
  a[i] = a[j];
  a[j] = tmp;
}

void printCard(card *a, int n) {
  cout << a[0].suit << a[0].value;
  for (int i = 1; i < n; i++) {
    cout << " " << a[i].suit << a[i].value;
  }
  cout << endl;
}

int main() {
  int n;
  card a[36];
  card b[36];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i].suit >> a[i].value;
    b[i].suit = a[i].suit;
    b[i].value = a[i].value;
  }

  for (int i = 0; i < n; i++) {
    for (int j = n - 1; j > i; j--) {
      if (a[j - 1].value > a[j].value) {
        swap(a, j - 1, j);
      }
    }
  }
  printCard(a, n);
  cout << "Stable" << endl;

  for (int i = 0; i < n; i++) {
    int mini = i;
    for (int j = i + 1; j < n; j++) {
      if (b[j].value < b[mini].value) {
        mini = j;
      }
    }
    swap(b, i, mini);
  }
  printCard(b, n);
  for (int i = 0; i < n; i++) {
    if (a[i].suit != b[i].suit) {
      cout << "Not stable" << endl;
      return 0;
    }
  }
  cout << "Stable" << endl;
}


Error: -11

Fixed submission:

/*
 * ITP1_6_B.cpp
 *
 *  Created on: Apr 20, 2018
 *      Author: 13743
 */

#include <cmath>
#include <iostream>

using namespace std;

struct card {
  char suit;
  int value;
};

void swap(card *a, int i, int j) {
  card tmp = a[i];
  a[i] = a[j];
  a[j] = tmp;
}

void printCard(card *a, int n) {
  cout << a[0].suit << a[0].value;
  for (int i = 1; i < n; i++) {
    cout << " " << a[i].suit << a[i].value;
  }
  cout << endl;
}

int main() {
  int n;
  card a[36];
  card b[36];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i].suit >> a[i].value;
    b[i].suit = a[i].suit;
    b[i].value = a[i].value;
  }

  for (int i = 0; i < n; i++) {
    for (int j = n - 1; j > i; j++) {
      if (a[j - 1].value > a[j].value) {
        swap(a, j - 1, j);
      }
    }
  }
  printCard(a, n);
  //	cout << "Stable" << endl;
  //
  //	for(int i=0; i<n; i++) {
  //		int mini = i;
  //		for(int j=i+1; j<n; j++) {
  //			if(b[j].value < b[mini].value) {
  //				mini = j;
  //			}
  //			swap(b, i, mini);
  //		}
  //	}
  //	printCard(b, n);
  //	for(int i=0; i<n; i++) {
  //		if(a[i].suit != b[i].suit) {
  //			cout << "Not Stable" << endl;
  //			return 0;
  //		}
  //	}
  //	cout << "Stable" << endl;
}
Problem p02261 (C++) — buggy submission, verdict: Runtime Error

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

struct Card {
  char suit;
  int num;
};

void printArray(std::vector<Card> cards) {
  int n = (int)cards.size();
  for (int i = 0; i < n; i++) {
    std::cout << cards[i].suit << cards[i].num;
    if (i < n - 1)
      std::cout << " ";
    else
      std::cout << std::endl;
  }
}

void bubbleSort(std::vector<Card> &a) {
  bool flag = true;
  int count = 0;
  while (flag) {
    flag = false;
    for (int i = (int)a.size() - 1; i >= 0; i--) {
      if (a[i].num < a[i - 1].num) {
        std::swap(a[i], a[i - 1]);
        count++;
        flag = true;
      }
    }
  }
}

void selectionSort(std::vector<Card> &a) {
  int min, n = (int)a.size(), count = 0;
  for (int i = 0; i < n; i++) {
    min = i;
    for (int j = i; j < n; j++) {
      if (a[j].num < a[min].num) {
        min = j;
      }
    }
    if (i != min) {
      std::swap(a[i], a[min]);
      count++;
    }
  }
}

int main() {
  int n;
  std::cin >> n;
  std::vector<Card> cards(n), cards2(n);
  char suit;
  int num;
  for (int i = 0; i < n; i++) {
    std::cin >> suit >> num;
    Card c = {suit, num};
    cards[i] = c;
    cards2[i] = c;
  }

  bubbleSort(cards);
  printArray(cards);
  std::cout << "Stable" << std::endl;

  selectionSort(cards2);
  printArray(cards2);
  for (int i = 0; i < n; i++) {
    if (cards[i].suit != cards2[i].suit) {
      std::cout << "Not stable" << std::endl;
      return 0;
    }
  }
  std::cout << "Stable" << std::endl;
}


Error: 1

Fixed submission:

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

struct Card {
  char suit;
  int num;
};

void printArray(std::vector<Card> cards) {
  int n = (int)cards.size();
  for (int i = 0; i < n; i++) {
    std::cout << cards[i].suit << cards[i].num;
    if (i < n - 1)
      std::cout << " ";
    else
      std::cout << std::endl;
  }
}

void bubbleSort(std::vector<Card> &a) {
  bool flag = true;
  int count = 0;
  while (flag) {
    flag = false;
    for (int i = (int)a.size() - 1; i >= 0; i--) {
      if (a[i].num < a[i - 1].num) {
        std::swap(a[i], a[i - 1]);
        count++;
        flag = true;
      }
    }
  }
}

void selectionSort(std::vector<Card> &a) {
  int min, n = (int)a.size(), count = 0;
  for (int i = 0; i < n; i++) {
    min = i;
    for (int j = i; j < n; j++) {
      if (a[j].num < a[min].num) {
        min = j;
      }
    }
    if (i != min) {
      std::swap(a[i], a[min]);
      count++;
    }
  }
}

int main() {
  int n;
  std::cin >> n;
  std::vector<Card> cards(n), cards2(n);
  char suit, num;
  for (int i = 0; i < n; i++) {
    std::cin >> suit >> num;
    Card c = {suit, num};
    cards[i] = c;
    cards2[i] = c;
  }

  bubbleSort(cards);
  printArray(cards);
  std::cout << "Stable" << std::endl;

  selectionSort(cards2);
  printArray(cards2);
  for (int i = 0; i < n; i++) {
    if (cards[i].suit != cards2[i].suit) {
      std::cout << "Not Stable" << std::endl;
      return 1;
    }
  }
  std::cout << "Stable" << std::endl;
}
Problem p02261 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

struct Card {
  char suit, value;
};

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

void selection(struct 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(struct 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(struct Card C1[], struct 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];

  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>
using namespace std;

struct Card {
  char suit, value;
};

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

void selection(struct 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(struct 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(struct Card C1[], struct 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];

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

#include <iostream>
using namespace std;

struct Card {
  char value;
  char suit;
};

void swap(struct Card a[], int i, int j) {
  Card tmp = a[i];
  a[i] = a[j];
  a[j] = tmp;
}

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

void selectionSort(struct 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;
    }
    swap(a, i, minj);
  }
}

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

int main() {

  int n;
  cin >> n;

  Card *card1 = new Card[n];
  for (int i = 0; i < n; ++i) {
    cin >> card1[i].suit >> card1[i].value;
  }

  Card *card2 = new Card[n];
  for (int i = 0; i < n; ++i) {
    card2[i] = card1[i];
  }

  bubbleSort(card1, n);

  for (int i = 0; i < n; ++i) {
    if (0 != i)
      cout << " ";
    cout << card1[i].suit << card1[i].value;
  }
  cout << endl;
  cout << "Stable" << endl;

  selectionSort(card2, n);

  for (int i = 0; i < n; ++i) {
    if (0 != i)
      cout << " ";
    cout << card2[i].suit << card2[i].value;
  }
  cout << endl;
  print(card1, card2, n);

  delete[] card1;
  delete[] card2;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

class Card {
public:
  Card() {
    value = 0;
    str = "";
  }
  int value;
  string str;
};

void swap(Card *a, int i, int j) {
  Card tmp = a[i];
  a[i] = a[j];
  a[j] = tmp;
}

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

void selectionSort(Card *a, int n, int &ans) {
  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;
    }
    swap(a, i, minj);
    if (i == minj)
      ++ans;
  }
}

void print(int ans) {
  if (ans % 2 == 0) {
    cout << "Stable" << endl;
  } else {
    cout << "Not stable" << endl;
  }
}

int main() {

  int n;
  cin >> n;

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

  Card *card = new Card[n];
  for (int i = 0; i < n; ++i) {
    card[i].str = a[i];
    card[i].value = a[i].at(1);
  }

  int ans = 0;
  bubbleSort(card, n, ans);

  int _n = n - 1;
  for (int i = 0; i < n; ++i) {
    if (i != _n)
      cout << card[i].str << " ";
    else
      cout << card[i].str << endl;
  }
  print(ans);

  for (int i = 0; i < n; ++i) {
    card[i].str = a[i];
    card[i].value = a[i].at(1);
  }

  ans = 0;
  selectionSort(card, n, ans);

  for (int i = 0; i < n; ++i) {
    if (i != _n)
      cout << card[i].str << " ";
    else
      cout << card[i].str << endl;
  }
  print(ans);

  delete[] a;
  delete[] card;
  return 0;
}