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] <= 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). Input The first line contains an integer 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  ≤ 10 9  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

Solution (Python):
class Card:
	def __init__(self, mark, num, order):
		self.mark = mark
		self.num = num
		self.order = order
	def __str__(self):
		return self.mark + ' ' + str(self.num)

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

def partition(cards, p, r):
	x = cards[r].num
	i = p - 1
	for j in range(p, r):
		if cards[j].num <= x:
			i += 1;
			swap(cards, i, j)
	swap(cards, i + 1, r)
	return i + 1

def swap(cards, i, j):
	tmp = cards[i]
	cards[i] = cards[j]
	cards[j] = tmp

def check_stability(cards):
	for i in range(1, len(cards)):
		c1 = cards[i-1]
		c2 = cards[i]
		if c1.num == c2.num and c1.order > c2.order:
			return False
	return True

n = int(input())
cards = []
for i in range(n):
	mark, num = map(str, input().split())
	cards.append(Card(mark, int(num), i))

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

Solution (Python):
class Card:
	def __init__(self, mark, num, order):
		self.mark = mark
		self.num = num
		self.order = order
	def __str__(self):
		return self.mark + ' ' + str(self.num)

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

def partition(cards, p, r):
	x = cards[r].num
	i = p - 1
	for j in range(p, r):
		if cards[j].num <= x:
			i += 1;
			swap(cards, i, j)
	swap(cards, i + 1, r)
	return i + 1

def swap(cards, i, j):
	tmp = cards[i]
	cards[i] = cards[j]
	cards[j] = tmp

def check_stability(cards):
	for i in range(1, len(cards)):
		c1 = cards[i-1]
		c2 = cards[i]
		if c1.num == c2.num and c1.order > c2.order:
			return False
	return True

n = int(input())
cards = []
for i in range(n):
	mark, num = map(str, input().split())
	cards.append(Card(mark, int(num), i))

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

Solution (Python):

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, int(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])

Solution (Python):
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]
        pos = B.index(i)
    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)

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

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

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

Solution (Python):
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
            cnt = 0
            for j in range(N):
                if A[i] == orig_list[j]:
                    small_idx = j
                    cnt += 1
                elif A[i+1] == orig_list[j]:
                    large_idx = j
                    cnt += 1
                if cnt == 2:
                    break
            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]))

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

Solution (Python):
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, ele_cnt):
    suits = []
    for i in range(first, N):
        if num == l[i][1]:
            suits.append(l[i][0])
        if ele_cnt == len(suits):
            break
    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]
            j = idx
            ele_cnt = 0
            while j < N and num == A[j][1]:
                ele_cnt += 1
                j += 1
            sorted_suits = correct_same_num_suits(A, num, idx, ele_cnt)
            orig_suits = correct_same_num_suits(orig_list, num, 0, ele_cnt)
            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]))

Solution (Python):
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, ele_cnt):
    suits = []
    append_cnt = 0
    for i in range(first, N):
        if num == l[i][1]:
            suits.append(l[i][0])
            append_cnt += 1
        if ele_cnt == append_cnt:
            break
    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]
            j = idx
            ele_cnt = 0
            while j < N and num == A[j][1]:
                ele_cnt += 1
                j += 1
            sorted_suits = correct_same_num_suits(A, num, idx, ele_cnt)
            orig_suits = correct_same_num_suits(orig_list, num, 0, ele_cnt)
            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]))

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

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

Solution (Python):
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):
                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.copy(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]))

Solution (Python):
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
            cnt = 0
            for j in range(N):
                if A[i] == orig_list[j]:
                    small_idx = j
                    cnt += 1
                elif A[i+1] == orig_list[j]:
                    large_idx = j
                    cnt += 1
                if cnt == 2:
                    break
            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.copy(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]))

Solution (Python):
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
            cnt = 0
            for j in range(N):
                if A[i] == orig_list[j]:
                    small_idx = j
                    cnt += 1
                elif A[i+1] == orig_list[j]:
                    large_idx = j
                    cnt += 1
                if cnt == 2:
                    break
            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 = 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]))

Solution (Python):
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)

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

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

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

Solution (Python):
import copy

n = int(input())
A = []
for i in range(n):
    tmp = input().split()
    A.append([tmp[0], int(tmp[1])])
B = copy.deepcopy(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[i], A[j] = A[j], A[i]
    A[i + 1], A[r] = A[r], A[i + 1]
    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)




def merge(A, left, mid, right):
    n1 = mid - left
    n2 = right - mid
    L = [0]*(n1+1)
    R = [0]*(n2+1)
    for i in range(n1):
        L[i] = A[left + i]
    for i in range(n2):
        R[i] = A[mid + i]
    L[n1] = ['nan', float("inf")]
    R[n2] = ['nan', float("inf")]
    i = 0
    j = 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 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)



quick_sort(A, 0, len(A)-1)
merge_sort(B, 0, len(B))

print('Stable' if A == B else 'Not stable')
for x in A:
    print(x[0] + ' ' + str(x[1]))

Solution (Python):
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 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)
n = int(input())
A = []
for _ in range(n):
    a, b = input().split()
    A.append((a, int(b)))
ind = dict((e, i) for i, e in enumerate(A))
quick_sort(A, 0, n - 1)
for i in range(n - 1):
    if A[i][1] == A[i + 1][1]:
        if ind[A[i]] > ind[A[i + 1]]:
            print('Not stable')
            break
else:
    print('Stable')
for i in A:
    print(*i)

Solution (Python):

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 = B[r].value
    i = p - 1  
    for j in range(p, r):
        if B[j].value <= x:
            i += 1
            B_tmp = B[i]
            B[i] = B[j]
            B[j] = B_tmp
    t = B[i+1]
    B[i+1] = B[r]
    B[r] = t
    return i+1


def quickSort(B, n, p, r):
    if p < r:
        q = partition(p, r)
        quickSort(B, n, p, q-1)
        quickSort(B, 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)]


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])))




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)

Solution (Python):
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]     
            i_ins += 1
    array[i_ins], array[end] = array[end], array[i_ins]     
    return i_ins


def quick_sort(array, first, end):
    
    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], int(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()

Solution (Python):
import copy

INF = 10000000000
def merge(A, left, mid, right):
    count = 0
    L = A[left:mid] + [(INF,INF)]
    R = A[mid:right] + [(INF,INF)]
    
    i, j = 0, 0
    for k in range(left, right):
        count = count + 1
        if L[i][1] <= R[j][1]:
            A[k] = L[i]
            i = i + 1
        else:
            A[k] = R[j]
            j = j + 1
    return count

def mergeSort(A, left, right):
    if left + 1 < right:
        mid = (left + right) // 2
        countL = mergeSort(A, left, mid)
        countR = mergeSort(A, mid, right)
        return merge(A, left, mid, right) + countL + countR
    return 0



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)
        
    
n = int(input())
A = [None for i in range(n)]
for i in range(n):
    a, b = input().split()
    A[i] = (a, int(b))

B = copy.deepcopy(A)
quickSort(A, 0, n-1)
mergeSort(B, 0, n)
if A == B:
    print("Stable")
else:
    print("Not stable")

ans = [str(x[0]) +" "+str(x[1]) for x in A]
ans = '\n'.join(ans)
print(ans)

Solution (Python):
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()