def bubble_sort(A, N):
    cnt = 0
    flag = True
    while flag:
        flag = False
        for j in range(N - 1, 0, -1):
            if int(A[j - 1][1]) > int(A[j][1]):
                A[j - 1], A[j] = A[j], A[j - 1]
                flag = True
                cnt += 1
    return A

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

N = int(input())
A = input().split()
order_A = [[] for _ in range(10)]
for s in A:
    order_A[int(s[1])].append(s)

order_bubble = [[] for _ in range(10)]
b_sorted = bubble_sort(A[:], N)
for s in b_sorted:
    order_bubble[int(s[1])].append(s)
print(*b_sorted)
print('Stable' if order_A == order_bubble else 'Not stable')

s_stability = [[] for _ in range(10)]
s_sorted = selection_sort(A[:], N)
for s in s_sorted:
    s_stability[int(s[1])].append(s)
print(*s_sorted)
print('Stable' if order_A == s_stability else 'Not stable') def bubble_sort(A, N):
    cnt = 0
    flag = True
    while flag:
        flag = False
        for j in range(N - 1, 0, -1):
            if int(A[j - 1][1]) > int(A[j][1]):
                A[j - 1], A[j] = A[j], A[j - 1]
                flag = True
                cnt += 1
    return A bubble_sort cnt = 0 cnt 0 flag = True flag True while flag:
        flag = False
        for j in range(N - 1, 0, -1):
            if int(A[j - 1][1]) > int(A[j][1]):
                A[j - 1], A[j] = A[j], A[j - 1]
                flag = True
                cnt += 1
     flag flag = False flag False for j in range(N - 1, 0, -1):
            if int(A[j - 1][1]) > int(A[j][1]):
                A[j - 1], A[j] = A[j], A[j - 1]
                flag = True
                cnt += 1
     j range(N - 1, 0, -1) range N - 1 N 1 0 -1 if int(A[j - 1][1]) > int(A[j][1]):
                A[j - 1], A[j] = A[j], A[j - 1]
                flag = True
                cnt += 1
     int(A[j - 1][1]) > int(A[j][1]) int(A[j - 1][1]) int A[j - 1][1] [j - 1] A j - 1 j 1 1 int(A[j][1]) int A[j][1] [j] A j 1 A[j - 1], A[j] = A[j], A[j - 1] A[j - 1] A j - 1 j 1 A[j] A j A[j] A j A[j - 1] A j - 1 j 1 flag = True flag True cnt += 1 cnt 1 return A A A A N N def selection_sort(A, N):
    cnt = 0
    for i in range(N):
        minj = i
        for j in range(i, N):
            if int(A[j][1]) < int(A[minj][1]):
                minj = j
        A[i], A[minj] = A[minj], A[i]
        if i != minj:
            cnt += 1
    return A selection_sort cnt = 0 cnt 0 for i in range(N):
        minj = i
        for j in range(i, N):
            if int(A[j][1]) < int(A[minj][1]):
                minj = j
        A[i], A[minj] = A[minj], A[i]
        if i != minj:
            cnt += 1
     i range(N) range N minj = i minj i for j in range(i, N):
            if int(A[j][1]) < int(A[minj][1]):
                minj = j
         j range(i, N) range i N if int(A[j][1]) < int(A[minj][1]):
                minj = j
         int(A[j][1]) < int(A[minj][1]) int(A[j][1]) int A[j][1] [j] A j 1 int(A[minj][1]) int A[minj][1] [minj] A minj 1 minj = j minj j A[i], A[minj] = A[minj], A[i] A[i] A i A[minj] A minj A[minj] A minj A[i] A i if i != minj:
            cnt += 1
     i != minj i minj cnt += 1 cnt 1 return A A A A N N N = int(input()) N int(input()) int input() input A = input().split() A input().split() ().split () input split [] for _ in range(10) for _ in range(10) _ range(10) range 10 for _ in range(10) [] order_A = [[] for _ in range(10)] order_A [[] for _ in range(10)] for s in A:
    order_A[int(s[1])].append(s) s A order_A[int(s[1])].append(s) [int(s[1])].append [int(s[1])] order_A int(s[1]) int s[1] s 1 append s [] for _ in range(10) for _ in range(10) _ range(10) range 10 for _ in range(10) [] order_bubble = [[] for _ in range(10)] order_bubble [[] for _ in range(10)] b_sorted = bubble_sort(A[:], N) b_sorted bubble_sort(A[:], N) bubble_sort A[:] A : N for s in b_sorted:
    order_bubble[int(s[1])].append(s) s b_sorted order_bubble[int(s[1])].append(s) [int(s[1])].append [int(s[1])] order_bubble int(s[1]) int s[1] s 1 append s print(*b_sorted) print *b_sorted b_sorted print('Stable' if order_A == order_bubble else 'Not stable') print 'Stable' if order_A == order_bubble else 'Not stable' order_A == order_bubble order_A order_bubble 'Stable' 'Not stable' [] for _ in range(10) for _ in range(10) _ range(10) range 10 for _ in range(10) [] s_stability = [[] for _ in range(10)] s_stability [[] for _ in range(10)] s_sorted = selection_sort(A[:], N) s_sorted selection_sort(A[:], N) selection_sort A[:] A : N for s in s_sorted:
    s_stability[int(s[1])].append(s) s s_sorted s_stability[int(s[1])].append(s) [int(s[1])].append [int(s[1])] s_stability int(s[1]) int s[1] s 1 append s print(*s_sorted) print *s_sorted s_sorted print('Stable' if order_A == s_stability else 'Not stable') print 'Stable' if order_A == s_stability else 'Not stable' order_A == s_stability order_A s_stability 'Stable' 'Not stable' N = int(input()) int(input()) N order_A = [[] for _ in range(10)] [[] for _ in range(10)] order_A b_sorted = bubble_sort(A[:], N) bubble_sort(A[:], N) b_sorted def selection_sort(A, N):
    cnt = 0
    for i in range(N):
        minj = i
        for j in range(i, N):
            if int(A[j][1]) < int(A[minj][1]):
                minj = j
        A[i], A[minj] = A[minj], A[i]
        if i != minj:
            cnt += 1
    return A def selection_sort(A, N):
    cnt = 0
    for i in range(N):
        minj = i
        for j in range(i, N):
            if int(A[j][1]) < int(A[minj][1]):
                minj = j
        A[i], A[minj] = A[minj], A[i]
        if i != minj:
            cnt += 1
    return A selection_sort s_sorted = selection_sort(A[:], N) selection_sort(A[:], N) s_sorted A = input().split() input().split() A order_bubble = [[] for _ in range(10)] [[] for _ in range(10)] order_bubble def bubble_sort(A, N):
    cnt = 0
    flag = True
    while flag:
        flag = False
        for j in range(N - 1, 0, -1):
            if int(A[j - 1][1]) > int(A[j][1]):
                A[j - 1], A[j] = A[j], A[j - 1]
                flag = True
                cnt += 1
    return A def bubble_sort(A, N):
    cnt = 0
    flag = True
    while flag:
        flag = False
        for j in range(N - 1, 0, -1):
            if int(A[j - 1][1]) > int(A[j][1]):
                A[j - 1], A[j] = A[j], A[j - 1]
                flag = True
                cnt += 1
    return A bubble_sort s_stability = [[] for _ in range(10)] [[] for _ in range(10)] s_stability
def bubble_sort(A, N):
    cnt = 0
    flag = True
    while flag:
        flag = False
        for j in range(N - 1, 0, -1):
            if int(A[j - 1][1]) > int(A[j][1]):
                A[j - 1], A[j] = A[j], A[j - 1]
                flag = True
                cnt += 1
    return A

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

N = int(input())
A = input().split()
order_A = [[] for _ in range(10)]
for s in A:
    order_A[int(s[1])].append(s)

order_bubble = [[] for _ in range(10)]
b_sorted = bubble_sort(A[:], N)
for s in b_sorted:
    order_bubble[int(s[1])].append(s)
print(*b_sorted)
print('Stable' if order_A == order_bubble else 'Not stable')

s_stability = [[] for _ in range(10)]
s_sorted = selection_sort(A[:], N)
for s in s_sorted:
    s_stability[int(s[1])].append(s)
print(*s_sorted)
print('Stable' if order_A == s_stability else 'Not stable')
