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:
n=int(input())
l=input().split()

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

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

print(BubbleSort(l))
print(SelectionSort(l)

Submission status: Runtime Error

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

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

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

print(BubbleSort(l))
print(SelectionSort(l))

Submission status: Runtime Error

Solution code:
n=raw_int(input())
r=raw_input().split()

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

def SelectionSort(c, n):
    for i in range(n):
        m=i
        for j in range(i, n):
            if int(c[j][-1]) < int(c[m][-1]):
                m=j
        c[i],c[m]=c[m],c[i]
    return c

l=list(r)
a=BubbleSort(l, n)
print *a
print 'Stable'
l=list(r)
b=SelectionSort(l, n)
print *b
if a==b:
    print 'Stable'
else:
    print 'Not stable'

Submission status: Runtime Error

Solution code:
n=int(raw_input())
r=raw_input().split()

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

def SelectionSort(c, n):
    for i in range(n):
        m=i
        for j in range(i, n):
            if int(c[j][-1]) < int(c[m][-1]):
                m=j
        c[i],c[m]=c[m],c[i]
    return c

l=list(r)
a=BubbleSort(l, n)
print *a
print 'Stable'
l=list(r)
b=SelectionSort(l, n)
print *b
if a==b:
    print 'Stable'
else:
    print 'Not stable'

Submission status: Runtime Error

Solution code:
n = input()
nums = input()
nums = nums.split()
print(' '.join(map(str, nums)))

def bubble_sort(c, n):
    for i in range(n-1):
        for j in range(n-1):
            if c[j] > c[j+1]:
                c[j], c[j+1] = c[j+1], c[j]
    return c

def selection_sort(c, n):
    for i in range((n):
        mink = i
        for v in range(i, n):
            if c[v] <c[mink]:
                mink = v
        if mink != i:
            c[i], c[mink] = c[mink], c[i]
    return c

def is_stable(bs, ss):
    if bs == ss:
        return 'Stable'
    else:
        return 'Not stable'

bs = bubble_sort(nums, int(n))
print(' '.join(bs))
print('Stable')
ss = selection_sort(nums, int(n))
print(' '.join(ss))
print(is_stable(bs, ss))

Submission status: Runtime Error

Solution code:
# Quick Sort #
# 安定かどうかの判定の計算量を減らすことができなかったので，悪いことをした...
def partition(A, p, r):
    x = A[r][1]
    i = p-1
    for k in range(p, r):
        if A[k][1] <= x:
            i += 1
            A[i], A[k] = A[k], A[i]
    A[i+1], A[r] = A[r], A[i+1]
    return i+1

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

def bubble_sort(c, n):
    x = c[:]
    for i in range(n):
        for k in range(n-1, i, -1):
            if x[k][1] < x[k-1][1]:
                x[k], x[k-1] = x[k-1], x[k]
    return x

def is_stable(_in, out):
    n = len(_in)
    for i in range(n):
        for k in range(i+1, n):
            for a in range(n):
                for b in range(a+1, n):
                    if _in[i][1] == _in[k][1] and _in[i] == out[b] and _in[k] == out[a]:
                        return False
    return True

n = int(input())
a = []
for i in range(n):
    mrk, num = input().split()
    num = int(num)
    a.append((mrk, num))
b = a[:]
quick_sort(b, 0, n-1)
if n > 10000:
    print("Not stable")
else:
# BubbleSortは常に安定
    c = bubble_sort(a[:], n)
    if b == c:
        print("Stable")
    else:
        print("Not stable")
for i in b:
    print(*i)

Submission status: Runtime Error

Solution code:
import re
import copy
pattern = r'([0-9])'
repatter = re.compile(pattern)

n = str(input())
text = input()

*lists, = text.split(" ")
lists2 = copy.copy(lists)
lists3 = copy.copy(lists)


# バブルソート
def bubble_sort(a):
    flag = 1
    i = 0
    while flag:
        flag = 0
        for j in range(n-1, i, -1):
            x = repatter.findall(a[j])
            y = repatter.findall(a[j-1])
            if x < y:
                a[j], a[j-1] = a[j-1], a[j]
                flag = 1
        i += 1
    print(*a)
    print("Stable")


# 選択ソート
def select_sort(a):
    for i in range(n):
        min_j = i
        for j in range(i+1, n):
            x = repatter.findall(a[j])
            y = repatter.findall(a[min_j])
            if x < y:
                min_j = j
        a[i], a[min_j] = a[min_j], a[i]
    print(*a)


def isStable(before, after):
    for i in range(n):
        for j in range(i+1, n):
            for a in range(n):
                for b in range(a+1, n):
                    x = repatter.findall(before[i])
                    y = repatter.findall(before[j])
                    if x == y and before[i] == after[b] and before[j] == after[a]:
                        print("Not stable")


bubble_sort(lists)
select_sort(lists2)
isStable(lists3, lists2)

Submission status: Runtime Error

Solution code:
# #バブルソート
# def BubbleSort(C,N):
#     for i in range (0,N,1):
#         for j in range(N-1,i-1,-1):
#             if int(C[j][1:2]) < int(C[j-1][1:2]):
#                 tmp = C[j]
#                 C[j] = C[j-1]
#                 C[j-1] = tmp
#     print(' '.join(C))

#バブルソート
def BubbleSort(C,N):
    flag = 1
    while flag:
        flag = 0
        for j in range(N-1,0,-1):
            if int(C[j][1:2]) < int(C[j-1][1:2]):
                tmp = C[j]
                C[j] = C[j-1]
                C[j-1] = tmp
                flag = 1
    print(' '.join(C))
    
    
                
                
#選択ソート
def SelectionSort(C,N):
    for i in range(0,N,1):
        minj = i
        for j in range(i,N,1):
            if int(C[j][1:2]) < int(C[minj][1:2]):
                minj = j
        tmp = C[i]
        C[i] = C[minj]
        C[minj] = tmp
    print(' '.join(C))
        
#安定かどうかの判定
def isStable(inp, out,N):
    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):
#                     print("inp(i,j):",inp[i],inp[j],"out(b,a):",out[b],out[a]+"\n")
                    if int(inp[i][1]) == int(inp[j][1]) and inp[i] == out[b] and inp[j] == out[a]:
                        print('Not stable')
                        return
    print('Stable')



愚直に調べるパターン
N = int(input())
A = list(input().split())
Bubble_A = A.copy()
Selection_A = A.copy()

BubbleSort(Bubble_A,N)
isStable(A,Bubble_A,N)
SelectionSort(Selection_A,N)
isStable(A,Selection_A,N)

Submission status: Runtime Error

Solution code:
#バブルソート
def BubbleSort(C,N):
    flag = 1
    while flag:
        flag = 0
        for j in range(N-1,0,-1):
            if int(C[j][1:2]) < int(C[j-1][1:2]):
                tmp = C[j]
                C[j] = C[j-1]
                C[j-1] = tmp
                flag = 1
    print(' '.join(C))
    
    
                
                
#選択ソート
def SelectionSort(C,N):
    for i in range(0,N,1):
        minj = i
        for j in range(i,N,1):
            if int(C[j][1:2]) < int(C[minj][1:2]):
                minj = j
        tmp = C[i]
        C[i] = C[minj]
        C[minj] = tmp
    print(' '.join(C))
        
#安定かどうかの判定
def isStable(inp, out,N):
    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):
#                     print("inp(i,j):",inp[i],inp[j],"out(b,a):",out[b],out[a]+"\n")
                    if int(inp[i][1]) == int(inp[j][1]) and inp[i] == out[b] and inp[j] == out[a]:
                        print('Not stable')
                        return
    print('Stable')



愚直に調べるパターン
N = int(input())
A = list(input().split())
Bubble_A = A.copy()
Selection_A = A.copy()

BubbleSort(Bubble_A,N)
isStable(A,Bubble_A,N)
SelectionSort(Selection_A,N)
isStable(A,Selection_A,N)

Submission status: Runtime Error

Solution code:
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"reflect"
)

var sc = bufio.NewScanner(os.Stdin)
var wtr = bufio.NewWriter(os.Stdout)

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

type Card struct {
	mark   string
	number int
}

func nextCard() Card {
	sc.Scan()
	text := sc.Text()
	mark := text[0:1]
	number, e := strconv.Atoi(text[1:])
	if e != nil {
		panic(e)
	}
	return Card{mark, number}
}

func main() {
	sc.Split(bufio.ScanWords)
	var n int = nextInt()
	var A []Card

	for i := 0; i < n; i++ {
		A = append(A, nextCard())
	}



	bubble_A := make([]Card, n)

	copy(bubble_A, A)

	var flag = true
	for flag {
		flag = false
		for i := n - 1; i >= 1; i-- {

			if bubble_A[i].number < bubble_A[i-1].number {
				bubble_A[i], bubble_A[i-1] = bubble_A[i-1], bubble_A[i]
				flag = true
			}
		}
	}
	
	//fprintCardArray(A)
	for i := 0; i < n; i++ {
		var minj = i
		for j := i; j < n; j++ {
			if A[j].number < A[minj].number {
				minj = j
			}
		}
		if A[i].number != A[minj].number {
			A[i], A[minj] = A[minj], A[i]
		}

	}

	fprintCardArray(A)
	fmt.Fprintln(wtr,"Stable")
	fprintCardArray(bubble_A)
	if reflect.DeepEqual(A,bubble_A){
		fmt.Fprintln(wtr,"Stable")
	}else {
		fmt.Fprintln(wtr,"Not stable")
	}
	_ = wtr.Flush()

}

func fprintIntArray(arr []int) {
	for i := 0; i < len(arr); i++ {
		if i == 0 {
			fmt.Fprintf(wtr, "%d", arr[i])
		} else {
			fmt.Fprintf(wtr, " %d", arr[i])
		}
	}
	fmt.Fprintln(wtr)
}

func fprintCardArray(arr []Card) {
	for i := 0; i < len(arr); i++ {
		if i == 0 {
			fmt.Fprintf(wtr, "%s%d", arr[i].mark, arr[i].number)
		} else {
			fmt.Fprintf(wtr, " %s%d", arr[i].mark, arr[i].number)
		}
	}
	fmt.Fprintln(wtr)
}

Submission status: Runtime Error

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

    def __eq__(self, other):
        if not isinstance(other, Card):
            return NotImplemented
        return self.value == other.value and self.mark == other.mark

    def __lt__(self, other):
        return self.value < other.value

    def __str__(self):
        return self.mark + str(self.value)


def BubbleSort(arr):
    res = [Card(c.mark, c.value) for c in arr]
    swap_flg = True
    i = 0
    while swap_flg:
        swap_flg = False
        for j in range(len(res) - 1, i, -1):
            if (res[j] < res[j - 1]):
                res[j], res[j - 1] = res[j - 1], res[j]
                swap_flg = True
        i += 1
    return res


def SelectionSort(arr):
    res = [Card(c.mark, c.value) for c in arr]
    for i in range(0, len(res)):
        minj = i
        for j in range(i, N):
            if res[j] < res[minj]:
                minj = j
        if (i != minj):
            res[i], res[minj] = res[minj], res[i]
    return res


def isStable(in_arr, out_arr):
    stable_arr = BubbleSort(in_arr)
    return all(out_arr[i] == stable_arr[i] for i in range(0, len(out_arr)))


def printJudgeStable(in_arr, out_arr):
    print('Stable' if isStable(in_arr, out_arr) else 'Not stable')


N = int(input())
arr = [Card(s[0], int(s[1])) for s in input().split()]
b_arr = BubbleSort(arr)
print(' '.join(map(str, b_arr)))
printJudgeStable(arr, b_arr):
s_arr = SelectionSort(arr)
print(' '.join(map(str, s_arr)))
printJudgeStable(arr, s_arr):

Submission status: Runtime Error

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

    def __eq__(self, other):
        if not isinstance(other, Card):
            return NotImplemented
        return self.value == other.value and self.mark == other.mark

    def __lt__(self, other):
        return self.value < other.value

    def __str__(self):
        return self.mark + str(self.value)


def BubbleSort(arr):
    res = [Card(c.mark, c.value) for c in arr]
    swap_flg = True
    i = 0
    while swap_flg:
        swap_flg = False
        for j in range(len(res) - 1, i, -1):
            if (res[j] < res[j - 1]):
                res[j], res[j - 1] = res[j - 1], res[j]
                swap_flg = True
        i += 1
    return res


def SelectionSort(arr):
    res = [Card(c.mark, c.value) for c in arr]
    for i in range(0, len(res)):
        minj = i
        for j in range(i, N):
            if res[j] < res[minj]:
                minj = j
        if (i != minj):
            res[i], res[minj] = res[minj], res[i]
    return res


def isStable(in_arr, out_arr):
    stable_arr = BubbleSort(in_arr)
    return all(out_arr[i] == stable_arr[i] for i in range(0, len(out_arr)))


def printJudgeStable(in_arr, out_arr):
    print('Stable' if isStable(in_arr, out_arr) else 'Not stable')
    return


N = int(input())
arr = [Card(s[0], int(s[1])) for s in input().split()]
b_arr = BubbleSort(arr)
print(' '.join(map(str, b_arr)))
printJudgeStable(arr, b_arr):
s_arr = SelectionSort(arr)
print(' '.join(map(str, s_arr)))
printJudgeStable(arr, s_arr):

Submission status: Runtime Error

Solution code:
def bubble(A):
    flag = True
    n = len(A)
    i = 0
    while flag:
        flag = False
        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 = True
        i += 1
 
 
def selection(A):
    n = len(A)
    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:
            A[i],A[minj] = A[minj],A[i]
             
   
           
n = int(input())
ss = [x for x in input().split()]     
ss2 = ss.copy()
 
 
bubble(ss)
for i in range(len(ss)-1):
    print(ss[i], end=' ')
print(ss[-1])
print('Stable')
    
 
selection(ss2)
for i in range(len(ss2)-1):
    print(ss2[i], end=' ')
print(ss2[-1])
if ss == ss2:
    print('Stable')
else:
    print('Not stable')

Submission status: Runtime Error

Solution code:
def bubble(A):
    flag = True
    n = len(A)
    i = 0
    while flag:
        flag = False
        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 = True
        i += 1
 
 
def selection(A):
    n = len(A)
    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:
            A[i],A[minj] = A[minj],A[i]
             
   
           
n = int(input())
ss = list(map(int,input().split()))
ss2 = ss.copy()
 
 
bubble(ss)
for i in range(len(ss)-1):
    print(ss[i], end=' ')
print(ss[-1])
print('Stable')
    
 
selection(ss2)
for i in range(len(ss2)-1):
    print(ss2[i], end=' ')
print(ss2[-1])
if ss == ss2:
    print('Stable')
else:
    print('Not stable')

Submission status: Runtime Error

Solution code:
Last login: Thu Jun 28 18:57:04 on ttys001
fujitaryuuki-no-MacBook-Pro:~ fujitaryuki$ ls
Applications
Applications (Parallels)
Desktop
Documents
Downloads
KUT Master
Library
Logicool ゲームソフトウェア 8.82.lnk
Movies
Music
Pictures
Public
PythonStudy
Windows 10
eclipse
matsuzaki-lab
tenhou
wget
就活
fujitaryuuki-no-MacBook-Pro:~ fujitaryuki$ cd matsuzaki-lab/
fujitaryuuki-no-MacBook-Pro:matsuzaki-lab fujitaryuki$ ls
2048player
ACG2017へ投稿した論文，プログラム
AOJ
IFST
IFST.pptx
ISFT 原稿.docx
ISFT2017_fujita.doc
ISFTslides.pptx
ISFT論文 最終版 (Was_ Re_ データ再送)
ISFT論文 最終版 (Was_ Re_ データ再送).zip
Kiyou
Mahjong
N-Bakuuchi.pptx
README.md
Report
Study
TA.pdf
TCB.png
TCB.xlsx
TCB8staging.xlsx
TDFver2
cppStudy
figure1.pptx
seminar
special-1rep.docx
src
supervised_2048
vdw.pdf
写真（2015-09-17 19.19） 12-48-45-461.jpg
スクリーンショット 2017-10-20 12.13.11.png
スクリーンショット 2017-10-20 12.14.36.png
スクリーンショット 2017-10-20 4.11.06.png
スクリーンショット 2017-10-20 4.11.25.png
スクリーンショット 2017-10-24 18.22.45.png
スクリーンショット 2017-10-24 18.32.46.png
スクリーンショット 2018-01-30 0.52.09.png
スクリーンショット 2018-04-21 17.10.48.png
スクリーンショット 2018-04-22 21.45.32.png
スクリーンショット 2018-04-23 10.03.54.png
スクリーンショット 2018-04-23 10.12.21.png
スクリーンショット 2018-04-23 10.16.25.png
スクリーンショット 2018-04-23 18.07.52.png
スクリーンショット 2018-04-23 8.33.58.png
スクリーンショット 2018-04-23 9.02.26.png
スクリーンショット 2018-04-23 9.12.48.png
スクリーンショット 2018-04-23 9.15.41.png
スクリーンショット 2018-04-23 9.16.00.png
スクリーンショット 2018-04-23 9.26.32.png
スクリーンショット 2018-04-23 9.43.01.png
スクリーンショット 2018-04-23 9.43.30.png
スクリーンショット 2018-04-23 9.43.40.png
スクリーンショット 2018-04-23 9.52.31.png
スクリーンショット 2018-04-25 11.30.40.png
fujitaryuuki-no-MacBook-Pro:matsuzaki-lab fujitaryuki$ cd A
ACG2017へ投稿した論文，プログラム/ AOJ/
fujitaryuuki-no-MacBook-Pro:matsuzaki-lab fujitaryuki$ cd A
ACG2017へ投稿した論文，プログラム/ AOJ/
fujitaryuuki-no-MacBook-Pro:matsuzaki-lab fujitaryuki$ cd AOJ/
fujitaryuuki-no-MacBook-Pro:AOJ fujitaryuki$ ls
#copy.java#			copy.java
DPfibonacci.class		copy.java~
DPfibonacci.java		copylcs.class
DPfibonacci.java~		copylcs.java
ExhaustiveSearch.class		copylcs.java~
ExhaustiveSearch.java		dpLcs.class
ExhaustiveSearch.java~		dpLcs.java
LCS.class			dpLcs.java~
LCS.java			dpLcsv2.java
LCS.java~			dpLcsv2.java~
Main.class			fibonacci.class
RepeatedSubsequences.class	fibonacci.java
RepeatedSubsequences.java	fibonacci.java~
RepeatedSubsequences.java~	recursiveLcs.class
Xcubic.java			recursiveLcs.java
Xcubic.java~			recursiveLcs.java~
copy.class			recursivelca.java
fujitaryuuki-no-MacBook-Pro:AOJ fujitaryuki$ touch ALDS1_1_D.py
fujitaryuuki-no-MacBook-Pro:AOJ fujitaryuki$ emacs ALDS1_1_D.py 

[1]+  Stopped                 emacs ALDS1_1_D.py
fujitaryuuki-no-MacBook-Pro:AOJ fujitaryuki$ touch ALDS1_2_C.py
fujitaryuuki-no-MacBook-Pro:AOJ fujitaryuki$ emacs ALDS1_
fujitaryuuki-no-MacBook-Pro:AOJ fujitaryuki$ emacs ALDS1_2_C.py 
fujitaryuuki-no-MacBook-Pro:AOJ fujitaryuki$ python3 ALDS1_2_C.py
5
H4 C9 S4 D2 C3
D2 C3 H4 S4 C9
Stable
D2 C3 S4 H4 C9
Not stable
fujitaryuuki-no-MacBook-Pro:AOJ fujitaryuki$ emacs ALDS1_2_C.py

[2]+  Stopped                 emacs ALDS1_2_C.py
fujitaryuuki-no-MacBook-Pro:AOJ fujitaryuki$ python3 ALDS1_2_C.py
5
H4 C9 S4 D2 C35

['H4', 'C9', 'S4', 'D2', 'C3']
['H4', 'C9', 'S4', 'D2', 'C3']
D2 C3 H4 S4 C9
Stable
D2 C3 S4 H4 C9
Not stable
fujitaryuuki-no-MacBook-Pro:AOJ fujitaryuki$ fg
emacs ALDS1_2_C.py

[2]+  Stopped                 emacs ALDS1_2_C.py
fujitaryuuki-no-MacBook-Pro:AOJ fujitaryuki$ python3 ALDS1_2_C.py
5
H4 C9 S4 D2 C35

['H4', 'C9', 'S4', 'D2', 'C3']
['H4', 'C9', 'S4', 'D2', 'C3']
D2 C3 H4 S4 C9
Stable
D2 C3 S4 H4 C9
Not stable
fujitaryuuki-no-MacBook-Pro:AOJ fujitaryuki$ fg
emacs ALDS1_2_C.py

[2]+  Stopped                 emacs ALDS1_2_C.py
fujitaryuuki-no-MacBook-Pro:AOJ fujitaryuki$ fg
emacs ALDS1_2_C.py

input_line = int(input())
out_bubble = input().split()
out_selection = out_bubble[:]

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

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

print(*out_selection)
print("Stable" if out_b == out_selection else "Not stable")

Submission status: Runtime Error

Solution code:
input_line = int(input())
out_selection = input().split()
out_bubble = out_bubble[:]

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

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

print(*out_selection)
print("Stable" if out_bubble == out_selection else "Not stable")

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin


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


def selection(cards):
    for i in range(len(cards)):
        mini = i
        for j in range(i, len(cards)):
            if cards[j][1] < cards[mini][1]:
                mini = j
        cards[i], cards[mini] = cards[mini], cards[i]


def stable_test(orders, crads):
    for order in orders:
        n = -1
        for c in order:
            p = n
            n = cards.index(c)
            if p > n:
                return False
    return True


stdin.readline()
data1 = stdin.readline().split()
data2 = data1[:]
orders = [[c for c in data1 if s.endswith(str(i))] for i in range(1, 10)]
orders = [o for o in orders if len(s) > 1]

bubble(data1)
print(*data1)
print('Stable' if stable_test(orders, data1) else 'Not stable')

selection(data2)
print(*data2)
print('Stable' if stable_test(orders, data2) else 'Not stable')

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from __future__ import print_function
import time
import sys
import io
import re
import math
start = time.clock()

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

def selection(cards):
    for i in range(len(cards)):
        mini=i
        for j in range(i,len(cards)):
            if cards[j][1]<cards[mini][1]:
                mini=j
        cards[i],cards[mini]=cards[mini],cards[i]

def stabel_test(orders,cards):
    for order in orders:
        n=-1
        for c in order:
            p=n
            n=cards.index(c)
            if p>n:
                return False
    return True

sys.stdin.readline()
data1=sys.stdin.readline().split()
data2=data1[:]
orders=[[c for c in data1 if c.endswitch(str(i))] for i in range(1,10)]
orders=[o for o in orders if len(c)>1]

bubble(data1)
print(*data1)
print('Stable' if stabel_test(orders, data1) else 'Not stable')

selection(data2)
print(*data2)
print('Stable' if stable_test(orders, data2) else 'Not stable')

Submission status: WA: Presentation Error

Solution code:
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_2_C&lang=jp
sample_input = list(range(3))
sample_input[0] = '''5
H4 C9 S4 D2 C3'''
sample_input[1] = '''2
S1 H1'''
sample_input[2] = '''5
H4 C9 S4 D2 C3'''
give_sample_input = 2
if give_sample_input is not None:
    sample_input_list = sample_input[give_sample_input].split('\n')
    def input():
        return sample_input_list.pop(0)
        
# main
import copy

def swap_list_item(lst, i, j):
    tmp = lst[i]
    lst[i] = lst[j]
    lst[j] = tmp
    
class Card:
    number = None
    mark = None
    def __init__(self, str_card):
        self.number = int(str_card[1])
        self.mark = str_card[0]
        pass
    def __eq__(self, other):
        return self.number == other.number
    def __lt__(self, other):
        return self.number < other.number
    def to_str(self):
        return self.mark + str(self.number)

def selection_sort(list_of_data):
    num_of_data = len(list_of_data)
    for i in range(num_of_data):
        minj = i
        for j in range(i, num_of_data):
            if list_of_data[minj] > list_of_data[j]:
                minj = j
        if not minj == i:
            swap_list_item(list_of_data, i, minj)
            
def bubble_sort(list_of_data):
    flag = True
    while flag:
        flag = False
        n = num_of_data - 1
        while n >= 1:
            if list_of_data[n] < list_of_data[n-1]:
                swap_list_item(list_of_data, n, n-1)
                flag = True
            n -= 1
        
def get_mark_order_info(card_list, number):
    result = ''
    for card in card_list:
        if card.number == number:
            result += card.mark
    return result

def print_card_list(card_list):
    output = ''
    for card in card_list:
        output += card.to_str() + ' '
    output.rstrip()
    print(output)

num_of_data = int(input())
data_list_str = input().split()
data_cards = [Card(str) for str in data_list_str]
data_cards_bubble = copy.copy(data_cards)
data_cards_selection = copy.copy(data_cards)

mark_order_info = []
for number in [n+1 for n in range(13)]:
    mark_order_info.append(get_mark_order_info(data_cards, number))
    
bubble_sort(data_cards_bubble)
selection_sort(data_cards_selection)

mark_order_info_bubble = []
for number in [n+1 for n in range(13)]:
    mark_order_info_bubble.append(get_mark_order_info(data_cards_bubble, number))

mark_order_info_selection = []
for number in [n+1 for n in range(13)]:
    mark_order_info_selection.append(get_mark_order_info(data_cards_selection, number))
    
print_card_list(data_cards_bubble)
if (mark_order_info == mark_order_info_bubble):
    print('Stable')
else:
    print('Not stable')
    
print_card_list(data_cards_selection)
if (mark_order_info == mark_order_info_selection):
    print('Stable')
else:
    print('Not stable')

Submission status: WA: Presentation Error

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


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

def selection_sort(c):
    c = list(c)
    for i in range(len(c)):
        minj = i
        for j in range(i+1, len(c)):
            if int(c[j][1]) < int(c[minj][1]):
                minj = j
        c[i], c[minj] = c[minj], c[i]
    return c

input_num = int(input())
input_list = input().split()
bubble_list = bubble_sort(input_list)
selection_list = selection_sort(input_list)
for c in bubble_list:
    print(c, end=' ')
print("\nStable")
stable_flg = True
for i in range(len(input_list)):
    if bubble_list[i] != selection_list[i]:
        stable_flg = False
    print(selection_list[i], end=' ')
if stable_flg:
    print("\nStable")
else:
    print("\nNot stable")

Submission status: WA: Presentation Error

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]
    

    bubblesort(a)
    selection(b)
    
    printcards(a)
    print("Stable")

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

Submission status: WA: Presentation Error

Solution code:
import copy


def SelectionSort(arr, n):
    for i in range(n):
        mini = i
        for j in range(i, n):
            if arr[j]['val'] < arr[mini]['val']:
                mini = j
        if mini != i:
            arr[i], arr[mini] = arr[mini], arr[i]
    return


def BubleSort(arr, n):
    for i in range(n):
        for j in range(n - 1, i, -1):
            if arr[j]['val'] < arr[j-1]['val']:
                arr[j], arr[j-1] = arr[j-1], arr[j]
    return


def isStable(inarr, comparr, n):
    i = 0
    while i < n:
        tmpVal = comparr[i]['val']
        for j in range(n):
            if inarr[j]['val'] == tmpVal:
                if inarr[j]['crd'] == comparr[i]['crd']:
                    if i != n - 1 and comparr[i+1]['val'] == tmpVal:
                        i += 1
                    else:
                        break
                else:
                    return 'Not stable'
        i += 1
    return 'Stable'



n = int(input())
arr = input().split()
tar = []

for s in arr:
    tar.append({'crd': s[0], 'val': s[1], 'str': s})

bub = copy.copy(tar)
BubleSort(bub, n)

w = ""
for s in bub:
    w += s['str'] + ' '
print(w)
print(isStable(tar, bub, n))

bub = copy.copy(tar)
SelectionSort(bub, n)

w = ""
for s in bub:
    w += s['str'] + ' '
print(w)
print(isStable(tar, bub, n))