Problem:


Quick Sort

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] n, the number of cards.

n cards are given in the following lines. Each card is given in a line and represented by a pair of a character and an integer separated by a single space.

Output

In the first line, print the stability ("Stable" or "Not stable") of this output.

In the following lines, print the arranged cards in the same manner of that of the input.

Constraints

1 ≤ n ≤ 100,000

1 ≤ the number of a card  ≤ 109

There are no identical card in the input

Sample Input 1

6
D 3
H 2
D 1
S 3
D 2
C 1

Sample Output 1

Not stable
D 1
C 1
D 2
H 2
D 3
S 3

Sample Input 2

2
S 1
H 1

Sample Output 2

Stable
S 1
H 1



Sample input:
6
D 3
H 2
D 1
S 3
D 2
C 1


Sample output:
Not stable
D 1
C 1
D 2
H 2
D 3
S 3

Submission status: Runtime Error

Solution code:
#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_6_C&lang=jp

def partition(target_list, l, r):
    x = int(target_list[r][1:])
    i = l - 1

    for j in range(l, r):
        if int(target_list[j][1:]) <= x:
            i = i + 1
            tmp = target_list[i]
            target_list[i] = target_list[j]
            target_list[j] = tmp
    tmp = target_list[r]
    target_list[r] = target_list[i + 1]
    target_list[i + 1] = tmp
    
    return  i + 1

def check_stable(origin_list, target_list):
    for i, origin_v in enumerate(origin_list.split()):
        for origin_v2 in origin_list[i+1:].split():
            if origin_v[1] == origin_v2[1]:
                for j, target_v in enumerate(target_list.split()):
                    for target_v2 in target_list[j+1:].split():
                        if origin_v == target_v2 and origin_v2 == target_v:
                            return "Not stable"
    return "Stable"

def quick_sort(target_list, l, r):
    if l < r:
        c = partition(target_list, l, r)
        quick_sort(target_list, l, c - 1)
        quick_sort(target_list, c + 1, r)
        
if __name__ == "__main__":
    n_list = int(input())

    target_list = []
    for i in range(n_list):
        target_list.append(input())
    origin_list = [a for a in target_list]
    quick_sort(target_list, 0, n_list - 1)
    print(check_stable(origin_list, target_list))
    print("\n".join(target_list))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Input:
6
D 3
H 2
D 1
S 3
D 2
C 1

Output:
Not stable / Stable
D 1
C 1
D 2
H 2
D 3
S 3
"""
import sys
import copy


def partition(array, start, end):
    x = int(array[end][2:])
    i = start - 1
    assert start <= end
    for j in range(start, end):
        if int(array[j][2:]) <= x:
            i += 1
            array[i], array[j] = array[j], array[i]
    array[i + 1], array[end] = array[end], array[i + 1]
    return i + 1


def quick_sort(array, start, end):
    if start + 1 < end:
        cursor = partition(array, start, end)
        quick_sort(array, start, cursor - 1)
        quick_sort(array, cursor + 1, end)
    return array


def merge(A, left, mid, right):
    L = A[left:mid] + ['N ' + str(int(1e9 + 1))]
    R = A[mid:right] + ['N ' + str(int(1e9 + 1))]

    i, j = 0, 0

    for k in range(left, right):
        if int(L[i][2:]) <= int(R[j][2:]):
            A[k] = L[i]
            i += 1
        else:
            A[k] = R[j]
            j += 1

    return None


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


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    array_length = int(_input[0])
    array = list(map(lambda x: x.strip('\n'), _input[1:]))
    assert len(array) == array_length

    merge_array = copy.deepcopy(array)

    # print('merge',merge_array)
    merge_result = merge_sort(A=merge_array, left=0, right=array_length)
    # print(*merge_result, sep='\n')

    # print('quick',array)
    quick_result = quick_sort(array=array, start=0, end=array_length)
    if quick_result != merge_result:
        print('Not stable')
    else:
        print('Stable')
    print(*quick_result, sep='\n')

Submission status: Runtime Error

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


def merge(A, left, mid, right):
    n1 = mid - left
    n2 = right - mid
    L = A[left:mid]
    R = A[mid:right]
    L.append(('J', 99))
    R.append(('J', 99))
    i = 0  # L[]??¨?????????????????????
    j = 0  # R[]??¨?????????????????????
    for k in range(left, right):
        if L[i][1] <= R[j][1]:
            A[k] = L[i]
            i += 1
        else:
            A[k] = R[j]
            j += 1


def partition(A, p, r):
    """
    ???????????????????????????????????????A[r]?????°?????????????????????????????????????????????A[r-1]??¨?´??????????????????§??¨???
    """
    x = A[r][1]
    i = p - 1
    for j in range(p, r):
        if A[j][1] <= x:
            i += 1
            temp = A[i]
            A[i] = A[j]
            A[j] = temp
    temp = A[i+1]
    A[i+1] = A[r]
    A[r] = temp
    return i


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


if __name__ == '__main__':
    # ??????????????\???
    #cards = [('D', 3), ('H', 2), ('D', 1), ('S', 3), ('D', 2), ('C', 1)]
    #comp_cards = cards[:]
    num_of_cards = int(input())
    cards = []
    for i in range(num_of_cards):
        suit, rank = input().split(' ')
        cards.append((suit, int(rank)))
    comp_cards = cards[:]


    # ??????????????????
    data_length = len(cards)
    quick_sort(cards, 0, data_length-1)
    merge_sort(comp_cards, 0, data_length)  # ?????????????????????????????????????????¨

    # ???????????????
    stable = True  # ?????????????????????????????????????????????
    for x, y in zip(cards, comp_cards):
        if x != y:
            stable = False
            print('Not stable')
            break
    if stable is True:
        print('Stable')

    for c in cards:  # ??????????????¨???
        print('{0} {1}'.format(c[0], c[1]))

Submission status: Runtime Error

Solution code:
def partition(A,B,p,r):
    x = A[r]
    i = p-1
    for j in xrange(p,r):
        if A[j] <= x:
            i += 1
            tmp = A[i]
            A[i] = A[j]
            A[j] = tmp
            tmp = B[i]
            B[i] = B[j]
            B[j] = tmp
    tmp = A[i+1]
    A[i+1] = A[r]
    A[r] = tmp
    tmp = B[i+1]
    B[i+1] = B[r]
    B[r] = tmp
    return i+1

def quick_sort(A,B, p, r):
    # q??????????????????p??¨r????????????????????°??????????????§???
    # ?????????p?????????q-1???????????§???????????????????????????q+1???r????¶???????
    # ?????§??????????????§????????????????????¨?????????
    if p < r:
        # ???????????£?????§?????????????°??????°?????????
        q = partition(A,B,p,r)
        quick_sort(A,B,p,q-1)
        quick_sort(A,B,q+1,r)

def stable_check(sorted_values,sorted_suits,default_values,default_suits,n):
    for i in xrange(1,10):
        check_ss = []
        check_ds = []
        if i in sorted_values:
            for x in xrange(n):
                if i == sorted_values[x]:
                    check_ss.append(sorted_suits[x])
                if i == default_values[x]:
                    check_ds.append(default_suits[x])
            for x in xrange(len(check_ss)):
                if check_ss[x]!=check_ds[x]:
                    return "Not stable"
    return "Stable"

def main():
    n = input()
    A = []
    B = []
    for i in xrange(n):
        b,a = raw_input().split()
        A.append(int(a))
        B.append(b)
    cpA=list(A)
    setA = set(A)
    cpB=list(B)
    quick_sort(A,B,0,n-1)

    # Stable Check
    print stable_check(A,B,cpA,cpB,n)
    for i in xrange(n):
        line = "%s %s"%(B[i],A[i])
        print line

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def mergesort(A, s, e):
    if s + 1 < e:
        m = (e + s) // 2
        mergesort(A, s, m)
        mergesort(A, m, e)
        merge(A, s, m, e)


def merge(A, s, m, e):
    L = A[s:m] + [('*', 1E10)]
    R = A[m:e] + [('*', 1E10)]
    ix_l = ix_r = 0
    for ix in range(s, e):
        if L[ix_l][1] < R[ix_r][1]:
            A[ix] = L[ix_l]
            ix_l += 1
        else:
            A[ix] = R[ix_r]
            ix_r += 1


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


def quicksort(A, p, r):
    if p < r:
        i = partition(A, p, r)
        quicksort(A, p, i - 1)
        quicksort(A, i + 1, r)


fin = open('sample.txt')
input = fin.readline

if __name__ == '__main__':
    N = int(input().strip())
    AA = [0] * N
    for i in range(N):
        key, val = input().strip().split()
        val = int(val)
        AA[i] = (key, val)
    AA2 = AA
    quicksort(AA, 0, len(AA) - 1)
    mergesort(AA2, 0, len(AA2))
    if all([a == 2 for a, a2 in zip(AA, AA2)]):
        print('Stable')
    else:
        print('Not stable')
    for key, val in AA:
        print('{} {}'.format(key, val))

Submission status: Runtime Error

Solution code:
def mergesort(A, s, e):
    if s + 1 < e:
        m = (e + s) // 2
        mergesort(A, s, m)
        mergesort(A, m, e)
        merge(A, s, m, e)


def merge(A, s, m, e):
    L = A[s:m] + [('*', 1E10)]
    R = A[m:e] + [('*', 1E10)]
    ix_l = ix_r = 0
    for ix in range(s, e):
        if L[ix_l][1] < R[ix_r][1]:
            A[ix] = L[ix_l]
            ix_l += 1
        else:
            A[ix] = R[ix_r]
            ix_r += 1


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


def quicksort(A, p, r):
    if p < r:
        i = partition(A, p, r)
        quicksort(A, p, i - 1)
        quicksort(A, i + 1, r)


fin = open('sample.txt')
input = fin.readline

if __name__ == '__main__':
    N = int(input().strip())
    AA = [0] * N
    for i in range(N):
        key, val = input().strip().split()
        val = int(val)
        AA[i] = (key, val)
    AA2 = AA[:]
    quicksort(AA, 0, len(AA) - 1)
    mergesort(AA2, 0, len(AA2))
    if all([a == a2 for a, a2 in zip(AA, AA2)]):
        print('Stable')
    else:
        print('Not stable')
    for key, val in AA:
        print('{} {}'.format(key, val))

Submission status: Runtime Error

Solution code:
def swap(A,i,j):
    A[i],A[j] = A[j],A[i]
    return A

def isStable(A):
    for i in range(1, len(A)):
        if A[i][1] == A[i-1][1]:
            if A[i][2] < A[i-1][2]:
                return False
    return True

def partition(A,p=0, r=None):
    if r is None:
        r = len(A)-1
    x = A[r]
    i = p-1
    for j in range(p,r):
        if A[j] <= x:
            i += 1
            swap(A,i,j)
    swap(A,i+1,r)
    return i+1

def quick(A,p=0,r=None):
    if r is None:
        r = len(A)-1
    if p < r:
        q = partition(A,p,r)
        quick(A,p,q-1)
        quick(A,q+1,r)

import sys
if __name__ == "__main__":
    n = int(sys.stdin.readline())
    A = []
    for i in range(n):
        card = sys.stdin.readline().split()
       A.append(int(card[1]), [card[0], i])
    quick(A,0,n-1)
    if isStable(A):
        print("Stable")
    else:
        print("Not stable")
    for card in A:
        print(card[1], card[0])

Submission status: Runtime Error

Solution code:
def swap(A,i,j):
    A[i],A[j] = A[j],A[i]
    return A

def isStable(A):
    for i in range(1, len(A)):
        if A[i][1] == A[i-1][1]:
            if A[i][2] < A[i-1][2]:
                return False
    return True

def partition(A,p=0, r=None):
    if r is None:
        r = len(A)-1
    x = A[r]
    i = p-1
    for j in range(p,r):
        if A[j] <= x:
            i += 1
            swap(A,i,j)
    swap(A,i+1,r)
    return i+1

def quick(A,p=0,r=None):
    if r is None:
        r = len(A)-1
    if p < r:
        q = partition(A,p,r)
        quick(A,p,q-1)
        quick(A,q+1,r)

import sys
if __name__ == "__main__":
    n = int(sys.stdin.readline())
    A = []
    for i in range(n):
        card = sys.stdin.readline().split()
       A.append([int(card[1]), card[0], i])
    quick(A,0,n-1)
    if isStable(A):
        print("Stable")
    else:
        print("Not stable")
    for card in A:
        print(card[1], card[0])

Submission status: Runtime Error

Solution code:
def swap(A,i,j):
    A[i],A[j] = A[j],A[i]
    return A

def isStable(A):
    for i in range(1, len(A)):
        if A[i][1] == A[i-1][1]:
            if A[i][2] < A[i-1][2]:
                return False
    return True

def partition(A,p=0, r=None):
    if r is None:
        r = len(A)-1
    x = A[r]
    i = p-1
    for j in range(p,r):
        if A[j][1] <= x[1]:
            i += 1
            swap(A,i,j)
    swap(A,i+1,r)
    return i+1

def quick(A,p=0,r=None):
    if r is None:
        r = len(A)-1
    if p < r:
        q = partition(A,p,r)
        quick(A,p,q-1)
        quick(A,q+1,r)

import sys
if __name__ == "__main__":
    n = int(sys.stdin.readline())
    A = []
    for i in range(n):
        card = sys.stdin.readline().split()
       A.append([card[0], int(card[1]), i])
    quick(A,0,n-1)
    if isStable(A):
        print("Stable")
    else:
        print("Not stable")
    for card in A:
        print(card[0], card[1])

Submission status: Runtime Error

Solution code:
import sys
readline = sys.stdin.readline
def partition(A, p, r):
    x = A[r][1]
    i = p - 1
    for j in range(p, r):
        if A[j][1] <= x:
            i += 1
            A[i], A[j] = A[j], A[i]
    A[i + 1], A[r] = A[r], A[i + 1]
    return i + 1
def quicksort(A, p, r):
    if p < r:
        q = partition(A, p, r)
        quicksort(A, p, q - 1)
        quicksort(A, q + 1, r)
def isStable(A):
    for i in range(0, len(A) - 1):
        if A[i][1] == A[i + 1][1]:
            if A[i][2] > A[i + 1][2]:
                return False
    return True
n = int(input())
f = lambda a: (a[0], int(a[1]))
A = [f(readline().split()) + (i) for i in range(n)]
quicksort(A, 0, n - 1)
print("Stable" if isStable(A) else "Not stable")
print("\n".join(f"{a} {b}" for a, b, c in A))

Submission status: Runtime Error

Solution code:
def partition(hoge, p, r):
    x = int(hoge[r-1][1])
    i = p-1
    for j in range(p, len(hoge)):
        if int(hoge[j][1]) <= x:
            i += 1
            tmp = hoge[i]
            hoge[i] = hoge[j]
            hoge[j] = tmp
    return i
    
def qSort(hoge, p, r):
    if p < r:
        q = partition(hoge, p, r)
        qSort(hoge, p, q-1)
        qSort(hoge, q+1, r)
    
def isStable(input_array, output_array):
    N = len(input_array)
    for i in range(1, N-1):
        for j in range(i+1, N):
            for a in range(1, N-1):
                for b in range(a+1, N):
                    if input_array[i][1] == output_array[j][1] and input_array[i] == output_array[b] and input_array[j] == output_array[a]:
                        return 'Not stable'
    return 'Stable'
    
if __name__ == '__main__':
    num = int(input())
    hoge = list()
    for _ in range(num):
        c, s = input().split()
        hoge.append(c+s)
    origin = hoge[:]
    qSort(hoge, 0, len(hoge))
    print(isStable(origin, hoge))
    print('\n'.join(['{0[0]} {0[1]}'.format(x) for x in hoge]))

Submission status: Runtime Error

Solution code:
import sys
def t(A,p,r):
 x=A[r][1];i=p-1
 for j in range(p,r):
  if A[j][1]<=x:i+=1;A[i],A[j]=A[j],A[i]
 A[i+1],A[r]=A[r],A[i+1]
 return i+1
def k(A,p,r):
 if p<r:q=t(A,p,r);k(A,p,q-1);k(A,q+1,r)
def s(A):
 for i in range(n-1):
  if A[i][1]==A[i+1][1]:
   if A[i][2]>A[i+1][2]:return 0
 return 1
n=int(input())
f=lambda x,i:(x[0],int(x[1]),i)
A=[f(sys.stdin.readline().split(),i))for i in range(n)]
k(A,0,n-1)
print(['Not s','S'][s(A)]+'table')
for e in A:print(*e[:2])

Submission status: Runtime Error

Solution code:
import sys
def t(A,p,r):
 x=A[r][1];i=p-1
 for j in range(p,r):
  if A[j][1]<=x:i+=1;A[i],A[j]=A[j],A[i]
 A[i+1],A[r]=A[r],A[i+1]
 return i+1
def k(A,p,r):
 if p<r:q=t(A,p,r);k(A,p,q-1);k(A,q+1,r)
def s(A):
 for i in range(n-1):
  if A[i][1]==A[i+1][1]and A[i][2]>A[i+1][2]:return'Not s'
 return'S'
n=int(input())
A=[(e[0],int(e[2:]),i)for i,e in enumerate(sys.stdin)]
k(A,0,n-1)
print(s(A)+'table')
print("\n".join(f"{a} {b}"for a,b in A[:2]))

Submission status: Runtime Error

Solution code:
import sys
def t(A,p,r):
 x=A[r][1];i=p-1
 for j in range(p,r):
  if A[j][1]<=x:i+=1;A[i],A[j]=A[j],A[i]
 A[i+1],A[r]=A[r],A[i+1]
 return i+1
def k(A,p,r):
 if p<r:q=t(A,p,r);k(A,p,q-1);k(A,q+1,r)
def s(A):
 for i in range(n-1):
  if A[i][1]==A[i+1][1]and A[i][2]>A[i+1][2]:return'Not s'
 return'S'
def m():
 n=int(input())
 A=[(e[0],int(e[2:]),i)for i,e in enumerate(sys.stdin)]
 k(A,0,n-1)
 print(s(A)+'table')
 print('\n'.join(f'{a} {b}'for a,b,_ in A))
if'__main__'==__name__:m()

Submission status: Runtime Error

Solution code:
def partition(A, left, right):
    p = left
    r = right
    x = A[r][1]
    i = p - 1
    for j in range(p, r):
        if A[j][1] <= x:
            i += 1
            key = A[i]
            A[i] = A[j]
            A[j] = key
    key = A[i + 1]
    A[i + 1] = A[r]
    A[r] = key
    if i + 1 - left <= 1:
        if right - (i + 1) == 1:
            return i + 1, A[left:right + 1]
        else:
            return i + 1, A[left:i + 2]
    else:
        return i + 1, A[i + 1:i + 2]

def quickSort(A, left, right):
    list = []
    if left < right:
        Q = partition(A, left, right)
        q = Q[0]
        list.extend(quickSort(A, left, q - 1))
        list.extend(Q[1])
        list.extend(quickSort(A, q + 1, right))
    return list

def bubleSort(a):
    for i in range(len(a)):
        for j in range(len(a)-1, i, -1):
            if a[j][1] < a[j-1][1]:
                a[j], a[j-1] = a[j-1], a[j]

    return a

n = int(input())
T = [('0', 0)]
for i in range(n):
    a, b = map(str, input().split())
    x = int(b)
    T.append((a, x))
T1 = T[1:]
T2 = quickSort(T, 1, n)

if T2 == bubleSort(T1):
    print('Stable')
else:
    print('Not stable')

for i in range(n):
    a = str(T2[1])
    print(T2[i][0] + ' ' + str((T2[i][1])))

Submission status: Runtime Error

Solution code:
def partition(A, left, right):
    p = left
    r = right
    x = A[r][1]
    i = p - 1
    for j in range(p, r):
        if A[j][1] <= x:
            i += 1
            key = A[i]
            A[i] = A[j]
            A[j] = key
    key = A[i + 1]
    A[i + 1] = A[r]
    A[r] = key
    if i + 1 - left <= 1:
        if right - (i + 1) == 1:
            return i + 1, A[left:right + 1]
        else:
            return i + 1, A[left:i + 2]
    else:
        return i + 1, A[i + 1:i + 2]

def quickSort(A, left, right):
    list = []
    if left < right:
        Q = partition(A, left, right)
        q = Q[0]
        list.extend(quickSort(A, left, q - 1))
        list.extend(Q[1])
        list.extend(quickSort(A, q + 1, right))
    return list

def bubleSort(a):
    for i in range(len(a)):
        for j in range(len(a)-1, i, -1):
            if a[j][1] < a[j-1][1]:
                a[j], a[j-1] = a[j-1], a[j]

    return a

n = int(input())
T = [('0', 0)]
for i in range(n):
    a, b = map(str, input().split())
    x = int(b)
    T.append((a, x))
T1 = T[1:]
T2 = quickSort(T, 1, n)

if T2 == bubleSort(T1):
    print('Stable')
else:
    print('Not stable')

for i in range(n):
    a = str(T2[1])
    print(T2[i][0] + ' ' + str((T2[i][1])))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Thu May  3 21:24:25 2018
ALDS1_6_C
@author: maezawa
"""
a = []
n = int(input())
for i in range(n):
    s = input().split()
    a.append([s[0], int(s[1])])
    
a_in = a.copy()

def is_stable(a, b):
    s = True
    for i, card in enumerate(b):
        if b[i+1][1] == b[i][1]:
            j = a.index(card)
            k = a.index(b[i+1])
            if j > k:
                s = False
                return s
    return s
    

def partition(a, p, r):
    x = a[r][1]
    i = p - 1
    for j in range(p, r):
        if a[j][1] <= x:
            i += 1
            temp = a[i]
            a[i] = a[j]
            a[j] = temp
    temp = a[i+1]
    a[i+1] = a[r]
    a[r] = temp
    return i+1
    
def quick_sort(a, p, r):
    if p < r:
        q = partition(a, p, r)
        quick_sort(a, p, q-1)
        quick_sort(a, q+1, r)

quick_sort(a, 0, len(a)-1)
if is_stable(a_in, a):
    print('Stable')
else:
    print('Not stable')
for i in range(len(a)):
    print('{} {}'.format(a[i][0],a[i][1]))

Submission status: Runtime Error

Solution code:
INF = 10**10


def partition(ary, p, r):
    x = ary[r][1]
    i = p - 1
    for j in range(p, r):
        if ary[j][1] <= x:
            i += 1
            ary[i], ary[j] = ary[j], ary[i]

    ary[i + 1], ary[r] = ary[r], ary[i + 1]

    return i + 1


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


def merge(ary, left, mid, right):
    lry = ary[left:mid]
    rry = ary[mid:right]
    lry.append(INF)
    rry.append(INF)

    i = 0
    j = 0
    for k in range(left, right):
        if lry[i][1] <= rry[j][1]:
            ary[k] = lry[i]
            i += 1
        else:
            ary[k] = rry[j]
            j += 1

    return right - left


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

    return cnt


if __name__ == '__main__':
    import sys
    from copy import deepcopy

    n = int(input())
    ary = []
    for line in sys.stdin:
        _ = line.strip().split()
        ary.append((_[0], int(_[1])))

    qary = deepcopy(ary)
    mary = deepcopy(ary)
    quick_sort(qary, 0, n - 1)
    merge_sort(mary, 0, n)

    if qary == mary:
        print('Stable')
    else:
        print('Not stable')
    [print(*_) for _ in qary]

Submission status: Runtime Error

Solution code:
#coding:utf-8
from copy import deepcopy
n = int(input())
A = []
for i in range(n):
    ch, num = input().split()
    A.append([ch, int(num)])

B = deepcopy(A)



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

def MergeSort(A, left, right):
    if left+1 < right:
        mid = (left + right)//2
        MergeSort(A, left, mid)
        MergeSort(A, mid, right)
        Merge(A, left, mid, right)

    

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


def quickSort(A, p, r):
    if p < r:
        q = partition(A, p, r)
        quickSort(A, p, q)
        quickSort(A, q+1, r)




        
quickSort(A, 0, n)
MergeSort(B, 0, n)

if A == B:
    print("Stable")
else:
    print("Not stable")
    
for a in A:
    a = " ".join([a[0],str(a[1])])
    print(a)

Submission status: Runtime Error

Solution code:
n = int(input())
numbers = list(map(int, input().split()))

def main() :
    x = numbers[n - 1]
    i = 0
    for j in range(n) :
        if numbers[j] <= x :
            numbers[i], numbers[j] = numbers[j], numbers[i]
            i += 1

    [print(numbers[j] if j != i - 1 else '['+str(numbers[j])+']', end = ' ' if j != n - 1 else '\n') for j in range(n)]

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def partition(A, p, r):
    x = A[r]/10
    i = p-1
    for j in range(p, r):
        if A[j]/10 <= x:
            i += 1
            A[i], A[j] = A[j], A[i]
    i += 1
    A[i], A[r] = A[r], A[i]
    return i

def quicksort(A, p, r):
    if p<r:
        q = partition(A, p, r)
        quicksort(A, p, q-1)
        quicksort(A, q+1, r)
    return

def order(A):
    x = [0 for i in range(len(A))]
    xp = 0
    for i in range(max(A)):
        for e in A:
            if e/10 == i:
                x[xp] = e
                xp += 1
    return x

n = int(input())
x = [[] for i in range(n)]
for i in range(n):
    a, b = raw_input().split()
    x[i] = int(b)*10 + 'SHDC'.index(a)

x1 = x + []
quicksort(x1, 0, n-1)
if order(x) == order(x1):
    print 'Stable'
else:
    print 'Not stable'
for e in x1:
    print 'SHDC'[e % 10], e/10

Submission status: Runtime Error

Solution code:
def partition(A, p, r):
    x = A[r]/10
    i = p-1
    for j in range(p, r):
        if A[j]/10 <= x:
            i += 1
            A[i], A[j] = A[j], A[i]
    i += 1
    A[i], A[r] = A[r], A[i]
    print A,i,r
    return i

def quicksort(A, p, r):
    if p<r:
        q = partition(A, p, r)
        quicksort(A, p, q-1)
        quicksort(A, q+1, r)
    return

def bubblesort(A):
    for i in range(len(A)):
        for j in range(i+1,len(A))[::-1]:
            if A[j] < A[j-1]:
                A[j],A[j-1] = A[j-1], A[j]
    return

n = int(input())
x = [[] for i in range(n)]
for i in range(n):
    a, b = raw_input().split()
    x[i] = int(b)*10 + 'SHDC'.index(a)

x1 = x + []
quicksort(x1, 0, n-1)
x2 = x + []
bubblesort(x2, 0, n-1)

if x1 == x2:
    print 'Stable'
else:
    print 'Not stable'
for e in x1:
    print 'SHDC'[e % 10], e/10

Submission status: WA: Presentation Error

Solution code:
def partition(a, p, r):
    q  = p
    for i in range(p, r):
        if a[i][1] <= a[r][1]:
            a[q], a[i] = a[i], a[q]
            q += 1
    a[q], a[r] = a[r], a[q]
    return q 
 
def quicksort(a, p, r):
    if p < r:
        q = partition(a, p, r)
        quicksort(a, p, q-1)
        quicksort(a, q+1, r)
 
def check_stable(a):
    for i in range(1, len(a)):
        if a[i - 1][1] == a[i][1]:
            if a[i - 1][2] > a[i][2]:
                return 'Not stable'
    return 'Stable '
     
import sys
n = int(sys.stdin.readline())
a = []
for i in range(n):
    suit, num = sys.stdin.readline().split()
    a += [[suit, int(num), i]]
 
quicksort(a, 0, len(a) - 1)
print(check_stable(a))
 
for line in a:
    print(line[0],line[1])