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:
def is_stable(oline, line):
    for i in range(N):
        for j in range(i+1, N):
            for a in range(N):
                for b in range(a+1, N):
                    if (oline[i][1] == oline[j][1]) & (oline[i] == line[b]) & (oline[j] == line[a]):
                        return False
    return True

N = int(input())
line = input().split()
oline = line.copy()
#BubbleSort
for i in range(N):
    for j in range(N-1, i, -1):
        if int(line[j][1]) < int(line[j-1][1]):
            tmp = line[j]
            line[j] = line[j-1]
            line[j-1] = tmp
print(' '.join(line))
print(('Not stabe', 'Stable')[is_stable(oline, line)])

#SelectionSort
line = oline.copy()
for i in range(N):
    minj = i
    for j in range(i+1, N):
        if int(line[minj][1]) > int(line[j][1]):
            minj = j
    tmp = line[i]
    line[i] = line[minj]
    line[minj] = tmp
print(' '.join(line))

print(('Not stabe', 'Stable')[is_stable(oline, line)])

Submission status: Wrong Answer

Solution code:
def is_stable(output_str, same_num_str):
    if len(same_num_str)>0:
        for i in range(len(same_num_str)):
            if output_str.find(same_num_str[i]) == -1:
                return "Not Stable"
    return "Stable"
    
data_num = int(input())
cards = input().split(" ")
cards2 = cards[:]
cnt = 0
num_1 = 0
num_2 = 0
same_num_str = []
output_str = ""
judge_stbl = ""
for i in range(data_num):
    
    for j in range(data_num-1, i,-1):
        if int(cards[j][1:]) < int(cards[j-1][1:]):
            cards[j], cards[j-1] = cards[j-1], cards[j]
        elif int(cards[j][1:]) == int(cards[j-1][1:]):
            same_num_str.append(cards[j-1]+" "+cards[j])    
            
output_str = " ".join(cards) 
judge_stbl = is_stable(output_str, same_num_str)

print(output_str + "\n" + judge_stbl)

same_num_str = []
output_str = ""
judge_stbl = ""

for i in range(data_num):
    min_idx = i
    for j in range(i, data_num):
        if int(cards2[j][1:]) < int(cards2[min_idx][1:]):
            min_idx = j
        elif int(cards2[j][1:]) == int(cards2[min_idx][1:]) and j != min_idx:
            same_num_str.append(cards2[min_idx]+" "+cards2[j])    

    if i != min_idx:
        cards2[i], cards2[min_idx] = cards2[min_idx], cards2[i]

output_str = " ".join(cards2) 
judge_stbl = is_stable(output_str, same_num_str)

print(output_str + "\n" + judge_stbl)

Submission status: Wrong Answer

Solution code:
def isStable(inlist, outlist):
	for i in range(0, N):
		for j in range(i+1, N):
			for a in range(0, N):
				for b in range(a+1, N):
					if int(inlist[i][1:]) == int(inlist[j][1:]) and inlist[i] == outlist[b] and inlist[j] == outlist[a]:
						return False
						break
				else:
					continue
				break
			else:
				continue
			break
		else:
			continue
		break

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

flag = 1
counter = 0
while flag:
    flag = 0
    for i in reversed(range(1,N)):
        if int(C[i][1:]) < int(C[i-1][1:]):
            C[i], C[i-1] = C[i-1], C[i]
            counter += 1
            flag = 1
print(" ".join(map(str, C)))

if(isStable(A, C)):
	print("Stable")
else:
	print("Not Stable")

counter=0
for i in range(N):
	Br = B[i:]
	Brr = [int(Br[j][1:]) for j in range(N-i)]
	num = Brr.index(min(Brr)) 
	if num != 0:
		B[i], B[(i+num)] = B[(i+num)], B[i]
		counter += 1
print(" ".join(map(str,B)))
if(isStable(A, B)):
	print("Stable")
else:
	print("Not Stable")

Submission status: Wrong Answer

Solution code:
def isStable(inlist, outlist):
	flag = True
	for i in range(0, N):
		for j in range(i+1, N):
			for a in range(0, N):
				for b in range(a+1, N):
					if int(inlist[i][1:]) == int(inlist[j][1:]) and inlist[i] == outlist[b] and inlist[j] == outlist[a]:
						flag = False
						break
				else:
					continue
				break
			else:
				continue
			break
		else:
			continue
		break

	return(flag)
N = int(input())
A = input().split()
B = A[:]
C = A[:]

flag = 1
counter = 0
while flag:
    flag = 0
    for i in reversed(range(1,N)):
        if int(C[i][1:]) < int(C[i-1][1:]):
            C[i], C[i-1] = C[i-1], C[i]
            counter += 1
            flag = 1
print(" ".join(map(str, C)))

if(isStable(A, C)):
	print("Stable")
else:
	print("Not Stable")

counter=0
for i in range(N):
	Br = B[i:]
	Brr = [int(Br[j][1:]) for j in range(N-i)]
	num = Brr.index(min(Brr)) 
	if num != 0:
		B[i], B[(i+num)] = B[(i+num)], B[i]
		counter += 1
print(" ".join(map(str,B)))
if(isStable(A, B)):
	print("Stable")
else:
	print("Not Stable")

Submission status: Wrong Answer

Solution code:
def SelectionSort(A, N):
    for i in range(N):
        minj = i
        for j in range(i, N):
            if A[j][1:] < A[minj][1:]:
                minj = j
        #if i != minj:
            #times += 1
        A[i], A[minj] = A[minj], A[i]
    return A

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

    return A

N = int(input())
A = [str(x) for x in input().split()]
#print(*A)
print(*BubbleSort(A, N))
print('Stable')
print(*SelectionSort(A, N))
if BubbleSort(A, N) == SelectionSort(A, N):
    print('Stable')
else:
    print('Not stable')

Submission status: Wrong Answer

Solution code:
def SelectionSort(A, N):
    for i in range(N):
        minj = i
        for j in range(i, N):
            if A[j][1:] < A[minj][1:]:
                minj = j
        #if i != minj:
            #times += 1
        A[i], A[minj] = A[minj], A[i]
    return A

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

    return A

N = int(input())
A = [str(x) for x in input().split()]
#print(*A)
print(*BubbleSort(A, N))
print('Stable')
print(*SelectionSort(A, N))
if BubbleSort(A, N) == SelectionSort(A, N):
    print('Stable')
else:
    print('Not stable')

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Here your code !
A = int(input())
N = tuple(input().split())
bs=[]
ss=[]
def isStable(a,b):
    leng = int(len(a))
    for i in range(leng):
        for j in range(i+1,leng):
            for x in range(leng):
                for y in range(x+1,leng):
                    if a[i][1]==a[j][1] and a[i]==b[x] and a[j]==b[y]:
                        return "Stable"
    return "Not Stable"
    
def bSort(n,a):
    n = list(n)
    for i in range(a):
        for j in range(a-1,i,-1):
            if int(n[j][1]) < int(n[j-1][1]):
                n[j],n[j-1]=n[j-1],n[j]
    return n
    #print(" ".join(n))

    
def seleSort(n,a):
    n = list(n)
    for i in range(a):
        minj = i
        for j in range(i+1,a):
            if n[j][1]<n[minj][1]:
                minj = j
                
        n[i],n[minj]=n[minj],n[i]
    return n
    #print(" ".join(n))

bs = bSort(N,A)
ss = seleSort(N,A)
print(" ".join(bs))
print(isStable(bs,bs))
print(" ".join(ss))
print(isStable(bs,ss))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Here your code !
A = int(input())
N = tuple(input().split())
bs=[]
ss=[]
def isStable(a,b):
    leng = int(len(a))
    for i in range(leng):
        for j in range(i+1,leng):
            for x in range(leng):
                for y in range(x+1,leng):
                    if a[i][1]==a[j][1] and a[i]==b[x] and a[j]==b[y]:
                        return "Stable"
    return "Not stable"
    
def bSort(n,a):
    n = list(n)
    for i in range(a):
        for j in range(a-1,i,-1):
            if int(n[j][1]) < int(n[j-1][1]):
                n[j],n[j-1]=n[j-1],n[j]
    return n
    #print(" ".join(n))

    
def seleSort(n,a):
    n = list(n)
    for i in range(a):
        minj = i
        for j in range(i+1,a):
            if n[j][1]<n[minj][1]:
                minj = j
                
        n[i],n[minj]=n[minj],n[i]
    return n
    #print(" ".join(n))

bs = bSort(N,A)
ss = seleSort(N,A)
print(" ".join(bs))
print(isStable(bs,bs))
print(" ".join(ss))
print(isStable(bs,ss))

Submission status: Wrong Answer

Solution code:
N = input()
A = map(str,raw_input().split())
Ab = A[:]
As = A[:]

def BubbleSort(A,N):
    for i in range(N):
        flag = 0
        for i in range(1,N):
            j = N - i
            if A[j][1] < A[j-1][1]:
                v = A[j]
                A[j] = A[j-1]
                A[j-1] = v
                flag = 1

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

def Output(A):
    for i in range(N-1):
        print A[i],
    print A[-1]

def Stable(AA):
    l = [AA[i] for i in range(N-1) if AA[i][1] == AA[i+1][1]]
    for item in l:
        for i in range(N-1):
            if A[i][1] == item[1]:
                if A[i] == item:
                    msg = "Stable"
                else:
                    msg = "Not stable"
    print msg

SelectionSort(As,N)
BubbleSort(Ab,N)

Output(As)
Stable(As)
Output(Ab)
Stable(Ab)

Submission status: Wrong Answer

Solution code:
N = input()
A = map(str,raw_input().split())
Ab = A[:]
As = A[:]

def BubbleSort(A,N):
    for i in range(N):
        flag = 0
        for i in range(1,N):
            j = N - i
            if A[j][1] < A[j-1][1]:
                v = A[j]
                A[j] = A[j-1]
                A[j-1] = v
                flag = 1

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

def Output(A):
    for i in range(N-1):
        print A[i],
    print A[-1]

def Stable(AA):
    l = [AA[i] for i in range(N-1) if AA[i][1] == AA[i+1][1]]
    for item in l:
        for i in range(N-1):
            if A[i][1] == item[1]:
                if A[i] == item:
                    msg = "Stable"
                else:
                    msg = "Not stable"
    print msg

SelectionSort(As,N)
BubbleSort(Ab,N)

Output(Ab)
Stable(Ab)
Output(As)
Stable(As)

Submission status: Wrong Answer

Solution code:
N = input()
A = map(str,raw_input().split())
Ab = A[:]
As = A[:]

def BubbleSort(A,N):
    for i in range(N):
        flag = 0
        for i in range(1,N):
            j = N - i
            if A[j][1] < A[j-1][1]:
                v = A[j]
                A[j] = A[j-1]
                A[j-1] = v
                flag = 1

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

def Output(A):
    for i in range(N-1):
        print A[i],
    print A[-1]

def Stable(AA):
    n = []
    for i in range(N-1):
        if AA[i][1] == AA[i+1][1] and AA[1][1] not in n:
            n.append(AA[i][1])
    l = []
    m = []
    for item in n:
        for i in range(N):
            if AA[i][1] == item:
                l.append(AA[i])
            if A[i][1] == item:
                m.append(A[i])
    if l == m:
        msg = "Stable"
    else:
        msg = "Not stable"
    print msg

SelectionSort(As,N)
BubbleSort(Ab,N)

Output(Ab)
Stable(Ab)
Output(As)
Stable(As)

Submission status: Wrong Answer

Solution code:
N = input()
A = map(str,raw_input().split())
Ab = A[:]
As = A[:]

def BubbleSort(A,N):
    for i in range(N):
        flag = 0
        for i in range(1,N):
            j = N - i
            if A[j][1] < A[j-1][1]:
                v = A[j]
                A[j] = A[j-1]
                A[j-1] = v
                flag = 1

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

def Output(A):
    for i in range(N-1):
        print A[i],
    print A[-1]

def Stable(AA):
    n = []
    for i in range(N-1):
        if AA[i][1] == AA[i+1][1] and AA[i][1] not in n:
            n.append(AA[i][1])
    l = []
    m = []
    for item in n:
        for i in range(N):
            if AA[i][1] == item:
                l.append(AA[i])
            if A[i][1] == item:
                m.append(A[i])
    print n
    print l
    print m
    if l == m:
        msg = "Stable"
    else:
        msg = "Not stable"
    print msg

SelectionSort(As,N)
BubbleSort(Ab,N)

Output(Ab)
Stable(Ab)
Output(As)
Stable(As)

Submission status: Wrong Answer

Solution code:
class Card:
    def __init__(self, suit, value):
        self.suit = suit
        self.value = value
    
def selection(ls):
    for i in range(len(ls)):
        minj = i
        
        for j in range(i, len(ls)):
            if ls[j].value < ls[minj].value:
                minj = j
        ls[i], ls[minj] = ls[minj], ls[i]

def bubblesort(ls):
    for i in range(1, len(ls)):
        for j in reversed(range(i, len(ls))):
            if ls[j].value < ls[j-1].value:
                ls[j], ls[j-1] = ls[j-1], ls[j]

def isStable(ls1, ls2):
    if ls1 == ls2:
        print("Stable")
    else:
        print("Not stable")

def printcards(cards):
    for card in cards:
        print(card.suit, card.value, ' ', sep='', end='')

    print('\n', end='')
    
def main():
    N = int(input().rstrip())

    cards = []
    for card in list(input().split(' ')):
        cards.append(Card(card[0], card[1]))

    a = [x for x in cards]
    b = [x for x in cards]
    
    selection(a)
    bubblesort(b)

    printcards(a)
    print("Stable")

    printcards(b)
    isStable(a,b)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
A1 = list(map(str,input().split()))
A2 = A1[:]

def bubbleSort(A, N):
 flag = 0
 i = 0
 while flag == 0:
  flag = 1
  for j in range(N-1, i, -1):
   if A[j][1] < A[j-1][1]:
    A[j], A[j-1] = A[j-1], A[j]
    flag = 0
  i += 1
 return A

def selectSort(A, N):
 for i in range(N):
  minj = i
  for j in range(i, N):
   if A[j][1] < A[minj][1]:
    minj = j
  if A[i][1] != A[minj][1]:
   A[i], A[minj] = A[minj], A[i]
 return A

def stable(Ab, As, N):
 for i in range(N):
  if Ab[i] != As[i]:
   return False
 return True

bubbleSort(A1, N)
selectSort(A2, N)
print(" ".join(map(str, A1)))
print("Stable")
print(" ".join(map(str, A2)))
if stable(A1, A2, N):
 print("Stable")
else:
 print("Not Stable")

Submission status: Wrong Answer

Solution code:
def bubble(A, N):
    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
    return A

def selection(A, N):
    for i in range(0, N):
        minj = i
        for j in range(i, N):
            if int(A[j][-1]) < int(A[minj][-1]):
                minj = j
        if i != minj:
            A[i], A[minj] = A[minj], A[i]
    return A

if __name__ == "__main__":
    N = int(input())
    A = input().split()

    A_bubble = bubble(A[:], N)
    print (*A_bubble)
    print ("Stable")

    A_selection = selection(A[:], N)
    print (*A_selection)
    if A_bubble == A_selection:
        print ("Stable")
    else:
        print ("Not Stable")

Submission status: Wrong Answer

Solution code:
N=int(input())
A=input().split()

B=A[:]
S=A[:]

def BubbleSort(A, N):
    count = 0
    flag = True
    while flag:
        flag = False
        for j in range(N-1, 0, -1):
            if A[j][1] < A[j-1][1]:
                A[j], A[j-1] = A[j-1], A[j]
                count += 1
                flag = True
                
    return A

def SelectionSort(A, N):
    count = 0
    for i in range(N):
        minj = i
        for j in range(i, N):
            if A[j][1] < A[minj][1]:
                minj = j
        A[i], A[minj] = A[minj], A[i] 
        count += 1
    return A

b = BubbleSort(B, N)
s = SelectionSort(S, N)
print(*b)
print("Stable")
print(*s)

if b == s:
    print("Stable")
else:
    print("Not Stable")

Submission status: Wrong Answer

Solution code:
import sys

class Card:
    def __init__(self, kind, num):
        self.kind = kind
        self.num = num
    def __eq__(self, other):
        return (self.kind == other.kind) and (self.num == other.num)
    def __ne__(self, other):
        return not ((self.kind == other.kind) and (self.num == other.num))
    def __str__(self):
        return self.kind + self.num

def list_to_string(array):
    s = ""
    for n in array:
        s += str(n) + " "
    return s.strip()
    
def swap(array, i, j):
    tmp = array[i]
    array[i] = array[j]
    array[j] = tmp

def is_stable(array1, array2):
    if reduce(lambda x, y: x and y, map(lambda x: x[0] == x[1], zip(array1, array2))):
        return "Stable"
    else:
        return "Not stable"

def bubble_sort(array):
    for i in range(0, len(array)):
        for j in reversed(range(i+1, len(array))):
            if array[j].num < array[j-1].num:
                swap(array, j, j-1)
    return array
        
def selection_sort(array):
    for i in range(0, len(array)):
        min_index = i
        for j in range(i, len(array)):
            if array[min_index].num > array[j].num:
                min_index = j
        swap(array, i, min_index)
    return array

def main():
    num = int(sys.stdin.readline().strip())
    array = map(lambda x: Card(x[0], x[1]), sys.stdin.readline().strip().split(" "))

    a = list(array)
    b = list(array)

    print list_to_string(bubble_sort(a))
    print is_stable(a, array)
    print list_to_string(selection_sort(b))
    print is_stable(b, array)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def stable(c, c_b):
    for i in range(N-1):
        for j in range(i, N-1):
            if c_b[i][1] == c_b[j+1][1]:
                index1_cb = c_b.index(c_b[i])
                index2_cb = c_b.index(c_b[j+1])
                index1_c = c.index(c_b[i])
                index2_c = c.index(c_b[j+1])
                if index1_c < index2_c\
                and index1_cb < index2_cb:
                    print("Stable")
                else:
                    print("Not stable")

def print_list(list):
    for i in range(N):
        if i == N-1:
            print(list[i])
        else:
            print(list[i], end=" ")

N = int(input())
c = input().split()
c_b = [s for s in c]
for i in range(N):
    for j in range(N-1, i, -1):
        if c_b[j][1] < c_b[j-1][1]:
            t = c_b[j]
            c_b[j] = c_b[j-1]
            c_b[j-1] = t
print_list(c_b)
stable(c, c_b)
c_s = [s for s in c]
change = "no"
for i in range(N):
    cs_min = int(c_s[i][1])
    for j in range(i+1, N):
        if cs_min > int(c_s[j][1]):
            cs_min = int(c_s[j][1])
            index = c_s.index(c_s[j])
            change = "ok"
    if change == "ok":
        t = c_s[index]
        c_s[index] = c_s[i]
        c_s[i] = t
        change = "no"
print_list(c_s)
stable(c, c_s)

Submission status: Wrong Answer

Solution code:
def print_list(array, N):
    for i in range(N):
        if i == N-1:
            print(array[i])
        else:
            print(array[i], end=" ")
            
def stable_sort(array_a, array, N):
    for i in range(N-1):
        if array[i][1] == array[i+1][1]:
            index_1 = array.index(array[i])
            index_2 = array.index(array[i+1])
            index_a1 = array_a.index(array[i])
            index_a2 = array_a.index(array[i+1])
            #print(index_1, index_2, index_a1, index_a2)
            if index_1 < index_2\
            and index_a1 < index_a2:
                print("Stable")
            else:
                print("Not stable")
                break
        
            
N = int(input())
a = input().split()
b = [s for s in a]
for i in range(N-1):
    for j in range(N-1, i, -1):
        if b[j][1] < b[j-1][1]:
            t = b[j]
            b[j] = b[j-1]
            b[j-1] = t
print_list(b, N)
stable_sort(a, b, N)

c = [s for s in a]
for i in range(N-1):
    min_i = c[i][1]
    change = "no"
    for j in range(i+1, N):
        if min_i > c[j][1]:
            min_i = c[j][1]
            index = c.index(c[j])
            change = "ok"
    if change == "ok":
        t = c[i]
        c[i] = c[index]
        c[index] = t
print_list(c, N)
stable_sort(a, c, N)

Submission status: Wrong Answer

Solution code:
def print_list(array, N):
    for i in range(N):
        if i == N-1:
            print(array[i])
        else:
            print(array[i], end=" ")
            
def stable_sort(array_a, array, N):
    for i in range(N-1):
        if array[i][1] == array[i+1][1]:
            index_1 = array.index(array[i])
            index_2 = array.index(array[i+1])
            index_a1 = array_a.index(array[i])
            index_a2 = array_a.index(array[i+1])
            #print(index_1, index_2, index_a1, index_a2)
            if index_1 < index_2\
            and index_a1 < index_a2:
                print("Stable")
            else:
                print("Not stable")
                break
        else:
            print("Stable")
            
N = int(input())
a = input().split()
b = [s for s in a]
for i in range(N-1):
    for j in range(N-1, i, -1):
        if b[j][1] < b[j-1][1]:
            t = b[j]
            b[j] = b[j-1]
            b[j-1] = t
print_list(b, N)
stable_sort(a, b, N)

c = [s for s in a]
for i in range(N-1):
    min_i = c[i][1]
    change = "no"
    for j in range(i+1, N):
        if min_i > c[j][1]:
            min_i = c[j][1]
            index = c.index(c[j])
            change = "ok"
    if change == "ok":
        t = c[i]
        c[i] = c[index]
        c[index] = t
print_list(c, N)
stable_sort(a, c, N)

Submission status: Wrong Answer

Solution code:
import re


def bubble_sort(C, N):
    for i in range(N):
        for j in reversed(range(1, N)):
            if C[j][1] < C[j-1][1]:
                # 1???????????????????°????????????°??????
                C[j], C[j-1] = C[j-1], C[j]
    return C


def selection_sort(C, N):
    for i in range(N):
        # i ?????????????????¨????????????
        minj = i
        for j in range(i, N):
            # ?????????????????¨??????????°???????????????????
            if C[j][1] < C[minj][1]:
                minj = j
        # ??????????????¨??????????°????????????????????????\????????????
        C[i], C[minj] = C[minj], C[i]
    return C


def is_stable(C, sorted):
    # ????????°???????????????????¨???????????????????????´???????????????????
    c_container = []
    s_container = []
    for i in range(len(C)):
        c_container.append(C[i][0])
        # C ??§ i ????????\?????????????????§????????°????????????????????¢???
        for j in range(i+1, len(C)):
            # ????????°???????????????????????£?????´???
            if C[i][1] == C[j][1]:
                # C ????????????????¨?????????????
                c_container.append(C[j][0])
        # ????????°???????????????????????¨????????´???
        if len(c_container) >= 2:
            # C ??§ i ???????????????????????°?????¨????????°????????????????????¢???
            for k in range(len(sorted)):
                # ????±??????????????????°?????¨????????°???????¨?????????????????????????????????????????????????????´?
                if sorted[k][1] == C[i][1]:
                    s_container.append(sorted[k][0])
            # ??????????????´?????????????????£?????´???
            if c_container != s_container:
                # Not Stable
                return False
        # ?????????????????????
        c_container = []
        s_container = []

    return True


if __name__ == '__main__':
    N = int(input())
    # ??????????????????????????????????????¨?????¨??°?????¨?????????????????????????´?
    cards = input().split()
    C = [[] for i in range(N)]
    for i in range(N):
        # ??????????¨??????¨??°????????????
        symbol_and_num = re.findall(r'(\d+|\D+)', cards[i])
        C[i] = [symbol_and_num[0], int(symbol_and_num[1])]

    # ??????????????????????????????
    bubble_sorted = bubble_sort(C.copy(), N)
    # ?¨??????¨???????????????
    bubble_sorted_cards = [val[0] + str(val[1]) for val in bubble_sorted]
    # ?????????????????????
    print(' '.join(bubble_sorted_cards))
    # stable or not stable
    print('Stable' if is_stable(C, bubble_sorted) else 'Not stable')

    # ?????????????????????
    selection_sorted = selection_sort(C.copy(), N)
    # ?¨??????¨???????????????
    selection_sorted_cards = [val[0] + str(val[1]) for val in selection_sorted]
    # ?????????????????????
    print(' '.join(selection_sorted_cards))
    # stable or not stable
    print('Stable' if is_stable(C, selection_sorted) else 'Not stable')

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    CARD = tuple(input().split())

    sort_ = []
    for i in range(1, 10):
        for s in CARD:
            if str(i) in s:
                sort_.append(s)

    sort_ = tuple(sort_)
    a = list(CARD)
    bsort = tuple(bubbleSort(a, N))
    ssort = tuple(selectionSort(a, N))

    for s in (bsort, ssort):
        print(*s, sep=' ')
        if s == sort_:
            print('Stable')
        else:
            print('Not stable')

def bubbleSort(c, n):
    flag = 1
    while flag:
        flag = 0
        for i in range(1, n):
            if c[i][1] < c[i-1][1]:
                c[i], c[i-1] = c[i-1], c[i]
                flag = 1
    return c

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

main()

Submission status: Wrong Answer

Solution code:
def BubbleSort(C,n):
	for i in range(n):
		for j in range(i+1,n)[::-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 range(n):
		minj = i
		for j in range(i,n):
			if C[j][1] < C[minj][1]:
				minj = j
		C[i],C[minj] = C[minj],C[i]
	return C

def stable(C):
	new = []
	for i in range(1,10):
		Ci = [cc for cc in C if cc[1]==str(i)]
		new += Ci
	return C

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

print(' '.join(BubbleSort(C,n)))
print({True:'Stable',False:'Not stable'}[stable(C)==BubbleSort(C,n)])
print(' '.join(SelectionSort(C,n)))
print({True:'Stable',False:'Not stable'}[stable(C)==SelectionSort(C,n)])

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Here your code !
n=int(input())
cards=input().split()

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

def selection_sort(cards,n):
    for i in range(n):
        minv=i
        for j in range(i,n):
            a=int(cards[minv][1])
            b=int(cards[j][1])
            if a>b:
                minv=j
        cards[minv],cards[i]=cards[i],cards[minv]
    return cards

ans_1=bubble_sort(cards,n)
ans_2=selection_sort(cards,n)

print(*ans_1)
print("Stable")
print(*ans_2)
if ans_1!=ans_2:
    print("Not stable")
else:
    print("Stable")

Submission status: Wrong Answer

Solution code:
# your code goes here
N=int(input())
Ci=[i for i in input().split()]
C=[]
for i in range(N):
   # print(C)
    C.append([])
    C[i].append(Ci[i][0])
  #  print(C)
    C[i].append(int(Ci[i][1]))
Cb=C
fl=1
#print(Cb)
while fl==1:
    fl=0
    for j in range(N-1):
   #     print(Cb)
        if Cb[N-1-j][1]<Cb[N-2-j][1]:
       #     print(Cb)
            t=Cb[N-1-j]
            Cb[N-1-j]=Cb[N-2-j]
            Cb[N-2-j]=t
            fl=1
        #    print(C)
for i in range(N):
    Cb[i][1]=str(Cb[i][1])
 #   print(Cb)
    Cb[i]="".join(Cb[i])
print(" ".join(Cb))
p="stable"
print(p)
C=[]
for i in range(N):
 #   print(C)
    C.append([])
    C[i].append(Ci[i][0])
  #  print(C)
    C[i].append(int(Ci[i][1]))
Co=[]
for i in range(N):
    Co.append([])
  #  print(Co)
    Co[i].append(Ci[i][0])
    Co[i].append(int(Ci[i][1]))
for i in range(N):
    minj=i
    for j in range(i,N):
    #    print(C)
        if C[j][1]<C[minj][1]:
            minj=j
        elif C[j][1]==C[minj][1]:
            k=0
            while k<N and C[j][1]!=Co[k][1]:
                k+=1
            if k<N and C[j][0]!=Co[k][0]:
                p="not stable"
    if i!=minj:
        t=C[i]
        C[i]=C[minj]
        C[minj]=t
   # print(minj)
for i in range(N):
    C[i][1]=str(C[i][1])
    C[i]="".join(C[i])
print(" ".join(C))
print(p)

Submission status: Wrong Answer

Solution code:
# your code goes here
N=int(input())
Ci=[i for i in input().split()]
C=[]
for i in range(N):
   # print(C)
    C.append([])
    C[i].append(Ci[i][0])
  #  print(C)
    C[i].append(int(Ci[i][1]))
Cb=C
fl=1
#print(Cb)
while fl==1:
    fl=0
    for j in range(N-1):
   #     print(Cb)
        if Cb[N-1-j][1]<Cb[N-2-j][1]:
       #     print(Cb)
            t=Cb[N-1-j]
            Cb[N-1-j]=Cb[N-2-j]
            Cb[N-2-j]=t
            fl=1
        #    print(C)
for i in range(N):
    Cb[i][1]=str(Cb[i][1])
 #   print(Cb)
    Cb[i]="".join(Cb[i])
print(" ".join(Cb))
p="Stable"
print(p)
C=[]
for i in range(N):
 #   print(C)
    C.append([])
    C[i].append(Ci[i][0])
  #  print(C)
    C[i].append(int(Ci[i][1]))
Co=[]
for i in range(N):
    Co.append([])
  #  print(Co)
    Co[i].append(Ci[i][0])
    Co[i].append(int(Ci[i][1]))
for i in range(N):
    minj=i
    for j in range(i,N):
    #    print(C)
        if C[j][1]<C[minj][1]:
            minj=j
        elif C[j][1]==C[minj][1]:
            k=0
            while k<N and C[j][1]!=Co[k][1]:
                k+=1
            if k<N and C[j][0]!=Co[k][0]:
                p="Not stable"
    if i!=minj:
        t=C[i]
        C[i]=C[minj]
        C[minj]=t
   # print(minj)
for i in range(N):
    C[i][1]=str(C[i][1])
    C[i]="".join(C[i])
print(" ".join(C))
print(p)