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

#include <cstdio>
#include <iostream>
using namespace std;
struct Card {
  char suit;
  int val;
};
int partition(Card *arr, int left, int right) {
  int x = arr[right].val;
  int i, j;
  for (i = left - 1, j = left; j < right; j++) {
    if (arr[j].val <= x)
      swap(arr[++i], arr[j]);
  }
  swap(arr[++i], arr[right]);
  return i;
}
void quickSort(Card *arr, int left, int right) {
  if (left < right) {
    int mid = partition(arr, left, right);
    quickSort(arr, left, mid - 1);
    quickSort(arr, mid + 1, right);
  }
}
void merge(Card *arr, int left, int mid, int right) {
  int len1 = mid - left + 1;
  int len2 = right - mid;
  Card *tempL = new Card[len1];
  Card *tempR = new Card[len2];
  for (int i = 0; i < len1; i++)
    tempL[i] = arr[left + i];
  for (int i = 0; i < len2; i++)
    tempR[i] = arr[mid + 1 + i];
  int i = 0, j = 0;
  for (int k = left; k <= right; k++) {
    if (i < len1 && (j >= len2 || tempL[i].val <= tempR[j].val))
      arr[k] = tempL[i++];
    else
      arr[k] = tempR[j++];
  }
}
void mergeSort(Card *arr, int left, int right) {
  int mid = (left + right) / 2;
  if (left < mid) {
    mergeSort(arr, left, mid);
    mergeSort(arr, mid + 1, right);
  }
  merge(arr, left, mid, right);
}
int main() {
  int len;
  cin >> len;
  /*int* arr = new int[len];
  for (int i = 0;i < len;i++)
          cin >> arr[i];*/
  /*int p = partition(arr, 0, len - 1);
  for (int i = 0;i < len;i++) {
          if (i) {
                  if (i != p)
                          cout << " " << arr[i];
                  else
                          printf(" [%d]", arr[i]);
          }
          else
                  if (i != p)
                          cout << arr[i];
                  else
                          printf("[%d]", arr[i]);
  }
  cout << endl;*/
  Card *arr = new Card[len];
  Card *judge = new Card[len];
  for (int i = 0; i < len; i++) {
    cin >> arr[i].suit >> arr[i].val;
    judge[i] = arr[i];
  }
  quickSort(arr, 0, len - 1);
  mergeSort(judge, 0, len - 1);
  int isStable = 1;
  for (int i = 0; i < len; i++) {
    if (arr[i].suit != judge[i].suit) {
      cout << "Not stable\\n";
      isStable = 0;
      break;
    }
  }
  if (isStable)
    cout << "Stable\\n";
  for (int i = 0; i < len; i++)
    cout << arr[i].suit << " " << arr[i].val << endl;
  delete[] arr;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
int partition(int *arr, char *ch, int head, int tail);
void quickSort(int *arr, char *ch, int head, int tail);
void bubleSort(int *arr, char *ch, int len);
void printAns(int *arr, char *ch, int len);
int main() {
  using namespace std;
  int len;
  cin >> len;
  int *arr1 = new int[len];
  int *arr2 = new int[len];
  char *ch1 = new char[len];
  char *ch2 = new char[len];
  for (int i = 0; i < len; i++) {
    cin >> ch1[i] >> arr1[i];
    ch2[i] = ch1[i];
    arr2[i] = arr1[i];
  }
  quickSort(arr1, ch1, 0, len - 1);
  bubleSort(arr2, ch2, len);
  for (int i = 0; i < len; i++) {
    if (ch1[i] != ch2[i]) {
      cout << "Not stable" << endl;
      break;
    }
    if (i == len - 1)
      cout << "Stable" << endl;
  }

  printAns(arr1, ch1, len);
  return 0;
}

int partition(int *arr, char *ch, int head, int tail) {
  using namespace std;
  int stan = arr[tail];
  int board = head - 1;
  for (int j = head; j < tail; j++) {
    if (arr[j] <= stan) {
      board++;
      swap(arr[j], arr[board]);
      swap(ch[j], ch[board]);
    }
  }
  swap(arr[tail], arr[board + 1]);
  swap(ch[tail], ch[board + 1]);
  return board + 1;
}

void quickSort(int *arr, char *ch, int head, int tail) {
  if (head < tail) {
    int ntail = partition(arr, ch, head, tail);
    quickSort(arr, ch, head, ntail - 1);
    quickSort(arr, ch, ntail + 1, tail);
  }
}

void printAns(int *arr, char *ch, int len) {
  using std::cout;
  using std::endl;
  for (int i = 0; i < len; i++)
    cout << ch[i] << " " << arr[i] << endl;
}

void bubleSort(int *arr, char *zimu, int len) {
  using std::swap;
  for (int i = 0; i < len - 1; i++) {
    for (int j = len - 1; j > i; j--) {
      if (arr[j] < arr[j - 1]) {
        swap(arr[j], arr[j - 1]);
        swap(zimu[j], zimu[j - 1]);
      }
    }
  }
}