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

#include <iostream>
#include <vector>

void Swap(int &a, int &b) {
  int tmp = std::move(a);
  a = std::move(b);
  b = std::move(tmp);
}

// when 0th Recurtion, params -> Partition( v, 1, v.size()-1 );
int Partition(std::vector<int> &v, int p, int r) {
  int x = v[r];
  // std::cout << "after v[r]\\n";
  int i = p - 1;
  // std::cout << "loop: ";
  for (int j = p; j < r; ++j) {
    // std::cout << j << " ";
    if (v[j] <= x) {
      Swap(v[++i], v[j]);
    }
  }
  // std::cout << "\\n";
  Swap(v[i + 1], v[r]);
  return i + 1;
}

int main() {
  int n;
  std::cin.tie(0);
  std::cin >> n;

  std::vector<int> v(n);
  for (auto &elem : v) {
    std::cin >> elem;
    // std::cout << elem << " ";
  }
  // std::cout << "\\n";

  int r = Partition(v, 0, v.size() - 1);

  for (int i = 0; i < v.size(); ++i) {
    if (i != r) {
      std::cout << v[i] << ((i != v.size() - 1) ? " " : "\\n");
    } else {
      std::cout << "[" << v[i] << "]" << ((i != v.size() - 1) ? " " : "\\n");
    }
  }
}


Error: -11

Fixed submission:

#include <iostream>
#include <vector>

void Swap(int &a, int &b) {
  int tmp = std::move(a);
  a = std::move(b);
  b = std::move(tmp);
}

// when 0th Recurtion, params -> Partition( v, 1, v.size()-1 );
int Partition(std::vector<int> &v, int p, int r) {
  int x = v[r];
  int i = p - 1;
  for (int j = p; j < r; ++j) {
    if (v[j] <= x) {
      Swap(v[++i], v[j]);
    }
  }
  Swap(v[i + 1], v[r]);
  return i + 1;
}

int main() {
  int n;
  std::cin.tie(0);
  std::cin >> n;

  std::vector<int> v;
  for (auto &elem : v) {
    std::cin >> elem;
  }

  int r = Partition(v, 1, v.size() - 1);

  for (int i = 0; i < v.size(); ++i) {
    if (i != r) {
      std::cout << v[i] << ((i != v.size() - 1) ? " " : "");
    } else {
      std::cout << "[" << v[i] << "]" << ((i != v.size() - 1) ? " " : "");
    }
  }
}
Problem p02276 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;
int n;
int partition(int A[], int p, int r) {
  int x = A[r];
  int i = p - 1;
  for (int j = p; j < r; j++) {
    if (A[j] <= x) {
      i++;
      swap(A[i], A[j]);
    }
  }
  swap(A[i + 1], A[r]);
  return i + 1;
}
int main() {
  for (; cin >> n;) {
    int data[n];
    for (int i = 0; i < n; i++)
      cin >> data[i];

    int q = partition(data, 0, n - 1);
    for (int i = 0; i < n; i++) {
      if (i == q)
        cout << "[";
      cout << data[i];
      if (i == q)
        cout << "]";

      if (i != n - 1)
        cout << " ";
      else
        cout << endl;
    }
  }
}

Error: 0
j:0
j:1
j:2
j:3
j:4
j:5
j:6
j:7
j:8
j:9
j:10


Fixed submission:

#include <iostream>
#include <vector>

using namespace std;
int n;
int partition(int A[], int p, int r) {
  int x = A[r];
  int i = p - 1;
  for (int j = p; j < r; j++) {
    cerr << "j:" << j << endl;
    if (A[j] <= x) {
      i++;
      swap(A[i], A[j]);
    }
  }
  swap(A[i + 1], A[r]);
  return i + 1;
}
int main() {
  for (; cin >> n;) {
    int data[n];
    for (int i = 0; i < n; i++)
      cin >> data[i];

    int q = partition(data, 0, n - 1);
    for (int i = 0; i < n; i++) {
      if (i == q)
        cout << "[";
      cout << data[i];
      if (i == q)
        cout << "]";

      if (i != n - 1)
        cout << " ";
      else
        cout << endl;
    }
  }
}
Problem p02276 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  int *A = new int[n + 1];
  for (int i = 1; i < n + 1; i++) {
    cin >> A[i];
  }

  int x = A[n];
  int i = 0;
  int tmp = 0;
  for (int j = 1; j <= n - 1; j++) {
    if (A[j] <= x) {
      i = i + 1;
      tmp = A[i];
      A[i] = A[j];
      A[j] = tmp;
    }
  }
  tmp = A[i + 1];
  A[i + 1] = A[n];
  A[n] = tmp;
  i = i + 1;

  for (int j = 1; j < n + 1; j++) {
    if (j == i) {
      cout << '[' << A[j] << ']';
    } else {
      cout << A[j];
    }
    if (j < n) {
      cout << ' ';
    } else {
      cout << endl;
    }
  }
  delete[] A;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  int *A = new int[n + 1];
  for (int i = 1; i < n + 1; i++) {
    cin >> A[i];
  }

  int x = A[n];
  int i = 0;
  int tmp = 0;
  for (int j = 1; j <= n - 1; j++) {
    if (A[j] <= x) {
      i = i + 1;
      tmp = A[i];
      A[i] = A[j];
      A[j] = tmp;
    }
  }
  tmp = A[i + 1];
  A[i + 1] = A[n];
  A[n] = tmp;
  i = i + 1;

  for (int j = 1; j < n + 1; j++) {
    if (j == i) {
      cout << '[' << A[j] << ']';
    } else {
      cout << A[j];
    }
    if (j = n - 1) {
      cout << ' ';
    } else {
      cout << endl;
    }
  }
  delete[] A;
  return 0;
}
Problem p02276 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#define N 100000
int partition(int, int);

int A[N];
int main() {
  int i, n, q;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }
  q = partition(0, n - 1);

  for (i = 0; i < n; i++) {
    if (i != 0)
      printf(" ");
    if (i == q)
      printf("[");
    printf("%d", A[i]);
    if (i == q)
      printf("]");
  }
  printf("\\n");

  return 0;
}

int partition(int y, int x) {
  int i, j, n, m;
  n = A[x];
  j = y - 1;
  for (i = 0; i < x; i++) {
    if (A[i] <= n) {
      j++;
      m = A[j];
      A[j] = A[i];
      A[i] = m;
    }
  }
  m = A[j + 1];
  A[j + 1] = A[x];
  A[x] = m;
  return j + 1;
}

Error: 0

Fixed submission:

#include <stdio.h>
#define N 10000
int partition(int, int);

int A[N];
int main() {
  int i, n, q;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }
  q = partition(0, n - 1);

  for (i = 0; i < n; i++) {
    if (i != 0)
      printf(" ");
    if (i == q)
      printf("[");
    printf("%d", A[i]);
    if (i == q)
      printf("]");
  }
  printf("\\n");

  return 0;
}

int partition(int y, int x) {
  int i, j, n, m;
  n = A[x];
  j = y - 1;
  for (i = 0; i < x; i++) {
    if (A[i] <= n) {
      j++;
      m = A[j];
      A[j] = A[i];
      A[i] = m;
    }
  }
  m = A[j + 1];
  A[j + 1] = A[x];
  A[x] = m;
  return j + 1;
}
Problem p02276 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stdio.h>
using namespace std;
#define MAX 100000

int A[MAX], n;

int partition(int p, int r) {

  int x = A[r];
  int i = p - 1;
  for (int j = p; j < r; j++) {
    if (A[j] <= x) {
      i++;
      swap(A[j], A[i]);
    }
  }
  swap(A[i + 1], A[r]);
  return i + 1;
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);
  int q = partition(0, n - 1);
  for (int i = 0; i < n; i++) {
    if (i)
      printf(" ");
    if (i == q)
      printf("[");
    printf("%d", A[i]);
    if (i == q)
      printf("]");
  }
  printf("\\n");

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <stdio.h>
using namespace std;
#define MAX 100000

int A[MAX], n;

int partition(int p, int r) {

  int x = A[r];
  int i = p - 1;
  for (int j = p; j < r; j++) {
    if (A[j] <= x) {
      i++;
      swap(A[j], A[i]);
    }
  }
  swap(A[i + 1], A[r]);
  return i + 1;
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", A[i]);
  int q = partition(0, n);
  for (int i = 0; i < n; i++) {
    if (i)
      printf(" ");
    if (i == q) {
      printf("[");
      printf("%d", A[i]);
      printf("]");
    }
  }
  printf("\\n");

  return 0;
}
Problem p02276 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int a[100000], n;

int part(int a[], int p, int r) {
  int x = a[r];
  int i = p - 1;
  for (int j = p; j < r; j++) {
    if (a[j] <= x) {
      i++;
      swap(a[i], a[j]);
    }
  }
  swap(a[i + 1], a[r]);
  return i + 1;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; cin >> a[i++])
    ;
  int q = part(a, 0, n - 1);
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    if (i == q)
      cout << "[";
    cout << a[i];
    if (i == q)
      cout << "]";
  }
  cout << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int a[100000], n;

int part(int a[], int p, int r) {
  int x = a[r];
  int i = p - 1;
  for (int j = p; j < r;) {
    if (a[j] <= x) {
      i++;
      swap(a[i], a[j]);
    }
  }
  swap(a[i + 1], a[r]);
  return i + 1;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; cin >> a[i++])
    ;
  int q = part(a, 0, n - 1);
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    if (i == q)
      cout << "[";
    cout << a[i];
    if (i == q)
      cout << "]";
  }
  return 0;
}
Problem p02276 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
int main() {
  int i, a, A[100000], z, jsta = 0, jend = 0, B[100000], j;
  scanf("%d", &a);
  for (i = 0; i < a; i++) {
    scanf("%d", &A[i]);
  }
  for (i = 0; i < a; i++) {
    if (A[i] <= A[a - 1]) {
      z = A[jsta];
      A[jsta] = A[i];
      A[i] = z;
      jsta++;
      jend++;
    } else
      jend++;
  }

  for (i = 0; i < a; i++) {
    if (i == jsta - 1)
      printf("[%d] ", A[i]);
    else if (i != a - 1)
      printf("%d ", A[i]);
    else
      printf("%d", A[i]);
  }
  printf("\\n");
}


Error: 0

Fixed submission:

#include <stdio.h>
int main() {
  int i, a, A[50000], z, jsta = 0, jend = 0, B[50000], j;
  scanf("%d", &a);
  for (i = 0; i < a; i++) {
    scanf("%d", &A[i]);
  }
  for (i = 0; i < a; i++) {
    if (A[i] <= A[a - 1]) {
      z = A[jsta];
      A[jsta] = A[i];
      A[i] = z;
      jsta++;
      jend++;
    } else
      jend++;
  }

  for (i = 0; i < a; i++) {
    if (i == jsta - 1)
      printf("[%d] ", A[i]);
    else if (i != a - 1)
      printf("%d ", A[i]);
    else
      printf("%d", A[i]);
  }
  printf("\\n");
}
Problem p02276 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

// Exchange
void exchange(int A[], int i, int j) {
  int tmp = A[i];
  A[i] = A[j];
  A[j] = tmp;
}

// Partition
int partition(int A[], int l, int r) {
  int threshold = A[r - 1];
  int i = l - 1;
  for (int j = l; j < r - 1; j++) {
    if (A[j] <= threshold) {
      i++;
      // Exchange
      exchange(A, i, j);
    }
  }
  i++;
  // Insert threshold
  exchange(A, i, r - 1);
  return i;
}

// MAIN FUNCTION
int main(int argc, char **argv) {
  // Input
  int A[1000000];
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];

  // Partition
  int pivot = partition(A, 0, n);

  // Print all elements
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    if (i == pivot) {
      cout << "[" << A[i] << "]";
    } else {
      cout << A[i];
    }
  }
  cout << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

// Exchange
void exchange(int A[], int i, int j) {
  int tmp = A[i];
  A[i] = A[j];
  A[j] = tmp;
}

// Partition
int partition(int A[], int l, int r) {
  int threshold = A[r - 1];
  int i = l - 1;
  for (int j = l; j < r - 1; j++) {
    if (A[j] <= threshold) {
      i++;
      // Exchange
      exchange(A, i, j);
    }
  }
  i++;
  // Insert threshold
  exchange(A, i, r - 1);
  return i;
}

// MAIN FUNCTION
int main(int argc, char **argv) {
  // Input
  int A[50000];
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];

  // Partition
  int pivot = partition(A, 0, n);

  // Print all elements
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    if (i == pivot) {
      cout << "[" << A[i] << "]";
    } else {
      cout << A[i];
    }
  }
  cout << endl;

  return 0;
}
Problem p02276 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
using namespace std;

int n[100003] = {};
int main() {

  int N;
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> n[i];

  int x = 0, y = 0;
  while (x < N && y < N) {

    if (n[x] <= n[N - 1]) {
      x++;
      y = x + 1;
    } else if (n[x] > n[N - 1] && n[y] > n[N - 1])
      y++;
    else if (n[x] > n[N - 1] && n[y] <= n[N - 1]) {
      int t = n[y];
      n[y] = n[x];
      n[x] = t;
      x++;
      y++;
    }
  }

  //	cout<<n[N-1];
  //	int t=n[N-1];n[N-1]=n[x-1];n[x-1]=t;

  for (int i = 0; i < N; i++) {
    if (i > 0)
      cout << " ";
    if (i != x - 1)
      cout << n[i];
    if (i == x - 1)
      cout << "[" << n[i] << "]";
  }
  cout << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
using namespace std;

int n[100003] = {};
int main() {

  int N;
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> n[i];

  int x = 0, y = 0;
  while (x < N && y < N) {
    if (n[x] < n[N - 1]) {
      x++;
      y = x + 1;
    } else if (n[x] > n[N - 1] && n[y] > n[N - 1])
      y++;
    else if (n[x] > n[N - 1] && n[y] <= n[N - 1]) {
      int t = n[y];
      n[y] = n[x];
      n[x] = t;
      x++;
      y++;
    }
  }

  //	cout<<n[N-1];
  //	int t=n[N-1];n[N-1]=n[x-1];n[x-1]=t;

  for (int i = 0; i < N; i++) {
    if (i > 0)
      cout << " ";
    if (i != x - 1)
      cout << n[i];
    if (i == x - 1)
      cout << "[" << n[i] << "]";
  }
  cout << endl;

  return 0;
}
Problem p02276 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

int partition(int *A, int p, int r) {
  int x = A[r];
  int i = p - 1;
  int tmp;
  for (int j = 0; j < r; j++) {
    if (A[j] <= x) {
      i++;
      tmp = A[i];
      A[i] = A[j];
      A[j] = tmp;
    }
  }
  tmp = A[i + 1];
  A[i + 1] = A[r];
  A[r] = tmp;
  return i + 1;
}

int main() {
  int n, q;
  int A[100000];

  while (scanf("%d", &n) != EOF) {
    char c = ' ';
    for (int i = 0; i < n; i++) {
      scanf("%d", &A[i]);
    }
    q = partition(A, 0, n - 1);
    for (int i = 0; i < n; i++) {
      if (i == n - 1)
        c = '\\n';
      if (i == q)
        printf("[%d]%c", A[i], c);
      else
        printf("%d%c", A[i], c);
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int partition(int *A, int p, int r) {
  int x = A[r];
  int i = p - 1;
  int tmp;
  for (int j = 0; j < r; j++) {
    if (A[j] <= x) {
      i++;
      tmp = A[i];
      A[i] = A[j];
      A[j] = tmp;
    }
  }
  tmp = A[i + 1];
  A[i + 1] = A[r];
  A[r] = tmp;
  return i + 1;
}

int main() {
  int n, q;
  int A[100000];

  while (1) {
    cin >> n;
    char c = ' ';
    for (int i = 0; i < n; i++) {
      cin >> A[i];
    }
    q = partition(A, 0, n - 1);
    for (int i = 0; i < n; i++) {
      if (i == n - 1)
        c = '\\n';
      if (i == q)
        cout << '[' << A[i] << '[' << c;
      else
        cout << A[i] << c;
    }
  }
  return 0;
}
Problem p02277 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
typedef struct P {
  char a;
  int b;
  int n;
} P;
int getPosition(P a[], int p, int r) { return r; }
void swap(P a[], int x, int y) {
  P t = a[x];
  a[x] = a[y];
  a[y] = t;
}
int partition(P a[], int p, int r) {
  int i, j, x = getPosition(a, p, r);
  swap(a, x, r);
  i = p - 1;
  for (j = p; j < r; j++) {
    if (a[j].b <= a[r].b) {
      i++;
      swap(a, i, j);
    }
  }
  swap(a, i + 1, r);
  return i + 1;
}
void QS(P a[], int p, int r) {
  if (p < r) {
    int q;
    q = partition(a, p, r);
    QS(a, p, q - 1);
    QS(a, q + 1, r);
  }
}
void QuickSort(P a[], int n) { QS(a, 0, n - 1); }

int main() {
  int n, i;
  P a[100000];
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i].a >> a[i].b;
    a[i].n = i;
  }
  QuickSort(a, n);
  for (i = 0; i < n - 1; i++) {
    if (a[i].b == a[i + 1].b && a[i].n >= a[i + 1].n)
      break;
  }
  if (i == n - 1)
    cout << "Stable" << endl;
  else
    cout << "Not stable" << endl;
  for (i = 0; i < n; i++) {
    cout << a[i].a << ' ' << a[i].b << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
typedef struct P {
  char a;
  int b;
} P;
int getPosition(P a[], int p, int r) { return r; }
void swap(P a[], int x, int y) {
  P t = a[x];
  a[x] = a[y];
  a[y] = t;
}
int partition(P a[], int p, int r) {
  int i, j, x = getPosition(a, p, r);
  swap(a, x, r);
  i = p - 1;
  for (j = p; j < r; j++) {
    if (a[j].b <= a[r].b) {
      i++;
      swap(a, i, j);
    }
  }
  swap(a, i + 1, r);
  return i + 1;
}
void QS(P a[], int p, int r) {
  if (p < r) {
    int q;
    q = partition(a, p, r);
    QS(a, p, q - 1);
    QS(a, q + 1, r);
  }
}
void QuickSort(P a[], int n) { QS(a, 0, n - 1); }
void InsertionSort(P a[], int n) {
  int i, j;
  P v;
  for (i = 1; i < n; i++) {
    v = a[i];
    j = i - 1;
    while (j >= 0 && a[j].b > v.b) {
      a[j + 1] = a[j];
      j--;
    }
    a[j + 1] = v;
  }
}
int main() {
  int n, i;
  P a[100000], b[100000];
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i].a >> a[i].b;
    b[i].a = a[i].a;
    b[i].b = a[i].b;
  }
  QuickSort(a, n);
  InsertionSort(b, n);
  for (i = 0; i < n; i++) {
    if (!(a[i].a == b[i].a && a[i].b == b[i].b))
      break;
  }
  if (i == n)
    cout << "Stable" << endl;
  else
    cout << "Not stable" << endl;
  for (i = 0; i < n; i++) {
    cout << a[i].a << ' ' << a[i].b << endl;
  }
  return 0;
}
Problem p02277 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;

struct Card {
  char a;
  int b;
};

bool compare_as_ints(Card i, Card j) { return (i.b < j.b); }

int partition(Card A[], int p, int r) {
  Card x = A[r];
  int i = p - 1;

  for (int j = p; j <= r - 1; j++) {
    if (A[j].b <= x.b) {
      i++;
      swap(A[i], A[j]);
    }
  }

  swap(A[i + 1], A[r]);

  return i + 1;
}

void quickSort(Card A[], int p, int r) {
  if (p < r) {
    int q = partition(A, p, r);
    quickSort(A, p, q - 1);
    quickSort(A, q + 1, r);
  }
}

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

  Card A[100000], B[100000];
  for (int i = 0; i < n; i++) {
    cin >> A[i].a >> A[i].b;
    B[i] = A[i];
  }

  quickSort(A, 0, n - 1);
  stable_sort(B, B + n, compare_as_ints);

  for (int i = 0; i < n; i++) {
    if (A[i].a != B[i].a) {
      cout << "Not stable" << endl;
      break;
    }
    if (i == n - 1) {
      cout << "Stable" << endl;
    }
  }

  for (int i = 0; i < n; i++) {
    cout << A[i].a << ' ' << A[i].b << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  int *A = new int[n + 1];
  for (int i = 1; i < n + 1; i++) {
    cin >> A[i];
  }

  int x = A[n];
  int i = 0;
  int tmp = 0;
  for (int j = 1; j <= n - 1; j++) {
    if (A[j] <= x) {
      i = i + 1;
      tmp = A[i];
      A[i] = A[j];
      A[j] = tmp;
    }
  }
  tmp = A[i + 1];
  A[i + 1] = A[n];
  A[n] = tmp;
  i = i + 1;

  for (int j = 1; j < n + 1; j++) {
    if (j == i) {
      cout << '[' << A[j] << ']';
    } else {
      cout << A[j];
    }
    if (j = n - 1) {
      cout << ' ';
    } else {
      cout << endl;
    }
  }
  delete[] A;
  while (1)
    ;
  return 0;
}
Problem p02277 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;

struct card {
  char suit;
  int num;
  int jun;
};

int out_card(vector<card> &A, int n) {
  int i;
  for (i = 0; i <= n - 1; i++) {
    cout << A[i].suit << " " << A[i].num << endl;
  }
}

int judgeStability(vector<card> &A, int n) {
  int i;
  int flag = 0;
  for (i = 1; i < n; i++) {
    if ((A[i].num == A[i - 1].num) && (A[i].jun < A[i - 1].jun)) {
      flag = 1;
    }
  }
  return flag;
}

int partition(vector<card> &A, int p, int r) {
  int x = A[r].num;
  int i = p - 1;
  int j;
  for (j = p; j < r; j++) {
    if (A[j].num <= x) {
      i++;
      swap(A[i], A[j]);
    }
  }
  swap(A[i + 1], A[r]);
  return i + 1;
}

void QuickSort(vector<card> &A, int p, int r) {
  if (p < r) {
    int q = partition(A, p, r);
    QuickSort(A, p, q - 1);
    QuickSort(A, q + 1, r);
  }
  return;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, i;
  cin >> n;

  vector<card> C(n);
  for (i = 0; i < n; i++) {
    cin >> C[i].suit >> C[i].num;
    C[i].jun = i;
  }

  QuickSort(C, 0, n - 1);

  int flag = judgeStability(C, n);
  if (!flag) {
    cout << "Stable" << endl;
  } else {
    cout << "Not stable" << endl;
  }
  out_card(C, n);

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

struct card {
  char suit;
  int num;
};

int out_card(vector<card> &A, int n) {
  int i;
  for (i = 0; i <= n - 1; i++) {
    cout << A[i].suit << " " << A[i].num << endl;
  }
}

int card_equal(vector<card> &A, vector<card> &B, int n) {
  int i;
  int flag = 0;
  for (i = 0; i < n; i++) {
    if ((A[i].suit != B[i].suit) || (A[i].num != B[i].num)) {
      flag = 1;
    }
  }
  return flag;
}

void BubbleSort(vector<card> &A, int n) {
  int i, j;
  for (i = 0; i < n; i++) {
    for (j = n - 1; j > i; j--) {
      if (A[j].num < A[j - 1].num) {
        swap(A[j], A[j - 1]);
      }
    }
  }
}

int partition(vector<card> &A, int p, int r) {
  int x = A[r].num;
  int i = p - 1;
  int j;
  for (j = p; j < r; j++) {
    if (A[j].num <= x) {
      i++;
      swap(A[i], A[j]);
    }
  }
  swap(A[i + 1], A[r]);
  return i + 1;
}

void QuickSort(vector<card> &A, int p, int r) {
  if (p < r) {
    int q = partition(A, p, r);
    QuickSort(A, p, q - 1);
    QuickSort(A, q + 1, r);
  }
  return;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, i;
  cin >> n;

  vector<card> C(n), D(n);
  for (i = 0; i < n; i++) {
    cin >> C[i].suit >> C[i].num;
    D[i].suit = C[i].suit;
    D[i].num = C[i].num;
  }

  QuickSort(C, 0, n - 1);
  BubbleSort(D, n);

  int flag = card_equal(C, D, n);
  if (!flag) {
    cout << "Stable" << endl;
  } else {
    cout << "Not stable" << endl;
  }
  out_card(C, n);

  return 0;
}