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: Wrong Answer

Solution code:

class Card:
	def __init__(self, m, n, o):
		self.m = m
		self.n = n
		self.o = o
	def __str__(self):
		return str(self.m + ' ' + str(self.n))
	
def swap(a, i, j):
	tmp = a[i]
	a[i] = a[j]
	a[j] = tmp

def partition(a, f, c):
	x = a[c].n
	i = f - 1
	for j in range(f, c):
		if a[j].n <= x:
			i += 1
			swap(a, i, j)
	swap(a, i + 1, c)
	return i + 1

def quick_sort(a, f, c):
	if (f < c):
		q = partition(a, f, c)
		quick_sort(a, f, q - 1)
		quick_sort(a, q + 1, c)

def check_stability(a):
	for i in range(1, len(a)):
		c1 = a[i-1]
		c2 = a[i]
		if (c1.n == c2.n and c1.o > c2.o):
			return False
		return True
		
n = int(input())
cards = []
for i in range(n):
	m, num = map(str, input().split())
	cards.append(Card(m, num, i))

quick_sort(cards, 0, n - 1)
if (check_stability(cards)):
	print('Stable')
else:
	print('Not stable')
for i in range(n):
	print(cards[i])

Submission status: Wrong Answer

Solution code:

class Card:
	def __init__(self, m, n, o):
		self.m = m
		self.n = n
		self.o = o
	def __str__(self):
		return str(self.m + ' ' + str(self.n))
	
def swap(a, i, j):
	tmp = a[i]
	a[i] = a[j]
	a[j] = tmp

def partition(a, f, c):
	x = a[c].n
	i = f - 1
	for j in range(f, c):
		if a[j].n <= x:
			i += 1
			swap(a, i, j)
	swap(a, i + 1, c)
	return i + 1

def quick_sort(a, f, c):
	if (f < c):
		q = partition(a, f, c)
		quick_sort(a, f, q - 1)
		quick_sort(a, q + 1, c)

def check_stability(a):
	for i in range(1, len(a)):
		c1 = a[i-1]
		c2 = a[i]
		if (c1.n == c2.n and c1.o > c2.o):
			return False
		return True
		
n = int(input())
cards = []
for i in range(n):
	m, num = map(str, input().split())
	cards.append(Card(m, num, i))

quick_sort(cards, 0, n - 1)
if (check_stability(cards) == True):
	print('Stable')
else:
	print('Not stable')
for i in range(n):
	print(cards[i])

Submission status: Wrong Answer

Solution code:

class Card:
	def __init__(self, m, n, o):
		self.m = m
		self.n = n
		self.o = o
	def __str__(self):
		return str(self.m + ' ' + str(self.n))
	
def swap(a, i, j):
	tmp = a[i]
	a[i] = a[j]
	a[j] = tmp

def partition(a, f, c):
	x = a[c].n
	i = f - 1
	for j in range(f, c):
		if a[j].n <= x:
			i += 1
			swap(a, i, j)
	swap(a, i + 1, c)
	return i + 1

def quick_sort(a, f, c):
	if (f < c):
		q = partition(a, f, c)
		quick_sort(a, f, q - 1)
		quick_sort(a, q + 1, c)

def check_stability(a):
	for i in range(1, len(a)):
		c1 = a[i-1]
		c2 = a[i]
		if (c1.n == c2.n and c1.o > c2.o):
			return False
	return True
		
n = int(input())
cards = []
for i in range(n):
	m, num = map(str, input().split())
	cards.append(Card(m, num, i))

quick_sort(cards, 0, n - 1)
if (check_stability(cards) == True):
	print('Stable')
else:
	print('Not stable')
for i in range(n):
	print(cards[i])

Submission status: Wrong Answer

Solution code:

class Card:
	def __init__(self, m, n, o):
		self.m = m
		self.n = n
		self.o = o
	def __str__(self):
		return str(self.m + ' ' + str(self.n))
	
def swap(a, i, j):
	tmp = a[i]
	a[i] = a[j]
	a[j] = tmp

def partition(a, f, c):
	x = a[c].n
	i = f - 1
	for j in range(f, c):
		if a[j].n <= x:
			i += 1
			swap(a, i, j)
	swap(a, i + 1, c)
	return i + 1

def quick_sort(a, f, c):
	if (f < c):
		q = partition(a, f, c)
		quick_sort(a, f, q - 1)
		quick_sort(a, q + 1, c)

def check_stability(a):
	for i in range(1, len(a)):
		c1 = a[i-1]
		c2 = a[i]
		if (c1.n == c2.n and c1.o > c2.o):
			return False
	return True
		
n = int(input())
cards = []
for i in range(n):
	m, num = map(str, input().split())
	cards.append(Card(m, num, i))

quick_sort(cards, 0, n - 1)
if (check_stability(cards) == True):
	print('Stable')
else:
	print('Not stable')
for i in range(n):
	print(cards[i])

Submission status: Wrong Answer

Solution code:
def partition(A, p, r):
    x = A[r][1]
    i = p-1
    for j in range(p,r):
        if A[j][1] <= x:
            i = 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)
    return None
        
n = int(input())
A = [(i,int(j)) for i,j in [input().split() for i in range(n)]]
B = A.copy()
quickSort(A,0,n-1)
num = A[0][1]
pos = B.index(A[0])
ans = "Stable"
for i in A[1:]:
    if i[1] != num:
        num = i[1]
    else:
        if pos > B.index(i):
            ans = "Not stable"
            break
        else:
            pos = B.index(i)
print(ans)
for i,j in A:
    print(i,j)

Submission status: Wrong Answer

Solution code:
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]))

Submission status: Wrong Answer

Solution code:
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]))

Submission status: Wrong Answer

Solution code:
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 correct_same_num_suits(l, num):
    suits = []
    for card in l:
        if num == card[1]:
            suits.append(card[0])
    return suits

def is_stable():
    idx = 0
    while idx < N-1:
        idx_incr_flg = True
        if A[idx][1] == A[idx+1][1]:
            num = A[idx][1]
            sorted_suits = correct_same_num_suits(A, num)
            orig_suits = correct_same_num_suits(orig_list, num)
            print(sorted_suits)
            print(orig_suits)
            if sorted_suits != orig_suits:
                return False
            idx += len(sorted_suits)
            idx_incr_flg = False
        if idx_incr_flg:
            idx += 1
    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]))

Submission status: Wrong Answer

Solution code:
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 correct_same_num_suits(l, num, first):
    suits = []
    for i in range(first, N):
        if num == l[i][1]:
            suits.append(l[i][0])
    return suits

def is_stable():
    idx = 0
    while idx < N-1:
        idx_incr_flg = True
        if A[idx][1] == A[idx+1][1]:
            num = A[idx][1]
            sorted_suits = correct_same_num_suits(A, num, idx)
            orig_suits = correct_same_num_suits(orig_list, num, 0)
            print(sorted_suits)
            print(orig_suits)
            if sorted_suits != orig_suits:
                return False
            idx += len(sorted_suits)
            idx_incr_flg = False
        if idx_incr_flg:
            idx += 1
    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]))

Submission status: Wrong Answer

Solution code:
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 = orig_list[left:mid]
    R = orig_list[mid:right+1]
    L.append(["", INFTY])
    R.append(["", INFTY])
    i, j = 0, 0
    for k in range(left, right+1):
        if L[i][1] <= R[j][1]:
            orig_list[k] = L[i]
            i += 1
        else:
            orig_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])
orig_list = A[:]

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

if A == orig_list:
    print("Stable")
else:
    print("Not stable")

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

Submission status: Wrong Answer

Solution code:
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)

print(A)
print(merge_list)
if A == merge_list:
    print("Stable")
else:
    print("Not stable")

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

Submission status: Wrong Answer

Solution code:
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")
        break
if is_stable:
    print("Stable")

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

Submission status: Wrong Answer

Solution code:

def merge(A, n, left, mid, right):
    n1 = mid - left
    n2 = right - mid
    for i in range(n1):
        L[i] = A[left + i]
    for i in range(n2):
        R[i] = A[mid + i]
    L[n1] = new_list("X", SENTINEL)
    R[n2] = new_list("X", SENTINEL)
    i = 0
    j = 0
    for k in range(left, right):
        if L[i].value <= R[j].value:
            A[k] = L[i]
            i += 1
        else:
            A[k] = R[j]
            j += 1


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


def partition(p, r):
    x = A[r].value
    i = p - 1  # <xの右端
    for j in range(p, r):
        if A[j].value <= x:
            i += 1
            A_tmp = A[i]
            A[i] = A[j]
            A[j] = A_tmp
    t = A[i+1]
    A[i+1] = A[r]
    A[r] = t
    return i+1


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


class new_list:
    def __init__(self, a1, a2):
        self.suit = a1
        self.value = a2


n = int(input())
S_tmp = [input().split() for i in range(n)]
for i in range(n):
    print(S_tmp[i])
A = []
B = []
for i in range(n):
    A.append(new_list(S_tmp[i][0], int(S_tmp[i][1])))
    B.append(new_list(S_tmp[i][0], int(S_tmp[i][1])))

# for i in range(n):
#     print(i, A[i].suit, A[i].value)

L = [''] * (n//2+2)
R = [''] * (n//2+2)
SENTINEL = 2000000000
mergeSort(A, n, 0, n)
quickSort(B, n, 0, n-1)

for i in range(n):
    if A[i].suit != B[i].suit:
        break
if i == n-1:
    print("Stable")
else:
    print("Not Stable")
for i in range(n):
    print(A[i].suit, A[i].value)

Submission status: Wrong Answer

Solution code:

def merge(A, n, left, mid, right):
    n1 = mid - left
    n2 = right - mid
    for i in range(n1):
        L[i] = A[left + i]
    for i in range(n2):
        R[i] = A[mid + i]
    L[n1] = new_list("X", SENTINEL)
    R[n2] = new_list("X", SENTINEL)
    i = 0
    j = 0
    for k in range(left, right):
        if L[i].value <= R[j].value:
            A[k] = L[i]
            i += 1
        else:
            A[k] = R[j]
            j += 1


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


def partition(p, r):
    x = A[r].value
    i = p - 1  # <xの右端
    for j in range(p, r):
        if A[j].value <= x:
            i += 1
            A_tmp = A[i]
            A[i] = A[j]
            A[j] = A_tmp
    t = A[i+1]
    A[i+1] = A[r]
    A[r] = t
    return i+1


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


class new_list:
    def __init__(self, a1, a2):
        self.suit = a1
        self.value = a2


n = int(input())
S_tmp = [input().split() for i in range(n)]
for i in range(n):
    print(S_tmp[i])
A = []
B = []
for i in range(n):
    A.append(new_list(S_tmp[i][0], int(S_tmp[i][1])))
    B.append(new_list(S_tmp[i][0], int(S_tmp[i][1])))

# for i in range(n):
#     print(i, A[i].suit, A[i].value)

L = [''] * (n//2+2)
R = [''] * (n//2+2)
SENTINEL = 2000000000
mergeSort(A, n, 0, n)
quickSort(B, n, 0, n-1)

for i in range(n):
    if A[i].suit != B[i].suit:
        break
if i == n-1:
    print("Stable")
else:
    print("Not stable")
for i in range(n):
    print(A[i].suit, A[i].value)

Submission status: Wrong Answer

Solution code:

def merge(A, n, left, mid, right):
    n1 = mid - left
    n2 = right - mid
    for i in range(n1):
        L[i] = A[left + i]
    for i in range(n2):
        R[i] = A[mid + i]
    L[n1] = new_list("X", SENTINEL)
    R[n2] = new_list("X", SENTINEL)
    i = 0
    j = 0
    for k in range(left, right):
        if L[i].value <= R[j].value:
            A[k] = L[i]
            i += 1
        else:
            A[k] = R[j]
            j += 1


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


def partition(p, r):
    x = A[r].value
    i = p - 1  # <xの右端
    for j in range(p, r):
        if A[j].value <= x:
            i += 1
            A_tmp = A[i]
            A[i] = A[j]
            A[j] = A_tmp
    t = A[i+1]
    A[i+1] = A[r]
    A[r] = t
    return i+1


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


class new_list:
    def __init__(self, a1, a2):
        self.suit = a1
        self.value = a2


n = int(input())
S_tmp = [input().split() for i in range(n)]
for i in range(n):
    print(S_tmp[i])
A = []
B = []
for i in range(n):
    A.append(new_list(S_tmp[i][0], int(S_tmp[i][1])))
    B.append(new_list(S_tmp[i][0], int(S_tmp[i][1])))

# for i in range(n):
#     print(i, A[i].suit, A[i].value)

L = [''] * (n//2+2)
R = [''] * (n//2+2)
SENTINEL = 2000000000
mergeSort(A, n, 0, n)
quickSort(B, n, 0, n-1)

for i in range(n):
    if A[i].suit != B[i].suit:
        break
if i == n-1:
    print("Stable")
else:
    print("Not stable")
for i in range(n):
    print(B[i].suit, B[i].value)

Submission status: Wrong Answer

Solution code:

def merge(A, n, left, mid, right):
    n1 = mid - left
    n2 = right - mid
    for i in range(n1):
        L[i] = A[left + i]
    for i in range(n2):
        R[i] = A[mid + i]
    L[n1] = new_list("X", SENTINEL)
    R[n2] = new_list("X", SENTINEL)
    i = 0
    j = 0
    for k in range(left, right):
        if L[i].value <= R[j].value:
            A[k] = L[i]
            i += 1
        else:
            A[k] = R[j]
            j += 1


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


def partition(p, r):
    x = A[r].value
    i = p - 1  # <xの右端
    for j in range(p, r):
        if A[j].value <= x:
            i += 1
            A_tmp = A[i]
            A[i] = A[j]
            A[j] = A_tmp
    t = A[i+1]
    A[i+1] = A[r]
    A[r] = t
    return i+1


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


class new_list:
    def __init__(self, a1, a2):
        self.suit = a1
        self.value = a2


n = int(input())
S_tmp = [input().split() for i in range(n)]
# for i in range(n):
#     print(S_tmp[i])
A = []
B = []
for i in range(n):
    A.append(new_list(S_tmp[i][0], int(S_tmp[i][1])))
    B.append(new_list(S_tmp[i][0], int(S_tmp[i][1])))

# for i in range(n):
#     print(i, A[i].suit, A[i].value)

L = [''] * (n//2+2)
R = [''] * (n//2+2)
SENTINEL = 2000000000
mergeSort(A, n, 0, n)
quickSort(B, n, 0, n-1)

for i in range(n):
    if A[i].suit != B[i].suit:
        break
if i == n-1:
    print("Stable")
else:
    print("Not stable")
for i in range(n):
    print(B[i].suit, B[i].value)

Submission status: Wrong Answer

Solution code:
import sys


def partition_for_second_element(array, first, end):
    criteria = array[end][1]
    i_ins = first
    for i_cur in range(first, end):
        if array[i_cur][1] <= criteria:
            array[i_ins], array[i_cur] = array[i_cur], array[i_ins]     # swap
            i_ins += 1
    array[i_ins], array[end] = array[end], array[i_ins]     # swap
    return i_ins


def quick_sort(array, first, end):
    #print("quick_sort",first,end,array)
    if first < end:
        criteria_idx = partition_for_second_element(array, first, end)
        quick_sort(array, first, criteria_idx - 1)
        quick_sort(array, criteria_idx + 1, end)


def shcd_order(cards):
    orders = {}
    for card in cards:
        if card[1] not in orders:
            orders[card[1]] = [card[0]]
        else:
            orders[card[1]].append(card[0])
    return orders


def is_identical_order(prev_orders, after_orders):
    for key, value in prev_orders.items():
        if after_orders[key] != value:
            return False
    return True


def main():
    input_lines = sys.stdin.readlines()
    n = int(input_lines[0].rstrip())
    cards = [(x.split()[0], str(x.split()[1])) for x in input_lines[1:]]
    prev_orders = shcd_order(cards)
    quick_sort(cards, 0, n-1)
    after_orders = shcd_order(cards)
    if is_identical_order(prev_orders, after_orders):
        print('Stable')
    else:
        print('Not stable')
    [print(x[0], x[1]) for x in cards]
    return


main()

Submission status: Wrong Answer

Solution code:
def InputData():
    
    sequence_len = int(input())
    sequence = [input().split(" ") for _ in range(sequence_len)]
    sequence_m = [(a[0], int(a[1])) for a in sequence]
    sequence_q = [(a[0], int(a[1])) for a in sequence]
    return sequence_len, sequence_m, sequence_q

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

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

def merge(sequence, left, mid, right):
    n1 = mid - left
    n2 = right - mid
    sequence_left = sequence[left:left + n1]
    sequence_right = sequence[mid:mid + n2]
    sequence_left.append([None, float('inf')])
    sequence_right.append([None, float('inf')])

    left_cnt = 0
    right_cnt = 0
    for i in range(left, right):
        if sequence_left[left_cnt][1] <= sequence_right[right_cnt][1]:
            sequence[i] = sequence_left[left_cnt]
            left_cnt += 1
        else:
            sequence[i] = sequence_right[right_cnt]
            right_cnt += 1

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

def PrintOut(sequence_m, sequence_q):
    if sequence_m == sequence_q:
        print("Stable")
    else:
        print("Not Stable")
    
    for a in sequence_q:
        print(a[0], a[1])

def main():
    [sequence_len, sequence_m, sequence_q] = InputData()
    mergeSort(sequence_m, 0, sequence_len)
    quickSort(sequence_q, 0, sequence_len-1)
    
    PrintOut(sequence_m, sequence_q)

if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
sentinel = str(10**9+1)

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

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

def merge(lst,left,middle,right):
    left_lst  = lst[left:middle]  + [['',sentinel]]
    right_lst = lst[middle:right] + [['',sentinel]]

    i,j = 0,0
    for k in range(left,right):
        if int(left_lst[i][1]) <= int(right_lst[j][1]):
            lst[k] = left_lst[i]
            i += 1
        else:
            lst[k] = right_lst[j]
            j += 1

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


n = int(input())
n_lst = [[i for i in input().split()] for _ in range(n)]
m_lst = list(n_lst)
quick_sort(n_lst,0,n)
merge_sort(m_lst,0,n)
print('Stable' if n_lst == m_lst else 'Not stable')
for ans in n_lst:
    print('{} {}'.format(ans[0],ans[1]))

Submission status: Wrong Answer

Solution code:
sentinel = str(10**9+1)

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

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

def merge(lst,left,middle,right):
    left_lst  =  lst[left:middle] + [['',sentinel]]
    right_lst = lst[middle:right] + [['',sentinel]]

    i,j = 0,0
    for k in range(left,right):
        if int(left_lst[i][1]) <= int(right_lst[j][1]):
            lst[k] = left_lst[i]
            i += 1
        else:
            lst[k] = right_lst[j]
            j += 1

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


n = int(input())
n_lst = [[i for i in input().split()] for _ in range(n)]
m_lst = list(n_lst)
quick_sort(n_lst,0,n)
merge_sort(m_lst,0,n)
print('Stable' if n_lst == m_lst else 'Not stable')
for ans in n_lst:
    print('{} {}'.format(ans[0],ans[1]))

Submission status: Wrong Answer

Solution code:
n = int(input())
data = list(input().split() for _ in range(n))
c = 0

for k in range(n):
    data[k].append(str(k))


def exchange(a, i, j):
    tmp = a[i]
    a[i] = a[j]
    a[j] = tmp
    return a


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 = exchange(a, i, j)
    exchange(a, i+1, r)
    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)


quicksort(data, 0, n-1)

for l in range(n-1):
    if data[l][1] == data[l+1][1]:
        if data[l][2] > data[l+1][2]:
            print('Not stable')
            c = 1
            break

if c == 0:
    print('Stable')

for l in range(n):
    print(data[l][0], data[l][1])

Submission status: Wrong Answer

Solution code:
n = int(input())
data = list(input().split() for _ in range(n))
c = 0

for k in range(n):
    data[k].append(str(k))


def exchange(a, i, j):
    tmp = a[i]
    a[i] = a[j]
    a[j] = tmp
    return a


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 = exchange(a, i, j)
    exchange(a, i+1, r)
    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)


quicksort(data, 0, n-1)

for l in range(n-1):
    if data[l][1] == data[l+1][1]:
        if int(data[l][2]) > int(data[l+1][2]):
            print('Not stable')
            c = 1
            break

if c == 0:
    print('Stable')

for l in range(n):
    print(data[l][0], data[l][1])

Submission status: Wrong Answer

Solution code:
n = int(input())
data = list(input().split() for _ in range(n))
c = 0

for k in range(n):
    data[k].append(str(k))


def exchange(a, i, j):
    tmp = a[i]
    a[i] = a[j]
    a[j] = tmp
    return a


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 = exchange(a, i, j)
    exchange(a, i+1, r)
    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)


quicksort(data, 0, n-1)

for l in range(n-1):
    if int(data[l][1]) == int(data[l+1][1]):
        if int(data[l][2]) > int(data[l+1][2]):
            print('Not stable')
            c = 1
            break

if c == 0:
    print('Stable')

for l in range(n):
    print(data[l][0], data[l][1])

Submission status: Wrong Answer

Solution code:
a = []
n = int(input())
for _ in range(n):
    s, i = input().split()
    a += [(s, int(i))]
print(a)
b = a[:]

def partition(a, left, right):
    standard = a[right][1]
    cnt = left
    for i in range(left, right+1):
        if i == right:
            if a[i][1] <= standard:
                a[i],a[cnt] = a[cnt], a[i] 
        elif a[i][1] <= standard:
            a[cnt],a[i] = a[i], a[cnt]
            cnt += 1
    return cnt

def quickSort(a, left = 0, right = len(a) - 1):
    if 1 <= right - left:
        cnt_ = partition(a, left, right)
        quickSort(a,left, cnt_ - 1)
        quickSort(a,cnt_ + 1, right)

quickSort(a, 0 , len(a)-1)

#bubble sort


for i in range(n-1):
    for j in range(n-1-i):
        if b[j][1] > b[j+1][1]:
            b[j], b[j+1] = b[j+1], b[j]
        

if a == b:
    print('Stable')
else:
    print('Not Stable')

for i in range(n):
    print(*a[i])

Submission status: Wrong Answer

Solution code:
a = []
n = int(input())
for _ in range(n):
    s, i = input().split()
    a += [(s, int(i))]
b = a[:]

def partition(a, left, right):
    standard = a[right][1]
    cnt = left
    for i in range(left, right+1):
        if i == right:
            if a[i][1] <= standard:
                a[i],a[cnt] = a[cnt], a[i] 
        elif a[i][1] <= standard:
            a[cnt],a[i] = a[i], a[cnt]
            cnt += 1
    return cnt

def quickSort(a, left = 0, right = len(a) - 1):
    if 1 <= right - left:
        cnt_ = partition(a, left, right)
        quickSort(a,left, cnt_ - 1)
        quickSort(a,cnt_ + 1, right)

quickSort(a, 0 , len(a)-1)

#bubble sort


for i in range(n-1):
    for j in range(n-1-i):
        if b[j][1] > b[j+1][1]:
            b[j], b[j+1] = b[j+1], b[j]
        

if a == b:
    print('Stable')
else:
    print('Not Stable')

for i in range(n):
    print(*a[i])