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:

N = int(raw_input())
num_list1 = raw_input().split()

num_list2 = num_list1


flag = 1
while flag==1:
    flag =0
    for j in range(N-1,0,-1):
        if int(num_list1[j][1:]) < int(num_list1[j-1][1:]):
            a = num_list1[j]
            num_list1[j] = num_list1[j-1]
            num_list1[j-1] = a
           
            flag =1

print " ".join(num_list1)
print "Stable"


for i in range(0,N,1):
   
    minj =i
    for j in range(i,N,1):
        if int(num_list2[j][1:]) < int(num_list2[minj][1:]):
            minj = j
            
    num_list2[i], num_list2[minj] = num_list2[minj], num_list2[i]
print " ".join(num_list2)


if num_list1 == num_list2: 
    print "Stable"
else:
    print "Not Stable"

Submission status: Wrong Answer

Solution code:
class card(object):
    def __init__(self, str):
        self.symbol = str[0]
        self.num = int(str[1:])
    
    def __lt__(self,obj):
        if(self.num < obj.num):
            return True
        else:
            return False
    def __gt__(self,obj):
        if(self.num > obj.num):
            return True
        else:
            return False

    def display_card(self):
        print("{0}{1}".format(self.symbol,self.num),end="")

n = int(input())
s = input()
org = bub = sel = list(map(card,s.split()))  

cnt = 0

#####################################
"""
Bubble sort(bub[])
"""
for i in range(0,len(bub)):
    for j in range(len(bub)-1,i-1,-1):
        if(bub[j] < bub[j-1]):
            tmp = bub[j]
            bub[j] = bub[j-1]
            bub[j-1] = tmp
"""
selection sort(sel[])
"""

for i in range(0,len(sel)):
    minj = i
    for j in range(i,len(sel)):
        if(sel[j] < sel[minj]):
            minj = j
        tmp = sel[i]
        sel[i] = sel[minj]
        sel[minj] = tmp

            
"""
comp org(org[])
"""

# buble
notStable = 0
for i in range(1,len(bub)):
    if(bub[i-1].num == bub[i].num):
        firstCard = bub[i-1]
        for j in range(0,len(bub)):
            if(org[j].num == firstCard.num):
                if(org[j] == firstCard):
                    pass
                    break
                else:
                    notStable = 1
                    break    
for i in range(0,len(bub)):
    bub[i].display_card()
    if (i==len(bub)-1):
        print("\n",end="")
    else :
        print(" ",end="")
if notStable == 0:
    print("Stable")
else:
    print("Not stable")
# selec
notStable = 0
for i in range(1,len(sel)):
    if(sel[i-1].num == sel[i].num):
        firstCard = sel[i-1]
        for j in range(0,len(sel)):
            if(org[j].num == firstCard.num):
                if(org[j] == firstCard):
                    pass
                    break
                else:
                    notStable = 1
                    break
                
for i in range(0,len(sel)):
    sel[i].display_card()
    if (i==len(sel)-1):
        print("\n",end="")
    else :
        print(" ",end="")
if notStable == 0:
    print("Stable")
else:
    print("Not stable")

Submission status: Wrong Answer

Solution code:

N = int(raw_input())
num_list1 = raw_input().split()

num_list2 = num_list1


flag = 1
while flag==1:
    flag =0
    for j in range(N-1,0,-1):
        if int(num_list1[j][1:]) < int(num_list1[j-1][1:]):
            a = num_list1[j]
            num_list1[j] = num_list1[j-1]
            num_list1[j-1] = a
           
            flag =1

print " ".join(num_list1)
print "Stable"


for i in range(0,N,1):
   
    minj =i
    for j in range(i,N,1):
        if int(num_list2[j][1:]) < int(num_list2[minj][1:]):
            minj = j
            
    num_list2[i], num_list2[minj] = num_list2[minj], num_list2[i]
print " ".join(num_list2)


if num_list1 == num_list2: 
    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):
    m=0
    flag=True
    while(flag):
        flag=False
        for i in range(n-1,0,-1):
            if(a[i-1][1:] > a[i][1:]):
                tmp=a[i-1]
                a[i-1]=a[i]
                m+=1
                a[i]=tmp
                flag=True
                
    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
    return True

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

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):
    m=0
    flag=True
    while(flag):
        flag=False
        for i in range(n-1,0,-1):
            if(a[i-1][1:] > a[i][1:]):
                tmp=a[i-1]
                a[i-1]=a[i]
                m+=1
                a[i]=tmp
                flag=True
                
    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")

Submission status: Wrong Answer

Solution code:
N = int(raw_input())
A = raw_input().split(" ")
Acopy = list(A)

i = 0
for i in range(0, N):
    for j in reversed(range(i, N)):
        if int(A[j][1]) < int(A[j-1][1]):
            temp = A[j]
            A[j] = A[j-1]
            A[j-1] = temp

    i += 1
    
print " ".join(A)
print "Stable"

A = list(Acopy)
flag = 0

for i in range(N):
    minj = i
    for j in range(i, N):
        if int(A[j][1]) < int(A[minj][1]):
            minj = j

    if i != minj:
        temp = A[i]
        A[i] = A[minj]
        A[minj] = temp
        if int(A[j][1]) == int(A[minj][1]):
            flag = 1

print " ".join(A)
if flag > 0:
    print "Not Stable"
else:
    print "Stable"

Submission status: Wrong Answer

Solution code:
import sys


def BubbleSort(C, N):
    for i in range(N):
        for j in reversed(range(i, N)):
            if int(C[j][1]) < int(C[j-1][1]):
                C[j], C[j-1] = C[j-1], C[j]

    print " ".join(C)
    print "Stable"


def SelectionSort(C, N):
    flag = 0
    for i in range(N):
        minj = i
        for j in range(i, N):
            if int(C[j][1]) < int(C[minj][1]):
                minj = j
        if i != minj:
            C[i], C[minj] = C[minj], C[i]
        elif C[i][1] == C[minj][1]:
            flag = 1


    print " ".join(C)
    if flag == 1:
        print "Not Stable"
    else:
        print "Stable"


if __name__ == "__main__":
    lines = sys.stdin.readlines()
    N = int(lines[0])
    C1 = lines[1].strip().split(" ")
    C2 = list(C1)
    BubbleSort(C1, N)
    SelectionSort(C2, N)

Submission status: Wrong Answer

Solution code:
import sys


def BubbleSort(C, N):
    for i in range(N):
        for j in reversed(range(i, N)):
            if int(C[j][1]) < int(C[j-1][1]):
                C[j], C[j-1] = C[j-1], C[j]

    print " ".join(C)
    print "Stable"


def SelectionSort(C, N):
    flag = 0
    for i in range(N):
        minj = i
        for j in range(i, N):
            if int(C[j][1]) < int(C[minj][1]):
                minj = j
        if i != minj:
            C[i], C[minj] = C[minj], C[i]
        elif C[i][1] == C[minj][1]:
            flag = 1


    print " ".join(C)
    if flag == 1:
        print "Not stable"
    else:
        print "Stable"


if __name__ == "__main__":
    lines = sys.stdin.readlines()
    N = int(lines[0])
    C1 = lines[1].strip().split(" ")
    C2 = list(C1)
    BubbleSort(C1, N)
    SelectionSort(C2, N)

Submission status: Wrong Answer

Solution code:
N = int(raw_input())
A = raw_input().split(" ")
Acopy = list(A)

i = 0
for i in range(0, N):
    for j in reversed(range(i, N)):
        if int(A[j][1]) < int(A[j-1][1]):
            temp = A[j]
            A[j] = A[j-1]
            A[j-1] = temp

    i += 1
    
print " ".join(A)
print "Stable"

A = list(Acopy)
flag = 0

for i in range(N):
    minj = i
    for j in range(i, N):
        if int(A[j][1]) < int(A[minj][1]):
            minj = j

    if i != minj:
        temp = A[i]
        A[i] = A[minj]
        A[minj] = temp
        if int(A[i][1]) == int(A[minj][1]):
            flag = 1

print " ".join(A)
if flag > 0:
    print "Not stable"
else:
    print "Stable"

Submission status: Wrong Answer

Solution code:
import sys


def BubbleSort(C, N):
    for i in range(N):
        for j in reversed(range(i, N)):
            if int(C[j][1]) < int(C[j-1][1]):
                C[j], C[j-1] = C[j-1], C[j]

    return C

def SelectionSort(C, N):
    flag = 0
    for i in range(N):
        minj = i
        for j in range(i, N):
            if int(C[j][1]) < int(C[minj][1]):
                minj = j
        if i != minj:
            C[i], C[minj] = C[minj], C[i]
        elif C[i][1] == C[minj][1]:
            flag = 1

    return C


if __name__ == "__main__":
    lines = sys.stdin.readlines()
    N = int(lines[0])
    C1 = lines[1].strip().split(" ")
    C2 = list(C1)
    sorted1 = BubbleSort(C1, N)
    sorted2 = SelectionSort(C2, N)
    print " ".join(sorted1)
    print "Stable"
    print " ".join(sorted2)
    if sorted1 is sorted2:
        print "Stable"
    else:
        print "Not stable"

Submission status: Wrong Answer

Solution code:
import sys


def BubbleSort(C, N):
    for i in range(N):
        for j in reversed(range(i, N)):
            if int(C[j][1]) < int(C[j-1][1]):
                C[j], C[j-1] = C[j-1], C[j]

    return C

def SelectionSort(C, N):
    flag = 0
    for i in range(N):
        minj = i
        for j in range(i, N):
            if int(C[j][1]) < int(C[minj][1]):
                minj = j
        if i != minj:
            C[i], C[minj] = C[minj], C[i]
        elif C[i][1] == C[minj][1]:
            flag = 1

    return C


if __name__ == "__main__":
    lines = sys.stdin.readlines()
    N = int(lines[0])
    C1 = lines[1].strip().split(" ")
    C2 = list(C1)
    sorted1 = BubbleSort(C1, N)
    sorted2 = SelectionSort(C2, N)
    print " ".join(sorted1)
    print "Stable"
    print " ".join(sorted2)
    if " ".join(sorted1) == " ".join(sorted2):
        print "Stable"
    else:
        print "Not stable"

Submission status: Wrong Answer

Solution code:
class card(object):
    def __init__(self, str):
        self.symbol = str[0]
        self.num = int(str[1:])
    
    def __lt__(self,obj):
        if(self.num < obj.num):
            return True
        else:
            return False
    def __gt__(self,obj):
        if(self.num > obj.num):
            return True
        else:
            return False

    def display_card(self):
        print("{0}{1}".format(self.symbol,self.num),end="")

n = int(input())
s = input()
org = list(map(card,s.split()))  

bub = list(org)
sel = list(org)

cnt = 0
#####################################
"""
Bubble sort(bub[])
"""
for i in range(0,len(bub)):
    for j in range(len(bub)-1,i-1,-1):
        if(bub[j] < bub[j-1]):
            tmp = bub[j]
            bub[j] = bub[j-1]
            bub[j-1] = tmp
"""
selection sort(sel[])
"""

for i in range(0,len(sel)-1):
    minj = i
    for j in range(i,len(sel)):
        if(sel[j] < sel[minj]):
            minj = j
    sel[i],sel[minj] = sel[minj],sel[i]
       
"""
comp org(org[])
"""


# buble

for i in range(0,len(bub)):
    bub[i].display_card()
    if (i==len(bub)-1):
        print("\n",end="")
    else :
        print(" ",end="")
print("Stable")
# selec
                
for i in range(0,len(sel)):
    sel[i].display_card()
    if (i==len(sel)-1):
        print("\n",end="")
    else :
        print(" ",end="")
if(bub == sel): 
    print("Stable")
else:
    print("Not stable")

Submission status: Wrong Answer

Solution code:
n = int(input())

cards_b = input().split()
cards_s = list(cards_b)

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

#SelectionSort
for i in range(n):
    min_i = i
    for j in range(i, n):
        if int(cards_s[j][1]) < int(cards_s[min_i][1]):
            min_i = j
    cards_s[i], cards_s[min_i] = cards_s[min_i], cards_s[i]

print(" ".join(cards_b))
print("Stable")
print(" ".join(cards_s))

if cards_b == cards_s:
    print("Stable")
else:
    print("Not Stable")

Submission status: Wrong Answer

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


class Card(object):

    def __init__(self, card):
        self.card = card
        self.suit = card[0]
        self.value = int(card[1:])

    def __str__(self):
        return str(self.card)


def print_array(a):
    print(" ".join(map(str, a)))


def bubblesort(cards, n):
    c = cards[:]
    for i in range(0, n):
        for j in range(n - 1, i - 1, -1):
            if c[j].value < c[j - 1].value:
                (c[j], c[j - 1]) = (c[j - 1], c[j])
    return c


def selectionsort(cards, n):
    c = cards[:]
    for i in range(0, n):
        minj = i
        for j in range(i, n):
            if c[j].value < c[minj].value:
                minj = j
        (c[i], c[minj]) = (c[minj], c[i])
    return c


def main():
    n = int(input())
    cards = list(map(Card, input().split()))
    b = bubblesort(cards, n)
    print_array(b)
    print("Stable")
    s = selectionsort(cards, n)
    print_array(s)
    if b == s:
        print("Stable")
    else:
        print("Not Stable")


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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


class Card(object):

    def __init__(self, card):
        self.card = card
        self.suit = card[0]
        self.value = int(card[1:])

    def __str__(self):
        return str(self.card)


def print_array(a):
    print(" ".join(map(str, a)))


def bubblesort(cards, n):
    c = cards[:]
    for i in range(0, n):
        for j in range(n - 1, i - 1, -1):
            if c[j].value < c[j - 1].value:
                (c[j], c[j - 1]) = (c[j - 1], c[j])
    return c


def selectionsort(cards, n):
    c = cards[:]
    for i in range(0, n):
        minj = i
        for j in range(i, n):
            if c[j].value < c[minj].value:
                minj = j
        (c[i], c[minj]) = (c[minj], c[i])
    return c


def main():
    n = int(input())
    cards = list(map(Card, input().split()))
    b = bubblesort(cards, n)
    print_array(b)
    print("Stable")
    s = selectionsort(cards, n)
    print_array(s)
    if b == s:
        print("Stable")
    else:
        print("Not stable")


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# coding: utf-8

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

def selection_sort(A, N):
    C = list(A)
    for i in xrange(N):
        minj = i
        for j in xrange(i, N):
            if C[j][1] < C[minj][1]:
                minj = j
        C[i], C[minj] = C[minj], C[i]
    return C

def is_stable(a, c, n):
    for i in xrange(n-1):
        if c[i][1] == c[i+1][1]:
            if a.index(c[i]) > a.index(c[i+1]):
                print "Not stable"
                return False
    print "Stable"
    return True

def print_c(c):
    print " ".join(map(lambda c:c[0] + str(c[1]), c))

def main():
    n = input()
    c = map(lambda c:(c[0], int(c[1:])), raw_input().split())
    bubble = bubble_sort(c, n)
    selection = selection_sort(c, n)
    print_c(bubble)
    is_stable(c, bubble, n)
    print_c(selection)
    is_stable(c, selection, n)

main()

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*-

def Selection_Sort(A,n):
    for i in range(n):
        mini = i
        for j in range(i,n): #i以上の要素において最小のAをminiに格納
            if int(A[j][1]) < int(A[mini][1]):
                mini = j
        if A[i] != A[mini]:
            A[i], A[mini] = A[mini], A[i]
    return A
    
def Bubble_Sort(A, n):
    for i in range(n):
        for j in range(n-1,i,-1):
            if int(A[j][1]) < int(A[j-1][1]):
                A[j], A[j-1] = A[j-1], A[j]
    return A
    
n = int(input())
A = input().strip().split()

print (A[1][1])

B = A[:]
A = Bubble_Sort(A,n)
print (' '.join(A))
print ("Stable")
B =Selection_Sort(B,n)
print (' '.join(B))

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

Submission status: Wrong Answer

Solution code:
n, a = int(input()), input().split()
ta = list((int(c[1]), i, c) for i, c in enumerate(a))
ca = sorted(ta)

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

print(*[t[2] for t in ba])
print(('Not s' if ba != ca else 'S') + 'table')

sa = ta[:]
for i in range(n):
    minj = i
    for j in range(i, n):
        if sa[j][0] < sa[minj][0]:
            minj = j
    if minj != i:
        sa[i], sa[minj] = sa[minj], sa[i]

print(*[t[2] for t in sa])
print(('Not s' if sa != ca else 'S') + 'table')

Submission status: Wrong Answer

Solution code:
class Card:
    def __init__(self, mark, num):
        self.mark = mark
        self.num = num

def BubbleSort(C, N):
    for i in range(N):
        for j in reversed(xrange(i, N)):
            if C[j].num < C[j - 1].num:
                tmp = C[j]
                C[j] = C[j - 1]
                C[j - 1] = tmp
    return C

def SelectionSort(C, N):
    for i in range(N):
        minj = i
        for j in range(i, N):
            if C[j].num < C[minj].num:
                minj = j
        tmp = C[i]
        C[i] = C[minj]
        C[minj] = tmp
    return C

n = input()

C = map(str, raw_input().split())

B = []
S = []
for i in range(n):
    B.append(Card(C[i][0], int(C[i][1])))
    S.append(Card(C[i][0], int(C[i][1])))

B = BubbleSort(B, n)
S = SelectionSort(S, n)

for i in range(n):
    print "%s%d" % (B[i].mark, B[i].num) ,
print "\nStable"

for i in range(n):
    print "%s%d" % (S[i].mark, S[i].num) ,
    
for i in range(n):
    if B[i].num != S[i].num or B[i].mark != S[i].mark:
        print "\nNot Stable"
        break
else:
    print "\nStable"

Submission status: Wrong Answer

Solution code:
class Card:
    def __init__(self, mark, num):
        self.mark = mark
        self.num = num

def BubbleSort(C, N):
    for i in range(N):
        for j in reversed(xrange(i, N)):
            if C[j].num < C[j - 1].num:
                tmp = C[j]
                C[j] = C[j - 1]
                C[j - 1] = tmp
    return C

def SelectionSort(C, N):
    for i in range(N):
        minj = i
        for j in range(i, N):
            if C[j].num < C[minj].num:
                minj = j
        tmp = C[i]
        C[i] = C[minj]
        C[minj] = tmp
    return C

n = input()

C = map(str, raw_input().split())

B = []
S = []
for i in range(n):
    B.append(Card(C[i][0], int(C[i][1])))
    S.append(Card(C[i][0], int(C[i][1])))

B = BubbleSort(B, n)
S = SelectionSort(S, n)

print "%s%d" % (B[0].mark, B[0].num) ,
for i in range(1, n):
    print " %s%d" % (B[i].mark, B[i].num) ,
print "\nStable"

print "%s%d" % (S[0].mark, S[0].num) ,
for i in range(1, n):
    print "%s%d" % (S[i].mark, S[i].num) ,
    
for i in range(n):
    if B[i].num != S[i].num or B[i].mark != S[i].mark:
        print "\nNot Stable"
        break
else:
    print "\nStable"

Submission status: Wrong Answer

Solution code:
class Card:
    def __init__(self, mark, num):
        self.mark = mark
        self.num = num

def BubbleSort(C, N):
    for i in range(N):
        for j in reversed(xrange(i, N)):
            if C[j].num < C[j - 1].num:
                tmp = C[j] 
                C[j] = C[j - 1]
                C[j - 1] = tmp
    return C

def SelectionSort(C, N):
    for i in range(N):
        minj = i
        for j in range(i, N):
            if C[j].num < C[minj].num:
                minj = j
        tmp = C[i]
        C[i] = C[minj]
        C[minj] = tmp
    return C

n = input()

C = map(str, raw_input().split())

B = []
S = []
for i in range(n):
    B.append(Card(C[i][0], int(C[i][1])))
    S.append(Card(C[i][0], int(C[i][1])))

B = BubbleSort(B, n)
S = SelectionSort(S, n)

print "%s%d" % (B[0].mark, B[0].num) ,
for i in range(1, n):
    print " %s%d" % (B[i].mark, B[i].num) ,
print "\nStable"

print "%s%d" % (S[0].mark, S[0].num) ,
for i in range(1, n):
    print " %s%d" % (S[i].mark, S[i].num) ,

for i in range(n):
    if B[i].num != S[i].num or B[i].mark != S[i].mark:
        print "\nNot Stable"
        break
else:
    print "\nStable"

Submission status: Wrong Answer

Solution code:
class Card:
    def __init__(self, mark, num):
        self.mark = mark
        self.num = num

def BubbleSort(C, N):
    for i in range(N):
        for j in reversed(xrange(i, N)):
            if C[j].num < C[j - 1].num:
                tmp = C[j]
                C[j] = C[j - 1]
                C[j - 1] = tmp
    return C

def SelectionSort(C, N):
    for i in range(N):
        minj = i
        for j in range(i, N):
            if C[j].num < C[minj].num:
                minj = j
        tmp = C[i]
        C[i] = C[minj]
        C[minj] = tmp
    return C

n = input()

C = map(str, raw_input().split())

B = []
S = []
for i in range(n):
    B.append(Card(C[i][0], int(C[i][1])))
    S.append(Card(C[i][0], int(C[i][1])))

B = BubbleSort(B, n)
S = SelectionSort(S, n)

for i in range(n - 1):
    print "%s%d" % (B[i].mark, B[i].num) ,
print "%s%d" % (B[n - 1].mark, B[n - 1].num)
print "Stable"

for i in range(n - 1):
    print "%s%d" % (S[i].mark, S[i].num) ,
print "%s%d" % (S[n - 1].mark, S[n - 1].num)
for i in range(n):
    if B[i].num != S[i].num or B[i].mark != S[i].mark:
        print "Not Stable"
        break
else:
    print "Stable"

Submission status: Wrong Answer

Solution code:
class Card:
    def __init__(self, mark, num):
        self.mark = mark
        self.num = num

def BubbleSort(C, N):
    for i in range(N):
        for j in reversed(xrange(i, N)):
            if C[j].num < C[j - 1].num:
                tmp = C[j]
                C[j] = C[j - 1]
                C[j - 1] = tmp
    return C

def SelectionSort(C, N):
    for i in range(N):
        minj = i
        for j in range(i, N):
            if C[j].num < C[minj].num:
                minj = j
        tmp = C[i]
        C[i] = C[minj]
        C[minj] = tmp
    return C

n = input()

C = map(str, raw_input().split())

B = []
S = []
for i in range(n):
    B.append(Card(C[i][0], int(C[i][1])))
    S.append(Card(C[i][0], int(C[i][1])))

B = BubbleSort(B, n)
S = SelectionSort(S, n)

for i in range(n - 1):
    print "%s%d" % (B[i].mark, B[i].num) ,
print "%s%d" % (B[n - 1].mark, B[n - 1].num)
print "Stable"

for i in range(n - 1):
    print "%s%d" % (S[i].mark, S[i].num) ,
print "%s%d" % (S[n - 1].mark, S[n - 1].num)
for i in range(n):
    if B[i].num != S[i].num or B[i].mark != S[i].mark:
        print "Not stable"
        break
else:
    print "Stable"

Submission status: Wrong Answer

Solution code:
def BubbleSort(C, N):
    for i in xrange(0,N):
        for j in xrange(N-1,i,-1):
            if int(C[j][1]) < int(C[j-1][1]):
                tmp = C[j]
                C[j] = C[j-1]
                C[j-1] = tmp
    return C

def SelectionSort(C, N):
    for i in xrange(0,N):
        minj = i
        for j in xrange(i,N):
            if int(C[j][1]) < int(C[j-1][1]):
                minj = j
        tmp = C[i]
        C[i] = C[minj]
        C[minj] = tmp
    return C

def isStable(Cin, Cout):
    for i in xrange(1,len(Cin)):
        for j in xrange(i+1, len(Cin)):
            for a in xrange(1, len(Cin)):
                for b in xrange(i+1, len(Cin)):
                    if (int(Cin[i][1]) == int(Cout[j][1])) & (Cin[i] == Cout[b]) & (Cin[j] == Cout[a]):
                        return False
    return True

def main():
    N = int(raw_input())
    A = raw_input().split()
    BA = BubbleSort(A, N)
    SA = SelectionSort(A, N)
    print ' '.join(BA)
    if isStable(A, BA):
        print "Stable"
    else:
        print "Not stable"
    print ' '.join(SA)
    if isStable(A, SA):
        print "Stable"
    else:
        print "Not stable"

    return 0

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def BubbleSort(C, N):
    for i in xrange(0,N):
        for j in xrange(N-1,i,-1):
            if int(C[j][1]) < int(C[j-1][1]):
                tmp = C[j]
                C[j] = C[j-1]
                C[j-1] = tmp
    return C

def SelectionSort(C, N):
    for i in xrange(0,N):
        minj = i
        for j in xrange(i,N):
            if int(C[j][1]) < int(C[minj][1]):
                minj = j
        if minj != i:
            tmp = C[i]
            C[i] = C[minj]
            C[minj] = tmp
    return C

def isStable(Cin, Cout):
    for i in xrange(1,len(Cin)):
        for j in xrange(i+1, len(Cin)):
            for a in xrange(1, len(Cin)):
                for b in xrange(i+1, len(Cin)):
                    if (int(Cin[i][1]) == int(Cout[j][1])) & (Cin[i] == Cout[b]) & (Cin[j] == Cout[a]):
                        return False
    return True

def main():
    N = int(raw_input())
    A = raw_input().split()
    BA = BubbleSort(A, N)
    SA = SelectionSort(A, N)
    print ' '.join(BA)
    if isStable(A, BA):
        print "Stable"
    else:
        print "Not stable"
    print ' '.join(SA)
    if isStable(A, SA):
        print "Stable"
    else:
        print "Not stable"

    return 0

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
class card:
    def __init__(self, types, value):
        self.types = types
        self.value = value

def bubbleSort(A, N):
    for i in range(0, N):
        for j in range(N - 1, i - 1, -1):
            if A[j].value < A[j - 1].value:
                exchange(A, j - 1, j)
    printCards(A)
    print "Stable"

def selectionSort(A, N):
    origin = A[:]
    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:
            exchange(A, i, minj)
    printCards(A)
    if checkStable(origin, A, N):
        print "Stable"
    else:
        print "Not stable"

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

def printCards(cards):
    print " ".join(t.types + str(t.value) for t in cards)

def checkStable(origin, sorted, N):
    for i in range(0, N - 1):
        if sorted[i].value == sorted[i + 1].value:
            if origin.index(sorted[i]) > origin.index(sorted[i + 1]):
                return False
    return True

if __name__ == "__main__":
    N = input()
    A = [card(c[0], int(c[1])) for c in raw_input().split()]
    bubbleSort(A[:], N)
    selectionSort(A[:], N)

Submission status: Wrong Answer

Solution code:
class card:
    def __init__(self, types, value):
        self.types = types
        self.value = value

def bubbleSort(A, N):
    for i in range(0, N):
        for j in range(N - 1, i, -1):
            if A[j].value < A[j - 1].value:
                exchange(A, j - 1, j)
        printCards(A)
    printCards(A)
    print "Stable"

def selectionSort(A, N):
    origin = A[:]
    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:
            exchange(A, i, minj)
    printCards(A)
    if checkStable(origin, A, N):
        print "Stable"
    else:
        print "Not stable"

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

def printCards(cards):
    print " ".join(t.types + str(t.value) for t in cards)

def checkStable(origin, sorted, N):
    for i in range(0, N - 1):
        if sorted[i].value == sorted[i + 1].value:
            if origin.index(sorted[i]) > origin.index(sorted[i + 1]):
                return False
    return True

if __name__ == "__main__":
    N = input()
    A = [card(c[0], int(c[1])) for c in raw_input().split()]
    bubbleSort(A[:], N)
    selectionSort(A[:], N)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

N = int(raw_input())
A = map(str, raw_input().split())
B = A[:]
for i in range(N):
    for j in range(N-1, i, -1):
        if int(A[j][1]) < int(A[j-1][1]):
            A[j], A[j-1] = A[j-1], A[j]
bubble = " ".join(A)
for i in range(N):
    minj = i
    for j in range(i, N):
        if int(B[j][1]) < int(B[minj][1]):
            minj = j
    B[i], B[minj] = B[minj], B[i]
select = " ".join(B)
print bubble
print "Stable"
print select
if bubble == select:
    print "Stable"
else:
    print "Not Stable"