Problem:
Let's arrange a deck of cards. Your task is to sort totally n cards. A card consists of a part of a suit (S, H, C or D) and an number. Write a program which sorts such cards based on the following pseudocode:                 Partition(A, p, r) 1 x = A[r] 2 i = p-1 3 for j = p to r-1 4     do if A[j] <= x 5        then i = i+1 6            exchange A[i] and A[j]  7 exchange A[i+1] and A[r] 8 return i+1   Quicksort(A, p, r) 1 if p < r 2    then q = Partition(A, p, r) 3        run Quicksort(A, p, q-1) 4        run Quicksort(A, q+1, r)   Here, A is an array which represents a deck of cards and comparison operations are performed based on the numbers.  Your program should also report the stability of the output for the given input (instance). Here, 'stability of the output' means that: cards with the same value appear in the output in the same order as they do in the input (instance).

Incorrect submission (verdict: Wrong Answer):
import copy

def partition(p, r):
    i = p
    for j in range(p, r):
        if A[r][1] >= A[j][1]:
            A[i], A[j] = A[j], A[i]
            i += 1
    A[r], A[i] = A[i], A[r]
    return i

def quick_sort(p, r):
    if p < r:
        q = partition(p, r)
        quick_sort(p, q-1)
        quick_sort(q+1, r)

def is_stable():
    for i in range(N-1):
        if A[i][1] == A[i+1][1]:
            small_idx, large_idx = 0, 0
            for j in range(N-1):
                if A[i] == orig_list[j]:
                    small_idx = j
                elif A[i+1] == orig_list[j]:
                    large_idx = j
            if small_idx > large_idx:
                return False
    return True


N = int(input())
A = []
for _ in range(N):
    suit, num = input().split()
    num = int(num)
    A.append([suit, num])
orig_list = copy.deepcopy(A)

quick_sort(0, N-1)

is_stable = is_stable()
if is_stable:
    print("Stable")
else:
    print("Not Stable")

for card in A:
    print("%s %d" % (card[0], card[1]))


Fixed, accepted submission:
def partition(p, r):
    i = p
    for j in range(p, r):
        if A[r][1] >= A[j][1]:
            A[i], A[j] = A[j], A[i]
            i += 1
    A[r], A[i] = A[i], A[r]
    return i

def quick_sort(p, r):
    if p < r:
        q = partition(p, r)
        quick_sort(p, q-1)
        quick_sort(q+1, r)

def merge(left, mid, right):
    L = merge_list[left:mid]
    R = merge_list[mid:right]
    L.append(["", INFTY])
    R.append(["", INFTY])
    i, j = 0, 0
    for k in range(left, right):
        if L[i][1] <= R[j][1]:
            merge_list[k] = L[i]
            i += 1
        else:
            merge_list[k] = R[j]
            j += 1

def merge_sort(left, right):
    if left+1 < right:
        mid = int( (left+right)/2 )
        merge_sort(left, mid)
        merge_sort(mid, right)
        merge(left, mid, right)

INFTY = 1000000001

N = int(input())
A = []
for _ in range(N):
    suit, num = input().split()
    num = int(num)
    A.append([suit, num])
merge_list = A[:]

quick_sort(0, N-1)
merge_sort(0, N)

is_stable = True
for i in range(N):
    if A[i][0] != merge_list[i][0]:
        print("Not stable")
        is_stable = False
        break
if is_stable:
    print("Stable")

for card in A:
    print("%s %d" % (card[0], card[1]))