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: Runtime Error

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

bubbleC = C.copy()
selectionC = C.copy()

bubble(bubbleC)
selection(selectionC)

print(*bubbleC)
print(check_stable(C, bubbleC))
print(*selectionC)
print(check_stable(C, selectionC))

Submission status: Runtime Error

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

bubbleC = C.copy()
selectionC = C.copy()

bubble(bubbleC)
#selection(selectionC)
print(*C)

Submission status: Runtime Error

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]

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

bubbleC = C.copy()

bubble(bubbleC)
print(*C)

Submission status: Runtime Error

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]

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

bubbleC = C.copy()

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

Submission status: Runtime Error

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

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

bubbleC = C.copy()

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

Submission status: Runtime Error

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: Runtime Error

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 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
                
    b=list(map(str,a))
    print(" ".join(b))

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: Runtime Error

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

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: Runtime Error

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

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: Runtime Error

Solution code:
STABLE = 'Stable'
UNSTABLE = 'Not stable'


def main():
  card_num = int(input())
  cards = input().split()

  bubble_sorted = bubble_sort(cards, card_num)
  select_sorted = select_sort(cards, card_num)

  stability = True
  for i, c in enumerate(select_sorted):
    if c != bubble_sorted[i]:
      stability = False
      break
  stability = STABLE if stability else UNSTABLE

  print(bubble_sorted)
  print(STABLE)

  print(select_sorted)
  print(stability)

  return 0


def order(card_sym):
  return int(card_sym[1])


def select_sort(elements, element_num):
  elements = list(elements)

  for i in range(0, element_num - 1):
    min_val = order(elements[i + 1])
    min_index = i + 1
    for j in range(i + 1, element_num):
      if order(elements[j]) < order(min_val):
        min_val = elements[j]
        min_index = j
    if order(elements[i]) > order(elements[min_index]):
      elements[i], elements[min_index] = elements[min_index], elements[i]

  return elements


def bubble_sort(elements, element_num):
  elements = list(elements)

  for i in range(1, element_num):
    for j in reversed(range(i, element_num)):
      if order(elements[j - 1]) > order(elements[j]):
        elements[j - 1], elements[j] = elements[j], elements[j - 1]

  return elements


if __name__ == '__main__':
  main()

Submission status: Runtime Error

Solution code:
import copy
N = int(input())
bscs = [(int(c[-1]), c) for c in input().split(" ")]
sscs = copy.copy(bscs)
for i in range(N):
    j = N - 1
    while j > i:
        if bscs[j][0] < bscs[j - 1][0]:
            tmp = bscs[j]
            bscs[j] = bscs[j - 1]
            bscs[j - 1] = tmp
        j -= 1
print(" ".join([c[1] for c in bscs])
print("Stable")
for i in range(N):
    minj = i
    for j in range(i, N):
        if sscs[j][0] < sscs[minj][0]:
            minj = j
    tmp = sscs[i]
    sscs[i] = sscs[minj]
    sscs[minj] = tmp
print(" ".join([c[1] for c in sscs]))
print("Stable" if bscs == sscs else "Not Stable")

Submission status: Runtime Error

Solution code:
length = int(input())
eles = [int(l) for l in input().split()]
is_stable = 'Stable'

import copy
_copy = copy.deepcopy(eles)

for i in range(length):
    for j in range(length-1, 0, -1):
        if _copy[j][1] < _copy[j-1][1]:
            _copy[j], _copy[j-1] = _copy[j-1], _copy[j]

print(*_copy)
print(is_stable)

__copy = copy.deepcopy(eles)

for i in range(length-1):
    _min = i
    for l in range(i, length):
        if __copy[l][1] < __copy[_min][1]:
            _min = l
            is_stable = 'Not Stable'
    __copy[i], __copy[_min] = __copy[_min], __copy[i]

print(*__copy)
print(is_stable)

Submission status: Runtime Error

Solution code:
n = int(input())
lst = input().split()
bub, sel = [lst]*2
sls = []
for i in range(n):
    m = i
    li = lst[i][0]
    if not li in sls:
        sls.append(li)
    for j in range(n-1,i,-1):
        if bub[j] < bub[j-1]:
            bub[j-1:j+1] = bub[j], bub[j-1]
    for k in range(i, n):
        if sel[k][1] < sel[k][1]:
            m = k
    if m != i:
        sel[i], sel[m] = sel[m], sel[i]

def stable(sort):
    s = 0
    for i in range(1,n):
        if sort[i][1] == sort[i-1][1] & sls.index(sort[i][0]) < sls.index(sort[i-1][0]):
            s += 1
    print('Not stable' if s else 'Stable')
    
print(*bub)
stable(bub)
print(*sel)
stable(sel)

Submission status: Runtime Error

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


def bubble_sort(cards, num):
  for left in range(num-1):
        for right in range(left+1, num):
            if cards[right][1] < cards[left][1]:
                cards[left], cards[right] = cards[right], cards[left]

    print(list_to_str(cards))


def selection_sort(cards, num):
    for head in range(num):
        min_i = head

        for target in range(head+1, num):
            if cards[target][1] < cards[min_i][1]:
                min_i = target
        cards[head], cards[min_i] = cards[min_i], cards[head]

    print(list_to_str(cards))


def is_stable(before, after, num):
    for i in range(num):
        for j in range(i+1, num):
            for a in range(num):
                for b in range(a+1, num):
                    if before[i][1] == before[j][1]\
                    and before[i] == after[b]\
                    and before[j] == after[a]:
                        return 'Not stable'
    return 'Stable'


def list_to_str(l, delimiter=' '):
    # The default delimiter is one space.
    return delimiter.join([str(v) for v in l])


if __name__ == '__main__':
    num = int(input())
    cards = input().split()
    cards_for_bubble = cards.copy()
    cards_for_selection = cards.copy()

    bubble_sort(cards_for_bubble, num)
    print(is_stable(cards, cards_for_bubble, num))

    selection_sort(cards_for_selection, num)
    print(is_stable(cards, cards_for_selection, num))

Submission status: Runtime Error

Solution code:
N = int(input())

C = input().split()

B = C[:]
S = C[:]

# bubble sort
flag = 1
while(flag):
    flag = 0
    for x in (1, N):
        if C[x][1:] < C[x-1][1:]:
            C[x], C[x-1] = C[x-1], C[x]
            flag = 1

# sectionSot
for x in range(0,N):
    minj = x
    for j in (x,n):
        if S[j][1:] < S[minj][1:]:
            minj = j
    if minj != x:
        S[x], S[j] = S[j], S[x]

for i in range(1, N):
    v = C[i]
    j = i - 1
    while(j >=0 and C[j] > v):
        C[j+1] = C[j]
        j -= 1
    C[j+1] = v

if(C == B):
    print(" ".join(b for b in B))
    print("Stable")
else:
    print(" ".join(b for b in B))
    print("Not stable")


if(C == S):
    print(" ".join(b for b in S))
    print("Stable")
else:
    print(" ".join(b for b in S))
    print("Not stable")

Submission status: Runtime Error

Solution code:
import sys

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

def	selection_sort(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

fin = open("test.txt", "r")
#fin = sys.stdin

N = int(fin.readline())
A = fin.readline().split()
B = A[:]

A = bubble_sort(A, N)
B = selection_sort(B, N)

print(" ".join(A))
print("Stable")

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

Submission status: Runtime Error

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:]) && inlist[i] == outlist[b] && inlist[j] == outlist[a]:
						return FALSE
						break
	else:
		return TRUE
	
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+1)]
	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: Runtime Error

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"
                    break
                else:
                    msg = "Not stable"
                    break
    print msg

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

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

Submission status: Runtime Error

Solution code:
def bubble_sort(num_list):
    list_b = num_list[:]
    for i in range(len(list_b)-1):
        for j in range(len(list_b)-1,i,-1):
            if list_b[j][1] < list_b[j-1][1]:
                list_b[j],list_b[j-1] = list_b[j-1],list_b[j]
    return list_b

def selection_sort(num_list):
    list_s = num_list[:]
    for i in range(len(list_s)):
        minj = i
        for j in range(i,len(list_s)):
            if list_s[minj][1] > list_s[j][1]:
                minj = j
        list_s[i],list_s[minj] = list_s[minj],list_s[i]
    return list_s

def isStable(list_x, list_y):
    leng = int(len(list_x))
    for i in range(leng-1):
        for j in range(i+1, leng):
            for x in range(leng-1):
                for y in range(x+1, leng):
                    if list_x[i][1]==list_x[j][1] and list_x[i]==list_y[y] and list_x[j] == list_y[x]:
                        return "Not stable"
    return "Stable"

qnt = int(input())
num_list = input().split()

bs = bubble_sort(num_list)
ss = selection_sort(num_list)
print(" ".join(bs))
print(isStable(num_list,bs))
print(" ".join(ss))
print(isStable(num_list,ss)

Submission status: Runtime Error

Solution code:
def bubble_sort(num_list):
    list_b = num_list[:]
    for i in range(len(list_b)-1):
        for j in range(len(list_b)-1,i,-1):
            if list_b[j][1] < list_b[j-1][1]:
                list_b[j],list_b[j-1] = list_b[j-1],list_b[j]
    return list_b


def selection_sort(num_list):
    list_s = num_list[:]
    for i in range(len(list_s)):
        minj = i
        for j in range(i,len(list_s)):
            if list_s[minj][1] > list_s[j][1]:
                minj = j
        list_s[i],list_s[minj] = list_s[minj],list_s[i]
    return list_s


def isStable(list_x, list_y):
    leng = int(len(list_x))
    for i in range(leng-1):
        for j in range(i+1, leng):
            for x in range(leng-1):
                for y in range(x+1, leng):
                    if list_x[i][1]==list_x[j][1] and list_x[i]==list_y[y] and list_x[j] == list_y[x]:
                        return "Not stable"
    return "Stable"


qnt = int(input())
num_list = input().split()

bs = bubble_sort(num_list)
ss = selection_sort(num_list)
print(" ".join(bs))
print(isStable(num_list,bs))
print(" ".join(ss))
print(isStable(num_list,ss)

Submission status: Runtime Error

Solution code:
def buble(lst):

    src_list = list(lst)
    flag=True
    while flag:
        flag = False
        for idx in range(len(lst)-1, 0, -1):
            if int(lst[idx][1]) < int(lst[idx-1][1]):
    flag_stable = True
    for num in range(0, 9):
        tmp1 = []
        tmp2 = []
        for i in range(0, len(lst)):
            if int(lst[i][1]) == num:
                tmp1.append(lst[i])
            if int(src_list[i][1]) == num:
                tmp2.append(src_list[i])

        if tmp1 != tmp2:
           flag_stable = False
           break
    print " ".join(lst)

    if flag_stable:
        print "Stable"
    else:
        print "Not stable"

def selection(lst):

    src_list = list(lst)
    for i in range(len(lst)):
        m = i
        for j in range(i,len(lst)):
            if int(lst[m][1]) > int(lst[j][1]):
                m=j
        tmp=lst[i]
        lst[i]=lst[m]
        lst[m]=tmp

    flag_stable = True
    for num in range(0, 9):
        tmp1 = []
        tmp2 = []
        for i in range(0, len(lst)):
            if int(lst[i][1]) == num:
                tmp1.append(lst[i])
            if int(src_list[i][1]) == num:
                tmp2.append(src_list[i])

        if tmp1 != tmp2:
           flag_stable = False
           break

    print " ".join(lst)
    if flag_stable:
        print "stable"
    else:
        print "not stable"


n=raw_input()
lst1 = raw_input().split()
lst2 = list(lst1)

buble(lst1)
selection(lst2)

Submission status: Runtime Error

Solution code:
n = int(input())
a = input().split()
b = a

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

ra = " ".join(a)

print(ra)
print("Stable")

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

rb = " ".join(b)
print(rb)

if ra == rb:
    print("Stable")
else:
    print("Not Stable")

Submission status: Runtime Error

Solution code:

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

def SelectionSort(N, C):
    C = [] + C
    for i in range(N):
        minj = i
        for j in range(i, N):
            if C[minj][1] > C[j][1]:
                minj = j
        C[i], C[minj] = C[minj], C[i]
    return C

def CompareSuit(L1, L2):
    suit1, suit2 = "", ""
    for i in range(len(L1)):
        suit1 = suit1 + L1[i]
        suit2 = suit2 + L2[i]
    if suit1 == suit2:
        return "Stable"
    else:
        return "Unstable"

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

C1 = BubbleSort(N, C)
C2 = SelectionSort(N, C)

print(" ".join(C1))
print(CompareSuit(C1, C1))
print(" ".join(C2))
print(CompareSuit(C1, C2))

Submission status: Runtime Error

Solution code:

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

def SelectionSort(N, C):
    C = [] + C
    for i in range(N):
        minj = i
        for j in range(i, N):
            if C[minj][1] > C[j][1]:
                minj = j
        C[i], C[minj] = C[minj], C[i]
    return C

def CompareSuit(L1, L2):
    suit1, suit2 = "", ""
    for i in range(len(L1)):
        suit1 = suit1 + L1[i]
        suit2 = suit2 + L2[i]
    if suit1 == suit2:
        return "Stable"
    else:
        return "Not stable"

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

C1 = BubbleSort(N, C)
C2 = SelectionSort(N, C)

print(" ".join(C1))
print(CompareSuit(C1, C1))
print(" ".join(C2))
print(CompareSuit(C1, C2))

Submission status: Runtime Error

Solution code:
import sys

class Card:
    def __init__(self, num, kind):
        self.kind = kind
        self.num = 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)
        
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] > array[j]:
                min_index = j
        swap(array, i, min_index)

def main():
    num = int(sys.stdin.readline().strip())
    array = map(lambda x: int(x), 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: Runtime Error

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

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: Runtime Error

Solution code:
# bable sort
def bubbleSort(o_nums, n):
    nums=o_nums[:]
    for i in range(len(nums)-1):
        for j in range(len(nums)-1,i,-1):
            if nums[j][1]<nums[j-1][1]:
                nums[j],nums[j-1]=nums[j-1],nums[j]  
    return nums

# bable sort
def bubbleSort(o_nums, n):
    nums=o_nums[:]
    for i in range(len(nums)-1):
        for j in range(len(nums)-1,i,-1):
            if nums[j][1]<nums[j-1][1]:
                nums[j],nums[j-1]=nums[j-1],nums[j]  
    return nums

n=int(input())
cards=list(input().split())

b=bubbleSort(cards, n)
s=selectionSort(cards,n)
print(*b)
print("Stable")
print(*s)
if bubbleSort(cards, n) == selectionSort(cards, n):
    print("Stable")
else:
    print("Not stable")

Submission status: Runtime Error

Solution code:
def bubbleSort(o_nums, n):
    nums=o_nums[:]
    for i in range(len(nums)-1):
        for j in range(len(nums)-1,i,-1):
            if nums[j][1]<nums[j-1][1]:
                nums[j],nums[j-1]=nums[j-1],nums[j]  
    return nums

def bubbleSort(o_nums, n):
    nums=o_nums[:]
    for i in range(len(nums)-1):
        for j in range(len(nums)-1,i,-1):
            if nums[j][1]<nums[j-1][1]:
                nums[j],nums[j-1]=nums[j-1],nums[j]  
    return nums

n=int(input())
cards=list(input().split())

b=bubbleSort(cards, n)
s=selectionSort(cards,n)
print(" ".join(b))
print("Stable")
print(" ".join(s))
if bubbleSort(cards, n) == selectionSort(cards, n):
    print("Stable")
else:
    print("Not stable")

Submission status: Runtime Error

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

A = []
A1 = []
A2 = []
for i in a:
    A.append(list(i))
    A1.append(list(i))
    A2.append(list(i))

# check stable or not
def is_stable(A3):
    for i in range( 1, n ):
        if int(A3[i-1][1]) == int(A3[i][1]):
            sm = i-1
            bg = i
            for j in range( n ):
                if ( A[j][0] == A3[sm][0]) & ( A[j][1] == A3[sm][1]):
                    t1 = j
                if ( A[j][0] == A3[bg][0]) & ( A[j][1] == A3[bg][1]):
                    t2 = j
            print(t1)
            print(t2)
            if t1 > t2:
                return "Not stable"
    return "Stable"

# bubble
for i in range( n ):
    for j in range( n-1, i, -1 ):
        if int(A1[j][1]) < int(A1[j-1][1]):
            t = A1[j-1]
            A1[j-1] = A1[j]
            A1[j] = t

s = is_stable(A1)
print(" ".join(A1))
print(s)


# selection
for i in range( n ):
    minj = i
    for j in range( i+1, n ):
        if int(A2[j][1]) < int(A2[minj][1]):
            minj = j
    if minj != i:
        t = A2[i]
        A2[i] = A2[minj]
        A2[minj] = t

s = is_stable(A2)
print(" ".join(A2))
print(s)

Submission status: Runtime Error

Solution code:
n = int(input())
a = input().split()
b = a[:]

while flag:
    flag = 0
    for i in range(n-1):
        if a[i][1] > a[i+1][1]:
            a[i][1], a[i+1][1] = a[i+1][1], a[i][1]
            flag = 1
print(*a)
print("Stable")

for i in range(n-1):
    min_j = i
    for j in range(i,n):
        if b[j][1] < b[min_j][1]:
            min_j = j
    b[i][1], b[min_j][1] = b[min_j][1], b[i][1]
print(*b)
if a == b:
    print("Stable")
else:
    print("Not stable")

Submission status: Runtime Error

Solution code:
N = int(input())
C = input().split()
_C = C.copy()

for i in range(N):
    for j in range(N-1, i, -1):
        if (C[j][1] < C[j-1][1]):
            C[j][1], C[j-1][1] = C[j-1][1], C[j][1]
print(*C)
print("Stable")

for j in range(N):
    m = i
    for j in range(i, N):
        if (_C[j][1] < _C[j-1][1]):
            m = j
    _C[i][1], _C[m][1] = _C[m][1], _C[i][1]
print(*_C)
if(C ==_C):
    print("Stable")
else:
    print("Not stable")

Submission status: Runtime Error

Solution code:
n=int(input())
c=input().split();d=c[:]
for i in range(n-1):
 for j in range(0,n-i-1):
  if c[j][1]>c[j+1][1]:c[j],c[j+1]=c[j+1],c[j]
 m=i
 for j in range(i,n):
  if b[m][1]>b[j][1]:m=j
 d[i],d[m]=d[m],d[i]
print(*c);print("Stable")
print(*d)
print(['Not s','S'][b==s]+'table')

Submission status: Runtime Error

Solution code:
n=int(input())
c=input().split();d=c[:]
for i in range(n-1):
 for j in range(0,n-i-1):
  if c[j][1]>c[j+1][1]:c[j],c[j+1]=c[j+1],c[j]
 m=i
 for j in range(i,n):
  if d[m][1]>d[j][1]:m=j
 d[i],d[m]=d[m],d[i]
print(*c);print("Stable")
print(*d)
print(['Not s','S'][b==s]+'table')

Submission status: Runtime Error

Solution code:
a = int(input())
B = input().split()

print(bubble(a,B))
print("Stable")
print(selection(a,B))
if bubble(a,B) == selection(a,B):
     print("Stable")
else:
    print("Not stable")

def selection(n,A):
    for i in range(n):
        minj = i
        for j in range(i,n):
            if int(A[j][1]) < int(A[minj][1]):
                minj = j
                
        A[i],A[minj] = A[minj],A[i]
    
    return " ".join(A)

def bubble(n,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]
    
    return " ".join(A)