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

#include <stdio.h>

#define MAX 100000

struct TRAMP {
  char mark;
  int number;
};

TRAMP cards[MAX];

struct Node {
  Node() {
    number = 0;
    mark_array_index = 0;
    l_p = r_p = p_p = -1;
  }
  int number, mark_array_index, l_p, r_p, p_p;
  char mark_array[5];
};

class Tree {
public:
  Tree() {
    num_of_nodes = 0;
    nodes = new Node[MAX];
  };
  ~Tree() { delete[] nodes; }
  void push(TRAMP data) {
    int t = 0;
    while (true) {
      if (num_of_nodes == 0) {
        nodes[num_of_nodes].number = data.number;
        nodes[num_of_nodes].mark_array[nodes[num_of_nodes].mark_array_index++] =
            data.mark;
        num_of_nodes++;
        break;
      } else {
        if (nodes[t].number == data.number) {
          nodes[t].mark_array[nodes[t].mark_array_index++] = data.mark;
          break;
        } else if (nodes[t].number > data.number) {
          if (nodes[t].l_p == -1) {
            nodes[t].l_p = num_of_nodes;
            nodes[num_of_nodes].p_p = t;
            nodes[num_of_nodes].number = data.number;
            nodes[num_of_nodes]
                .mark_array[nodes[num_of_nodes].mark_array_index++] = data.mark;
            num_of_nodes++;
            break;
          } else {
            t = nodes[t].l_p;
          }
        } else {
          if (nodes[t].r_p == -1) {
            nodes[t].r_p = num_of_nodes;
            nodes[num_of_nodes].p_p = t;
            nodes[num_of_nodes].number = data.number;
            nodes[num_of_nodes]
                .mark_array[nodes[num_of_nodes].mark_array_index++] = data.mark;
            num_of_nodes++;
            break;
          } else {
            t = nodes[t].r_p;
          }
        }
      }
    }
  }

  bool compare(int cards_index) {
    int k = 0, number = cards[cards_index].number, t = 0;

    while (true) {
      if (nodes[t].number == number)
        break;
      else {
        t = (nodes[t].number > number) ? nodes[t].l_p : nodes[t].r_p;
      }
    }

    while (number == cards[cards_index].number) {
      if (cards[cards_index].mark != nodes[t].mark_array[k])
        return false;
      else {
        cards_index++;
        k++;
      }
    }
    return true;
  }

private:
  int num_of_nodes;
  Node *nodes;
};

int n;

int Partition(int left, int right) {
  int i = left - 1, pivot = cards[right].number;
  TRAMP tmp;
  for (int start = left; start < right; start++) {
    if (cards[start].number <= pivot) {
      i++;
      tmp = cards[i];
      cards[i] = cards[start];
      cards[start] = tmp;
    }
  }

  tmp = cards[right];
  cards[right] = cards[i + 1];
  cards[i + 1] = tmp;

  return i + 1;
}

void quickSort(int left, int right) {
  if (left < right) {
    int q = Partition(left, right);
    quickSort(left, q - 1);
    quickSort(q + 1, right);
  }
}

int main() {

  Tree tree;
  bool stableFLG = true;
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%s %d", &cards[i].mark, &cards[i].number);
    tree.push(cards[i]);
  }

  quickSort(0, n - 1);

  int history = 0;
  for (int i = 1; i < n; i++) {
    if (cards[history].number != cards[i].number || i == n - 1) {
      if (!tree.compare(history)) {
        stableFLG = false;
        break;
      } else {
        history = i;
      }
    }
  }

  if (stableFLG) {
    printf("Stable\\n");
  } else {
    printf("Not stable\\n");
  }

  for (int i = 0; i < n; i++)
    printf("%c %d\\n", cards[i].mark, cards[i].number);
}

Error: TLE

Fixed submission:

#include <stdio.h>

#define MAX 100000

struct TRAMP {
  char mark;
  int number;
};

TRAMP cards[MAX], bubbleCards[MAX];

int n, count;

void bubbleSort() {
  TRAMP tmp;
  for (int max_pos = n - 1; max_pos > 0; max_pos--) {
    for (int i = 0; i < max_pos; i++) {
      if (bubbleCards[i].number > bubbleCards[i + 1].number) {
        tmp = bubbleCards[i + 1];
        bubbleCards[i + 1] = bubbleCards[i];
        bubbleCards[i] = tmp;
      }
    }
  }
}

bool isStable() {
  bubbleSort();
  for (int i = 0; i < n; i++) {
    if (bubbleCards[i].mark != cards[i].mark)
      return false;
  }
  return true;
}

int Partition(int left, int right) {
  int i = left - 1, pivot = cards[right].number;
  TRAMP tmp;

  for (int start = left; start < right; start++) {
    if (cards[start].number <= pivot) {
      i++;
      tmp = cards[i];
      cards[i] = cards[start];
      cards[start] = tmp;
    }
  }

  tmp = cards[right];
  cards[right] = cards[i + 1];
  cards[i + 1] = tmp;

  return i + 1;
}

void quickSort(int left, int right) {
  if (left < right) {
    int q = Partition(left, right);
    quickSort(left, q - 1);
    quickSort(q + 1, right);
  }
}

int main() {
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%s %d", &cards[i].mark, &cards[i].number);
    bubbleCards[i] = cards[i];
  }

  quickSort(0, n - 1);

  if (isStable()) {
    printf("Stable\\n");
  } else {
    printf("Not stable\\n");
  }

  for (int i = 0; i < n; i++)
    printf("%c %d\\n", cards[i].mark, cards[i].number);
}