Problem:


Stable Sort

Let's arrange a deck of cards. There are totally 36 cards of 4 suits(S, H, C, D) and 9 values (1, 2, ... 9). For example, 'eight of heart' is represented by H8 and 'one of diamonds' is represented by D1.

Your task is to write a program which sorts a given set of cards in ascending order by their values using the Bubble Sort algorithms and the Selection Sort algorithm respectively. These algorithms should be based on the following pseudocode:

BubbleSort(C)
1 for i = 0 to C.length-1
2     for j = C.length-1 downto i+1
3         if C[j].value 

Input

The first line contains an integer N, the number of cards.

N cards are given in the following line. Each card is represented by two characters. Two consecutive cards are separated by a space character.

Output

In the first line, print the arranged cards provided by the Bubble Sort algorithm. Two consecutive cards should be separated by a space character.

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

In the third line, print the arranged cards provided by the Selection Sort algorithm. Two consecutive cards should be separated by a space character.

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

Constraints

1 ≤ N ≤ 36

Sample Input 1

5
H4 C9 S4 D2 C3

Sample Output 1

D2 C3 H4 S4 C9
Stable
D2 C3 S4 H4 C9
Not stable

Sample Input 2

2
S1 H1

Sample Output 2

S1 H1
Stable
S1 H1
Stable



Sample input:
5
H4 C9 S4 D2 C3


Sample output:
D2 C3 H4 S4 C9
Stable
D2 C3 S4 H4 C9
Not stable

Submission status: Wrong Answer

Solution code:
import time
import copy

def BubbleSort(A, N):
    flag = 1
    while flag:
        flag = 0
        for j in reversed(range(1, N)):
            if A[j]['value']<A[j-1]['value']:
                A[j], A[j-1] = A[j-1], A[j]
                flag = 1
    return A

def SelectionSort(A, N):
    for i in range(0, N):
        minj = i
        for j in range(i, N):
            if A[j]['value']<A[minj]['value']:
                minj = j
        if i!=minj:
            A[i], A[minj] = A[minj], A[i]
    return A

def IsStable(answer, target):
    N = len(answer)
    for i in range(N):
        if answer[i]['value'] != target[i]['value']:
            return False
    return True

# Stable Sort
def main():
    N = int(input())
    A = list(
        map(lambda x: {'body': x, 'value': int(x[1])}, input().split())
    )
    A_sortby_bubble = copy.deepcopy(A)
    A_sortby_select = copy.deepcopy(A)
    A_sortby_bubble = BubbleSort(A_sortby_bubble, N)
    A_sortby_select = SelectionSort(A_sortby_select, N)
    print(' '.join(map(lambda x: x['body'],A_sortby_bubble)))
    print('Stable')
    print(' '.join(map(lambda x: x['body'], A_sortby_select)))
    if IsStable(A_sortby_bubble, A_sortby_select):
        print('Stable')
    else:
        print('Not stable') 
    
main()

Submission status: Wrong Answer

Solution code:
import copy

class Card:
    def __init__(self, suit, number):
        self.suit = suit
        self.number = number
        self.view = self.suit + str(self.number)

def bubble_sort(A):
    count = 0
    while True:
        swapped = False
        for i in range(len(A)-1):
            if A[i+1].number < A[i].number:
                A[i+1], A[i] = A[i], A[i+1]
                count += 1
                swapped = True
        if not swapped:
            return count

def selection_sort(A):
    count = 0
    for i in range(len(A)):
        min_value = A[i].number
        min_value_index = i
        # print('- i:', i, 'A[i]', A[i], '-')
        for j in range(i, len(A)):
            # print('j:', j, 'A[j]:', A[j])
            
            if A[j].number < min_value:
                min_value = A[j].number
                min_value_index = j
        # print('min_value', min_value, 'min_value_index', min_value_index)
        if i != min_value_index:
            count += 1
            A[i], A[min_value_index] = A[min_value_index], A[i]
            # print('swap!', A)
        
    return count

n = int(input())
A = []
for row in input().split():
    suit, number = list(row)
    A.append(Card(suit, int(number)))

def is_stable(A, B):
    N = len(A)
    for i_A in range(N-1):
        for j_A in range(i_A+1, N):
            for i_B in range(N-1):
                for j_B in range(i_B+1, N):
                    if A[i_A].number == A[j_A].number and A[i_A].view == B[j_B].view and A[j_A].view == B[i_B].view:
                        return False

    return B

bubble_sort_A = copy.deepcopy(A)
selection_sort_A = copy.deepcopy(A)

bubble_sort(bubble_sort_A)
print(*[elem.view for elem in bubble_sort_A])
if is_stable(A, bubble_sort_A):
    print('Stable')
else:
    print('Not Stable')

selection_sort(selection_sort_A)
print(*[elem.view for elem in selection_sort_A])

if is_stable(A, selection_sort_A):
    print('Stable')
else:
    print('Not Stable')

Submission status: Wrong Answer

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

data1 = a[:]
data2 = a[:]

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

	return(data)

def selection_sort(data):
	for i in range(len(data)):
		minj = i
		for j in range(i,len(data)):
			if(data[j][1] < data[minj][1]):
				minj = j

		data[i],data[minj] = data[minj],data[i]

	return(data)
data1 = bubble_sort(data1)
data2 = selection_sort(data2)
print(*data1)
print("Stable")
print(*data2)
print("Stable" if data1 == data2 else "NotStable")

Submission status: Wrong Answer

Solution code:
def bubblesort(n, a):
    r = a[:]
    for i in range(n):
        for j in range(n-1, i, -1):
            if int(r[j][1]) < int(r[j-1][1]):
                r[j], r[j-1] = r[j-1], r[j]

    print(*r)
    print("Stable")


def selectionsort(n, a):
    r = a[:]
    ret = "Stable"
    for i in range(n):
        tmp = i
        for j in range(i, n):
            if int(r[j][1]) < int(r[tmp][1]):
                tmp = j
        r[i], r[tmp] = r[tmp], r[i]
        if r[i][0] != r[tmp][0] and int(r[i][1]) == int(r[tmp][1]):
            ret = "Not Stable"

    print(*r)
    print(ret)


def main():
    n = int(input())
    a = list(input().split())

    bubblesort(n, a)
    selectionsort(n, a)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

def bubblesort(n, a):
    r = a[:]
    for i in range(n):
        for j in range(n-1, i, -1):
            if int(r[j][1]) < int(r[j-1][1]):
                r[j], r[j-1] = r[j-1], r[j]

    print(*r)
    print("Stable")


def selectionsort(n, a):
    r = a[:]
    ret = "Stable"
    for i in range(n):
        tmp = i
        for j in range(i, n):
            if int(r[j][1]) < int(r[tmp][1]):
                tmp = j
        r[i], r[tmp] = r[tmp], r[i]

    for i in range(n):
        for j in range(i+1, n):
            for k in range(n):
                for l in range(k+1, n):
                    if int(a[i][1]) == int(r[j][1]) and a[i] == r[l] and a[j] == r[k]:
                        ret = "Not Stable"

    print(*r)
    print(ret)


def main():
    n = int(input())
    a = list(input().split())

    bubblesort(n, a)
    selectionsort(n, a)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

def bubblesort(n, a):
    r = a[:]
    for i in range(n):
        for j in range(n-1, i, -1):
            if int(r[j][1]) < int(r[j-1][1]):
                r[j], r[j-1] = r[j-1], r[j]

    print(*r)
    print("Stable")


def selectionsort(n, a):
    r = a[:]
    ret = "Stable"
    for i in range(n):
        tmp = i
        for j in range(i, n):
            if int(r[j][1]) < int(r[tmp][1]):
                tmp = j
        r[i], r[tmp] = r[tmp], r[i]

    for i in range(n):
        for j in range(i+1, n):
            for k in range(n):
                for l in range(k+1, n):
                    if int(a[i][1]) == int(r[j][1]) and a[i] == r[l] and a[j] == r[k]:
                        ret = "Not stable"

    print(*r)
    print(ret)


def main():
    n = int(input())
    a = list(input().split())

    bubblesort(n, a)
    selectionsort(n, a)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = input()
b = raw_input().split()
s = b

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

for i in range(n):
    mini = i
    for j in range(i, n):
        if int(s[j][1]) < int(s[mini][1]):
            mini = j
    s[i], s[mini] = s[mini], s[i]

print(" ".join(b))
print("Stable")
print(" ".join(s))
print("Stable" if b == s else "Not Stable")

Submission status: Wrong Answer

Solution code:
n = input()
b = raw_input().split()
s = list(b)

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

for i in range(n):
    mini = i
    for j in range(i, n):
        if s[j][1] < s[mini][1]:
            mini = j
    s[i], s[mini] = s[mini], s[i]

print(" ".join(b))
print("Stable")
print(" ".join(s))
print("Stable" if b == s else "Not Stable")

Submission status: Wrong Answer

Solution code:
def bubble(c):
	for i in range(len(c)):
		for j in range(len(c)-1,i,-1):
			if int(c[j][-1]) < int(c[j-1][-1]):
				c[j],c[j-1] = c[j-1],c[j]
	return c

def selection(c):
	for i in range(len(c)):
		mini = i
		for j in range(i,len(c)):
			if int(c[j][-1])<int(c[mini][-1]):
				mini = j
	c[i],c[mini]=c[mini],c[i]
	return c

n = int(input())
card = list(map(str,input().split()))
card1 = card
bubble(card)
selection(card1)
print(" ".join(map(str,card)))
print("Stable")
print(" ".join(map(str,card1)))
print("Stable") if card==card1 else ("Not stable")

Submission status: Wrong Answer

Solution code:
def bubble(c):
	for i in range(len(c)):
		for j in range(len(c)-1,i,-1):
			if int(c[j][-1]) < int(c[j-1][-1]):
				c[j],c[j-1] = c[j-1],c[j]
	return c

def selection(c):
	for i in range(len(c)):
		mini = i
		for j in range(i,len(c)):
			if int(c[j][-1])<int(c[mini][-1]):
				mini = j
	c[i],c[mini]=c[mini],c[i]
	return c

n = int(input())
card = list(map(str,input().split()))
card1 = card
bubble(card)
selection(card1)
print(" ".join(map(str,card)))
print("Stable")
print(" ".join(map(str,card1)))
print("Stable") if card==card1 else print("Not stable")

Submission status: Wrong Answer

Solution code:
def bubble(c):
	for i in range(len(c)):
		for j in range(len(c)-1,i,-1):
			if int(c[j][-1]) < int(c[j-1][-1]):
				c[j],c[j-1] = c[j-1],c[j]
	return c

def selection(c):
	for i in range(len(c)):
		mini = i
		for j in range(i,len(c)):
			if int(c[j][-1])<int(c[mini][-1]):
				mini = j
	c[i],c[mini]=c[mini],c[i]
	return c

n = int(input())
card = list(map(str,input().split()))
card1 = card[:]
bubble(card)
selection(card1)
print(" ".join(map(str,card)))
print("Stable")
print(" ".join(map(str,card1)))
print("Stable") if card==card1 else print("Not stable")

Submission status: Wrong Answer

Solution code:
def compare_cards(a, b):
    return a[1] < b[1]
 
def bubble(C):
    for i in range(len(C)):
        for j in range(len(C) - 1, i, -1):
            if compare_cards(C[j], C[j - 1]):
                C[j], C[j - 1] = C[j -1], C[j]
                
def selection(C):
    for i in range(len(C)):
        mini = i
        for j in range(i, len(C)):
            if compare_cards(C[j], C[mini]):
                mini = j
        C[i], C[mini] = C[mini], C[i]
        
def check_stable(originC, sortedC):
    for i in range(len(originC) - 1):
        if not compare_cards(sortedC[i], sortedC[i + 1]):
            if originC.index(sortedC[i]) > originC.index(sortedC[i + 1]):
                return 'Not stable'
    return 'Stable'
 
         
N = int(input())
C = input().split()
 
print(*C)

Submission status: Wrong Answer

Solution code:
def compare_cards(a, b):
    return int(a[1]) < int(b[1])

N = int(input())
C = input().split()

bubbleC = list(C)

#compare_cards(C[0],C[1])
print(*C)

Submission status: Wrong Answer

Solution code:
def bubbleSort(l, n):
    for i in range(n):
        for j in range(i, n-1):
            if l[j][0] > l[j+1][0]:
                l[j], l[j+1] = l[j+1], l[j]
    return l[:]

def selectionSort(l, n):
    for i in range(n):
        minj = i
        for j in range(i+1, n):
            if l[j][0] < l[minj][0]:
                minj = j
        l[i], l[minj] = l[minj], l[i]
    return l[:]

n = int(input())
l = []
for c in input().split():
    l.append((int(c[1]), c[0]))
    
bl = ' '.join([k + str(n) for n, k in bubbleSort(l, n)])
sl = ' '.join([k + str(n) for n, k in selectionSort(l, n)])
print(bl)
print('Stable')
print(sl)
print('Stable' if sl == bl else 'Not stable')

Submission status: Wrong Answer

Solution code:
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_2_C
# Stable Sort
# Result:
import sys

ary_len = int(sys.stdin.readline())
ary_str = sys.stdin.readline().rstrip().split(' ')
ary = map(lambda s: (int(s[1]), s), ary_str)

# a is an array of tupples whose format is like (4, 'C4').
def bubble_sort(a):
    a_len = len(a)
    for i in range(0, a_len):
        for j in reversed(range(i, a_len)):
            if a[j][0] < a[j - 1][0]:
                v = a[j]
                a[j] = a[j - 1]
                a[j - 1] = v

# a is an array of tupples whose format is like (4, 'C4').
def selection_sort(a):
    a_len = len(a)
    for i in range(0, a_len):
        minj = i;
        for j in range(i, a_len):
            if a[j][0] < a[minj][0]:
                minj = j
        if minj != i:
            v = a[i]
            a[i] = a[minj]
            a[minj] = v

def print_ary(a):
    vals = map(lambda s: s[1], a)
    print ' '.join(vals)

def is_stable(before, after):
    length = len(before)
    for i in range(0, length):
        for j in range(i, length):
            if before[i][0] == before[j][0]:
                v1 = before[i][1]
                v2 = before[j][1]
                for k in range(0, length):
                    if after[k][1] == v1:
                        v1_idx_after = k
                        break
                for k in range(0, length):
                    if after[k][1] == v2:
                        v2_idx_after = k
                        break
                if v1_idx_after > v2_idx_after:
                    return False
    return True

def run_sort(ary, sort_fn):
    ary1 = list(ary)
    sort_fn(ary1)
    print_ary(ary1)
    if is_stable(ary, ary1):
        print 'Stable'
    else:
        print 'Not stable'

### main
run_sort(ary, bubble_sort)
run_sort(ary, selection_sort)

Submission status: Wrong Answer

Solution code:
MARK = 0
NUMBER = 1


def bubble_sort(A):
    for i in range(len(A)-1):
        for j in reversed(range(i+1, len(A))):
            if A[j][NUMBER] < A[j - 1][NUMBER]:
                swap(A, j-1, j)


def selection_sort(A):
    for i in range(len(A)):
        mini = i
        for j in range(i, len(A)):
            if A[j][NUMBER] < A[mini][NUMBER]:
                mini = j
        if mini != i:
            swap(A, mini, i)


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


amount = int(input())
cards = input().split()  # list of cards (ex:[H2 S8])
same_numbers = []
isStable_bubble = True
isStable_selection = True

# make tuple of mark and number
for i in range(amount):
    cards[i] = tuple([cards[i][MARK], int(cards[i][NUMBER])])

# make list of (pairs of) cards which have the same number
for i in range(amount-1):
    number = cards[i][NUMBER]
    for j in range(i+1, amount):
        if number == cards[j][NUMBER]:
            same_numbers.append(cards[i])
            same_numbers.append(cards[j])

cards_bubble = list(cards)
cards_selection = list(cards)

bubble_sort(cards_bubble)
selection_sort(cards_selection)

# jugle stable or not
for i in range(len(same_numbers)):
    for j in range(amount):
        if same_numbers[i][NUMBER] == cards_bubble[j][NUMBER]:
            if same_numbers[i] == cards_bubble[j]:
                isStable_bubble = True
            else:
                isStable_bubble = False
        if same_numbers[i][NUMBER] == cards_selection[j][NUMBER]:
            if same_numbers[i] == cards_selection[j]:
                isStable_selection = True
            else:
                isStable_selection = False


print(*[cards_bubble[i][MARK] + str(cards_bubble[i][NUMBER])
        for i in range(amount)])
if isStable_bubble:
    print("Stable")
else:
    print("Not Stable")

print(*[cards_selection[i][MARK] + str(cards_selection[i][NUMBER])
        for i in range(amount)])
if isStable_selection:
    print("Stable")
else:
    print("Not Stable")

Submission status: Wrong Answer

Solution code:
MARK = 0
NUMBER = 1


def bubble_sort(A):
    for i in range(len(A)-1):
        for j in reversed(range(i+1, len(A))):
            if A[j][NUMBER] < A[j - 1][NUMBER]:
                swap(A, j-1, j)


def selection_sort(A):
    for i in range(len(A)):
        mini = i
        for j in range(i, len(A)):
            if A[j][NUMBER] < A[mini][NUMBER]:
                mini = j
        if mini != i:
            swap(A, mini, i)


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


amount = int(input())
cards = input().split()  # list of cards (ex:[H2 S8])
same_numbers = []
isStable_bubble = True
isStable_selection = True

# make tuple of mark and number
for i in range(amount):
    cards[i] = tuple([cards[i][MARK], int(cards[i][NUMBER])])

# make list of (pairs of) cards which have the same number
for i in range(amount-1):
    number = cards[i][NUMBER]
    for j in range(i+1, amount):
        if number == cards[j][NUMBER]:
            same_numbers.append(cards[i])
            same_numbers.append(cards[j])

cards_bubble = list(cards)
cards_selection = list(cards)

bubble_sort(cards_bubble)
selection_sort(cards_selection)

# jugle stable or not
for i in range(len(same_numbers)):
    for j in range(amount):
        if same_numbers[i][NUMBER] == cards_bubble[j][NUMBER]:
            if same_numbers[i] == cards_bubble[j]:
                isStable_bubble = True
            else:
                isStable_bubble = False
        if same_numbers[i][NUMBER] == cards_selection[j][NUMBER]:
            if same_numbers[i] == cards_selection[j]:
                isStable_selection = True
            else:
                isStable_selection = False


print(*[cards_bubble[i][MARK] + str(cards_bubble[i][NUMBER])
        for i in range(amount)])
if isStable_bubble:
    print("Stable")
else:
    print("Not stable")

print(*[cards_selection[i][MARK] + str(cards_selection[i][NUMBER])
        for i in range(amount)])
if isStable_selection:
    print("Stable")
else:
    print("Not stable")

Submission status: Wrong Answer

Solution code:
MARK = 0
NUMBER = 1


def bubble_sort(A):
    for i in range(len(A)-1):
        for j in reversed(range(i+1, len(A))):
            if A[j][NUMBER] < A[j - 1][NUMBER]:
                swap(A, j-1, j)


def selection_sort(A):
    for i in range(len(A)):
        mini = i
        for j in range(i, len(A)):
            if A[j][NUMBER] < A[mini][NUMBER]:
                mini = j
        if mini != i:
            swap(A, mini, i)


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


amount = int(input())
cards = input().split()  # list of cards (ex:[H2 S8])
isStable_selection = True

# make tuple of mark and number
for i in range(amount):
    cards[i] = tuple([cards[i][MARK], int(cards[i][NUMBER])])

cards_bubble = list(cards)
cards_selection = list(cards)

bubble_sort(cards_bubble)
selection_sort(cards_selection)

# judge stable or not (bubble sort is stable)
for i in range(amount-1):
    if cards_selection[i][NUMBER] == cards_selection[i+1][NUMBER]:
        if cards.index(cards_selection[i]) > cards.index(cards_selection[i+1]):
            isStable_selection = False
        else:
            isStable_selection = True

print(*[cards_bubble[i][MARK] + str(cards_bubble[i][NUMBER])
        for i in range(amount)])
print("Stable")

print(*[cards_selection[i][MARK] + str(cards_selection[i][NUMBER])
        for i in range(amount)])
if isStable_selection:
    print("Stable")
else:
    print("Not stable")

Submission status: Wrong Answer

Solution code:
MARK = 0
NUMBER = 1


def bubble_sort(A):
    for i in range(len(A)-1):
        for j in reversed(range(i+1, len(A))):
            if A[j][NUMBER] < A[j - 1][NUMBER]:
                swap(A, j-1, j)


def selection_sort(A):
    for i in range(len(A)):
        mini = i
        for j in range(i, len(A)):
            if A[j][NUMBER] < A[mini][NUMBER]:
                mini = j
        if mini != i:
            swap(A, mini, i)


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


amount = int(input())
cards = input().split()  # list of cards (ex:[H2 S8])
isStable_selection = True

# make tuple of mark and number
for i in range(amount):
    cards[i] = tuple([cards[i][MARK], int(cards[i][NUMBER])])

cards_bubble = list(cards)
cards_selection = list(cards)

bubble_sort(cards_bubble)
selection_sort(cards_selection)

# judge stable or not (bubble sort is stable)
for i in range(amount-1):
    if cards_selection[i][NUMBER] >= cards_selection[i+1][NUMBER]:
        if cards.index(cards_selection[i]) > cards.index(cards_selection[i+1]):
            isStable_selection = False
        else:
            isStable_selection = True

print(*[cards_bubble[i][MARK] + str(cards_bubble[i][NUMBER])
        for i in range(amount)])
print("Stable")

print(*[cards_selection[i][MARK] + str(cards_selection[i][NUMBER])
        for i in range(amount)])
if isStable_selection:
    print("Stable")
else:
    print("Not stable")

Submission status: Wrong Answer

Solution code:
def bubbleSort( cards ):
    n = len( cards )
    for i in range( 0, n ):
        for j in range( n-1 , i, -1 ):
            if int( cards[j][1] ) < int( cards[ j-1 ][1] ):
                cards[j], cards[ j-1 ] = cards[ j-1 ], cards[j]
 
    print( " ".join( map( str, cards ) ) )
 
def selectionSort( cards ):
	n = len( cards )
	for i in range( 0, n ):
		mini = i
		for j in range( i , n ):
			if int( cards[j][1] ) < int( cards[ mini ][1] ):
				mini = j
		if mini != i:
			cnt += 1
			cards[i], cards[ mini ] = cards[ mini ], cards[i]

	print( " ".join( map( str, cards ) ) )


n = int( raw_input( ) )
cards = raw_input( ).split( " " )
cards2 = list( cards )
bubbleSort( cards )
print( "Stable" )
selectionSort( cards )
if " ".join( map( str, cards2 ) ) == " ".join( map( str, cards ) ):
	print( "Stable" )
else:
	print( "Not stable" )

Submission status: Wrong Answer

Solution code:
import collections

Card = collections.namedtuple('Card', ['suit', 'value'])


def bubble_sort(array):
    a = array[::]
    for i in range(len(a)):
        for j in reversed(range(i, len(a))):
            if a[j].value < a[j - 1].value:
                a[j], a[j - 1] = a[j - 1], a[j]
    return a


def selection_sort(array):
    a = array[::]
    for i in range(len(a)):
        min_j = i
        for j in range(i, len(a)):
            if a[j].value < a[min_j].value:
                min_j = j
        a[i], a[min_j] = a[min_j], a[i]
    return a


if __name__ == '__main__':
    n = int(input())
    cards = [Card(suit, int(value)) for suit, value in input().split()]
    stable = sorted(cards, key=lambda a: a.value)

    bubble = bubble_sort(cards)
    print(' '.join(''.join(map(str, card)) for card in bubble))
    print('Stable' if bubble == stable else 'Not stable')

    selection = selection_sort(cards)
    print(' '.join(''.join(map(str, card)) for card in selection))
    print('Stable' if selection == stable else 'Not stable')

Submission status: Wrong Answer

Solution code:
# coding: utf-8

def bubbleSort(C, N):
	for i in xrange(N):
		for j in xrange(N-1, i-1, -1):
			if C[j][1] < C[j-1][1]:
				C[j], C[j-1] = C[j-1], C[j]
	return C

def selectionSort(C, N):
	for i in xrange(N):
		minj = i
		for j in xrange(i, N):
			if C[minj][1] > C[j][1]:
				minj = j
		C[i], C[minj] = C[minj], C[i]
	return C

def main():
	N = input()
	cards = raw_input().split()
	bubble = bubbleSort(cards[:], N)
	selection = selectionSort(cards[:], N)
	print ' '.join(bubble)
	print 'Stable'
	print ' '.join(selection)
	print 'Stable' if selection == bubble else 'Not Stable'


if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
# coding: utf-8

def bubbleSort(C, N):
	for i in xrange(N):
		for j in xrange(N-1, i-1, -1):
			if C[j][1] < C[j-1][1]:
				C[j], C[j-1] = C[j-1], C[j]
	return C

def selectionSort(C, N):
	for i in xrange(N):
		minj = i
		for j in xrange(i, N):
			if C[minj][1] > C[j][1]:
				minj = j
		C[i], C[minj] = C[minj], C[i]
	return C

def main():
	N = input()
	cards = raw_input().split()
	bubble = bubbleSort(cards[:], N)
	selection = selectionSort(cards[:], N)
	print ' '.join(bubble)
	print 'Stable'
	print ' '.join(selection)
	print 'Stable' if selection == bubble else 'Not stable'


if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
n = int(input())
c = input().split()

d = c[:]

# bubblesort
for i in range(n):
    for j in range(n - 1, i, -1):
        if d[j] < d[j - 1]:
            d[j], d[j - 1] = d[j - 1], d[j]
print(*d)
print("Stable")

# selectionsort
for i in range(n):
    minj = i
    for j in range(i, n):
        if c[j] < c[minj]:
            minj = j
    c[i], c[minj] = c[minj], c[i]

print(*c)
print("Stable" if c == d else "Not stable")

Submission status: Wrong Answer

Solution code:
# coding: utf-8

def bubbleSort(C, N):
	for i in xrange(N):
		for j in xrange(N-1, i-1, -1):
			if C[j-1][1] > C[j][1]:
				C[j], C[j-1] = C[j-1], C[j]
	return C

def selectionSort(C, N):
	for i in xrange(N):
		minj = i
		for j in xrange(i, N):
			if C[minj][1] > C[j][1]:
				minj = j
		C[i], C[minj] = C[minj], C[i]
	return C

def main():
	N = input()
	cards = raw_input().split()
	bubble = bubbleSort(cards[:], N)
	selection = selectionSort(cards[:], N)
	print ' '.join(bubble)
	print 'Stable'
	print ' '.join(selection)
	print 'Stable' if selection == bubble else 'Not stable'



if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
# coding: utf-8

def bubbleSort(C, N):
    print C
    for i in xrange(N):
        for j in xrange(N-1, i, -1):
            if C[j-1][1] > C[j][1]:
                C[j], C[j-1] = C[j-1], C[j]
            print C
    return C

def selectionSort(C, N):
    for i in xrange(N):
        minj = i
        for j in xrange(i, N):
            if C[minj][1] > C[j][1]:
                minj = j
        C[i], C[minj] = C[minj], C[i]
    return C

def isStable(in_, out_, N):
    for i in xrange(N):
        for j in xrange(i + 1, N):
            for a in xrange(N):
                for b in xrange(a + 1, N):
                    if in_[i][1] == in_[j][1] and in_[i] == out_[b] and in_[j] == out_[a]:
                        return False
    return True

def main():
    N = input()
    cards = raw_input().split()
    bubble = bubbleSort(cards[:], N)
    selection = selectionSort(cards[:], N)
    print ' '.join(cards)
    print ' '.join(bubble)
    print 'Stable' if isStable(cards, bubble, N) else 'Not stable'
    print ' '.join(selection)
    print 'Stable' if isStable(cards, selection, N) else 'Not stable'



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#coding:utf-8
#1_2_C

def BubbleSort(cards, n):
    for i in range(n):
        for j in range(n-1, i, -1):
            if cards[j][1] < cards[j-1][1]:
                cards[j], cards[j-1] = cards[j-1], cards[j]
    return cards

def SelectionSort(cards, n):
    for i in range(n):
        minj = i
        for j in range(i, n):
            if cards[minj][1] > cards[j][1]:
                minj = j
        cards[i], cards[minj] = cards[minj], cards[i]
    return cards

n = int(input())
cards = input().split()
print(' '.join(BubbleSort(cards, n)))
print('Stable')
print(' '.join(SelectionSort(cards, n)))
if BubbleSort(cards, n) == SelectionSort(cards, n):
    print('Stable')
else:
    print('Not stable')

Submission status: Wrong Answer

Solution code:
#coding:utf-8
#1_2_C

def BubbleSort(cards, n):
    for i in range(n):
        for j in range(n-1, i, -1):
            if cards[j][1] < cards[j-1][1]:
                cards[j], cards[j-1] = cards[j-1], cards[j]
    return cards

def SelectionSort(cards, n):
    for i in range(n):
        print(cards)
        minj = i
        for j in range(i, n):
            if cards[minj][1] > cards[j][1]:
                minj = j
        cards[i], cards[minj] = cards[minj], cards[i]
    return cards

n = int(input())
cardsR = input().split()
cardsS = cardsR[:]

print(BubbleSort(cardsR, n))
print("Stable")
print(SelectionSort(cardsS, n))
print("Stable" if cardsR == cardsS else "Not stable")

Submission status: Wrong Answer

Solution code:
#coding:utf-8
#1_2_C

def BubbleSort(cards, n):
    for i in range(n):
        for j in range(n-1, i, -1):
            if cards[j][1] < cards[j-1][1]:
                cards[j], cards[j-1] = cards[j-1], cards[j]
    return cards

def SelectionSort(cards, n):
    for i in range(n):
        minj = i
        for j in range(i, n):
            if cards[minj][1] > cards[j][1]:
                minj = j
        cards[i], cards[minj] = cards[minj], cards[i]
    return cards

n = int(input())
cardsR = input().split()
cardsS = cardsR[:]

print(BubbleSort(cardsR, n))
print("Stable")
print(SelectionSort(cardsS, n))
print("Stable" if cardsR == cardsS else "Not stable")

Submission status: Wrong Answer

Solution code:
N = int(raw_input())
line = raw_input()
A = line.strip().split(" ")
A2 = line.strip().split(" ")
for i in range(0, N):
    min_j = i
    for j in range(i, N):
        if int(A2[j][1:]) < int(A2[min_j][1:]):
            min_j = j
    A2[i], A2[min_j] = A2[min_j], A2[i]
bubble = " ".join(A2)

flag = True
while flag:
    flag = False
    for j in range(N-1, 0, -1):
        if int(A[j][1:]) < int(A[j-1][1:]):
            A[j], A[j-1] = A[j-1], A[j]
            flag = True
select = " ".join(A)

print bubble
print "Stable"
print select
if bubble == select:
    print "Stable"
else:
    print "Not stable"

Submission status: Wrong Answer

Solution code:
def str_selection_sort(a,n):
    m=0
    for i in range(0,n):
        minj=i
        for j in range(i,n):
            if(int(a[minj][1:]) > int(a[j][1:])):
                minj=j
        if(minj!=i):
            m+=1
            a[minj],a[i] = a[i],a[minj]
        
    print(" ".join(a))
    return a

def str_bubble_sort(a,n):
    for j in range(0,n):
        for i in range(n-1,j,-1):
            if(int(a[i-1][1:]) > int(a[i][1:])):
                a[i],a[i-1]=a[i-1],a[i]

    print(" ".join(a))
    return a

def is_stable(a,b,n):
    for i in range(1,n):
        if(b[i-1][1:]!=b[i][1:]):
            continue
        flag=False
        for j in a:
            if(j[:1]==b[i-1][:1] and (not flag)):
                flag=True
                continue
            if(j[:1]==b[i][:1]):
                if(not flag):
                    return False
                else:
                    return True
        
        if(flag):
            return False

a=[]
n=int(input())
s=input()
a=s.split()
b=str_bubble_sort(list(a),n)
if(is_stable(a,b,n)):
    print("Stable")
else:
    print("Not stable")
b=str_selection_sort(list(a),n)
if(is_stable(a,b,n)):
    print("Stable")
else:
    print("Not stable")