Problem p02261 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

void swap(string &card1, string &card2) {
  string tmp = card1;
  card1 = card2;
  card2 = tmp;
}

int peek_number(string card) { return atoi(&card[1]); }

vector<string> bubble_sort(vector<string> cards) {
  for (int i = 0; i < cards.size(); i++) {
    for (int j = cards.size() - 1; j > i; j--) {
      if (peek_number(cards[j]) < peek_number(cards[j - 1]))
        swap(cards[j], cards[j - 1]);
    }
  }

  return cards;
}

vector<string> selection_sort(vector<string> cards) {
  for (int i = 0; i < cards.size(); i++) {
    int minj = i;

    for (int j = i; j < cards.size(); j++) {
      if (peek_number(cards[j]) < peek_number(cards[minj]))
        minj = j;
    }

    swap(cards[i], cards[minj]);
  }

  return cards;
}

bool is_stable(vector<string> stable, vector<string> target) {
  for (int i = 0; i < stable.size(); i++) {
    if (stable[i] != target[i])
      return false;
  }

  return true;
}

void print_cards(vector<string> cards) {
  bool first = true;

  for (int i = 0; i < cards.size(); i++) {
    if (!first)
      cout << " ";
    cout << cards[i];
    first = false;
  }

  cout << endl;
}

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

  vector<string> unsorted(n);
  for (int i = 0; i < n; i++)
    cin >> unsorted[i];

  vector<string> bubble_sorted = bubble_sort(unsorted);
  vector<string> selection_sorted = selection_sort(unsorted);

  print_cards(bubble_sorted);
  cout << "Stable" << endl;

  print_cards(selection_sorted);
  cout << (is_stable(bubble_sorted, selection_sorted) ? "Stable" : "Not stable")
       << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int peek_number(string card) { return atoi(&card[1]); }

vector<string> bubble_sort(vector<string> cards) {
  for (int i = 0; i < cards.size(); i++) {
    for (int j = cards.size() - 1; j >= 0; j--) {
      if (peek_number(cards[j]) < peek_number(cards[j - 1])) {
        string tmp = cards[j];
        cards[j] = cards[j - 1];
        cards[j - 1] = tmp;
      }
    }
  }

  return cards;
}

vector<string> selection_sort(vector<string> cards) {
  for (int i = 0; i < cards.size(); i++) {
    int min_idx = i;

    for (int j = i; j < cards.size(); j++) {
      if (peek_number(cards[j]) < peek_number(cards[min_idx])) {
        min_idx = j;
      }
    }

    string tmp = cards[i];
    cards[i] = cards[min_idx];
    cards[min_idx] = tmp;
  }

  return cards;
}

int index_of(vector<string> v, string target) {
  for (int i = 0; i < v.size(); i++) {
    if (v[i] == target) {
      return i;
    }
  }

  return -1;
}

bool is_stable(vector<string> unsorted, vector<string> sorted) {
  bool result = true;

  for (int i = 0; i < unsorted.size(); i++) {
    for (int j = 0; j < unsorted.size(); j++) {
      if (i == j)
        continue;
      if (peek_number(unsorted[i]) != peek_number(unsorted[j]))
        continue;

      int sorted_i = index_of(sorted, unsorted[i]);
      int sorted_j = index_of(sorted, unsorted[j]);

      if ((i < j && sorted_i < sorted_j) || (i > j && sorted_i > sorted_j))
        continue;

      result = false;
      goto END;
    }
  }

END:
  return result;
}

void print_array(vector<string> v) {
  for (int i = 0; i < v.size(); i++) {
    if (i != 0)
      cout << " ";
    cout << v[i];
  }
  cout << endl;
}

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

  vector<string> cards(n);
  for (int i = 0; i < n; i++)
    cin >> cards[i];

  vector<string> bubble_sorted = bubble_sort(cards);
  vector<string> selection_sorted = selection_sort(cards);

  print_array(bubble_sorted);
  cout << (is_stable(cards, bubble_sorted) ? "Stable" : "Not stable") << endl;

  print_array(selection_sorted);
  cout << (is_stable(cards, selection_sorted) ? "Stable" : "Not stable")
       << endl;

  return 0;
}