Problem p02261 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <utility>
#include <vector>

void print(std::vector<std::pair<int, char>> &readVec) {
  int n = readVec.size();
  for (int i = 0; i < n; ++i) {
    std::cout << readVec.at(i).second << readVec.at(i).first;
    if (n - 1 != i) {
      std::cout << " ";
    } else {
      std::cout << std::endl;
    }
  }
}

bool isStable(std::multimap<int, char> &readMmp,
              std::vector<std::pair<int, char>> &readVec) {
  int n = readMmp.size(), m = readVec.size();

  if (n != m) {
    return false;
  }

  bool flg = true;
  auto mmpItr = readMmp.begin();
  auto vecItr = readVec.begin();
  while (flg && readMmp.end() != mmpItr && readVec.end() != vecItr) {
    flg = mmpItr->first == vecItr->first && mmpItr->second == vecItr->second;
    ++mmpItr;
    ++vecItr;
  }

  if (flg) {
    std::cout << "Stable" << std::endl;
  } else {
    std::cout << "Not stable" << std::endl;
  }

  return true;
}

int bubbleSort(std::vector<std::pair<int, char>> &readVec) {
  int n = readVec.size(), count = 0;
  bool flg =
      true; // existence of the left-adjacent element less than an interest one.
  while (flg) {
    flg = false;
    for (int j = n - 1; 0 < j; --j) {
      if (readVec.at(j).first < readVec.at(j - 1).first) {
        std::swap(readVec.at(j), readVec.at(j - 1));
        flg = true;
        ++count;
      }
    }
  }

  print(readVec);

  return count;
}

int selectionSort(std::vector<std::pair<int, char>> &readVec) {
  int n = readVec.size(), count = 0;
  for (int i = 0; i < n; ++i) {
    int minJ = i;
    for (int j = i; j < n; ++j) {
      if (readVec.at(j).first < readVec.at(minJ).first) {
        minJ = j;
      }
    }

    if (i != minJ) {
      std::swap(readVec.at(i), readVec.at(minJ));
      ++count;
    }
  }

  print(readVec);

  return count;
}

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

  std::vector<std::pair<int, char>> a(n);
  std::multimap<int, char> mmp;
  for (int i = 0; i < n; ++i) {
    std::string str;
    std::cin >> str;
    a.at(i) = std::make_pair(str.at(1) - 48, str.at(0));
    mmp.insert(std::make_pair(str.at(1) - 48, str.at(0)));
  }

  auto bubbled = a, selected = a;

  bubbleSort(bubbled);
  isStable(mmp, bubbled);

  selectionSort(selected);
  isStable(mmp, selected);

  return 0;
}


Error: TLE

Fixed submission:

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

void showVector(std::vector<std::string> &readVec) {
  int n = readVec.size();
  for (int i = 0; i < n; ++i) {
    std::cout << readVec.at(i);
    if (n - 1 != i) {
      std::cout << " ";
    } else {
      std::cout << std::endl;
    }
  }
}

bool isStable(std::vector<std::string> &readBubble,
              std::vector<std::string> &readSelect) {
  int n = readBubble.size();
  for (int i = 0; i < n; ++i) {
    if (readBubble.at(i) != readSelect.at(i)) {
      return false;
    }
  }

  return true;
}

int selectionSort(std::vector<std::string> &readVec) {
  int n = readVec.size();
  int count = 0;
  for (int i = 0; i < n; ++i) {
    int tmp = i;
    for (int j = i; j < n; ++j) {
      if (readVec.at(j).at(1) < readVec.at(tmp).at(1)) {
        tmp = j;
      }
    }

    if (tmp != i) {
      std::string buf;
      buf = readVec.at(i);
      readVec.at(i) = readVec.at(tmp);
      readVec.at(tmp) = buf;

      ++count;
    }
  }

  return 0;
}

int bubbleSort(std::vector<std::string> &readVec) {
  bool flg = true;
  int i, tmp, count = 0;
  int n = readVec.size();
  while (flg) {
    flg = false;
    for (i = n - 1; 0 < i; --i) {
      std::cout << &readVec.at(i).at(1) << ", " << &readVec.at(i - 1).at(1)
                << std::endl;
      if (&readVec.at(i).at(1) < &readVec.at(i - 1).at(1)) {
        std::string buf;
        buf = readVec.at(i - 1);
        readVec.at(i - 1) = readVec.at(i);
        readVec.at(i) = buf;
        flg = true;
        ++count;
      }
    }
  }
  return count;
}

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

  std::vector<std::string> a(n);
  for (int i = 0; i < n; ++i) {
    std::cin >> a.at(i);
  }

  std::vector<std::string> bubbled = a;
  bubbleSort(bubbled);

  std::vector<std::string> selected = a;
  selectionSort(selected);

  showVector(bubbled);
  //    std::cout << "Stable" << std::endl;
  //    showVector(selected);
  if (isStable(bubbled, selected)) {
    //        std::cout << "Stable" << std::endl;
  } else {
    //        std::cout << "Not Stable" << std::endl;
  }

  return 0;
}