#! /usr/local/bin/python3
# coding: utf-8

def swap(c, i, j):
    t = c[i]
    c[i] = c[j]
    c[j] = t

def bubble_sort(cs, f):
    c = cs[:]
    for i in range(len(c) - 1):
        for j in range(len(c) - 1, i, -1):
            if f(c[j - 1]) > f(c[j]):
                swap(c, j - 1, j)
            # print(i, j, c)
    return c

def selection_sort(cs, f):
    c = cs[:]
    for i in range(len(c)):
        minj = i
        for j in range(i, len(c)):
            if f(c[j]) < f(c[minj]):
                minj = j
        swap(c, i, minj)
        # print(c)
    return c

def to_group(c):
    b = {}
    for i in c:
        if not(i[1] in b):
            b[i[1]] = [i]
        else:
            b[i[1]] = b[i[1]] + [i]
    return b

def is_stable(c, s): 
    bc = to_group(c)
    bs = to_group(s(c, lambda x: x[1]))
    # print(bc)
    # print(bs)
    for k in bc:
        if bc[k] != bs[k]:
            return "Not stable"
    return "Stable"

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

print(" ".join(bubble_sort(c, lambda x: x[1])))
print(is_stable(c, bubble_sort))

print(" ".join(selection_sort(c, lambda x: x[1])))
print(is_stable(c, selection_sort))

 def swap(c, i, j):
    t = c[i]
    c[i] = c[j]
    c[j] = t swap t = c[i] t c[i] c i c[i] = c[j] c[i] c i c[j] c j c[j] = t c[j] c j t c c i i j j def bubble_sort(cs, f):
    c = cs[:]
    for i in range(len(c) - 1):
        for j in range(len(c) - 1, i, -1):
            if f(c[j - 1]) > f(c[j]):
                swap(c, j - 1, j)
            # print(i, j, c)
    return c bubble_sort c = cs[:] c cs[:] cs : for i in range(len(c) - 1):
        for j in range(len(c) - 1, i, -1):
            if f(c[j - 1]) > f(c[j]):
                swap(c, j - 1, j)
            # print(i, j, c)
     i range(len(c) - 1) range len(c) - 1 len(c) len c 1 for j in range(len(c) - 1, i, -1):
            if f(c[j - 1]) > f(c[j]):
                swap(c, j - 1, j)
            # print(i, j, c)
     j range(len(c) - 1, i, -1) range len(c) - 1 len(c) len c 1 i -1 if f(c[j - 1]) > f(c[j]):
                swap(c, j - 1, j)
            # print(i, j, c)
     f(c[j - 1]) > f(c[j]) f(c[j - 1]) f c[j - 1] c j - 1 j 1 f(c[j]) f c[j] c j swap(c, j - 1, j) swap c j - 1 j 1 j return c c cs cs f f def selection_sort(cs, f):
    c = cs[:]
    for i in range(len(c)):
        minj = i
        for j in range(i, len(c)):
            if f(c[j]) < f(c[minj]):
                minj = j
        swap(c, i, minj)
        # print(c)
    return c selection_sort c = cs[:] c cs[:] cs : for i in range(len(c)):
        minj = i
        for j in range(i, len(c)):
            if f(c[j]) < f(c[minj]):
                minj = j
        swap(c, i, minj)
        # print(c)
     i range(len(c)) range len(c) len c minj = i minj i for j in range(i, len(c)):
            if f(c[j]) < f(c[minj]):
                minj = j
         j range(i, len(c)) range i len(c) len c if f(c[j]) < f(c[minj]):
                minj = j
         f(c[j]) < f(c[minj]) f(c[j]) f c[j] c j f(c[minj]) f c[minj] c minj minj = j minj j swap(c, i, minj) swap c i minj return c c cs cs f f def to_group(c):
    b = {}
    for i in c:
        if not(i[1] in b):
            b[i[1]] = [i]
        else:
            b[i[1]] = b[i[1]] + [i]
    return b to_group b = {} b {} for i in c:
        if not(i[1] in b):
            b[i[1]] = [i]
        else:
            b[i[1]] = b[i[1]] + [i]
     i c if not(i[1] in b):
            b[i[1]] = [i]
        else:
            b[i[1]] = b[i[1]] + [i]
     not(i[1] in b) i[1] in b i[1] i 1 b b[i[1]] = [i] b[i[1]] b i[1] i 1 [i] i b[i[1]] = b[i[1]] + [i] b[i[1]] b i[1] i 1 b[i[1]] + [i] b[i[1]] b i[1] i 1 [i] i return b b c c def is_stable(c, s): 
    bc = to_group(c)
    bs = to_group(s(c, lambda x: x[1]))
    # print(bc)
    # print(bs)
    for k in bc:
        if bc[k] != bs[k]:
            return "Not stable"
    return "Stable" is_stable bc = to_group(c) bc to_group(c) to_group c bs = to_group(s(c, lambda x: x[1])) bs to_group(s(c, lambda x: x[1])) to_group s(c, lambda x: x[1]) s c lambda x: x[1] x[1] x 1 x for k in bc:
        if bc[k] != bs[k]:
            return "Not stable"
     k bc if bc[k] != bs[k]:
            return "Not stable"
     bc[k] != bs[k] bc[k] bc k bs[k] bs k return "Not stable" "Not stable" return "Stable" "Stable" c c s s n = int(input()) n int(input()) int input() input c = input().split() c input().split() ().split () input split print(" ".join(bubble_sort(c, lambda x: x[1]))) print " ".join(bubble_sort(c, lambda x: x[1])) " ".join " " join bubble_sort(c, lambda x: x[1]) bubble_sort c lambda x: x[1] x[1] x 1 x print(is_stable(c, bubble_sort)) print is_stable(c, bubble_sort) is_stable c bubble_sort print(" ".join(selection_sort(c, lambda x: x[1]))) print " ".join(selection_sort(c, lambda x: x[1])) " ".join " " join selection_sort(c, lambda x: x[1]) selection_sort c lambda x: x[1] x[1] x 1 x print(is_stable(c, selection_sort)) print is_stable(c, selection_sort) is_stable c selection_sort def bubble_sort(cs, f):
    c = cs[:]
    for i in range(len(c) - 1):
        for j in range(len(c) - 1, i, -1):
            if f(c[j - 1]) > f(c[j]):
                swap(c, j - 1, j)
            # print(i, j, c)
    return c def bubble_sort(cs, f):
    c = cs[:]
    for i in range(len(c) - 1):
        for j in range(len(c) - 1, i, -1):
            if f(c[j - 1]) > f(c[j]):
                swap(c, j - 1, j)
            # print(i, j, c)
    return c bubble_sort n = int(input()) int(input()) n def swap(c, i, j):
    t = c[i]
    c[i] = c[j]
    c[j] = t def swap(c, i, j):
    t = c[i]
    c[i] = c[j]
    c[j] = t swap c = input().split() input().split() c def is_stable(c, s): 
    bc = to_group(c)
    bs = to_group(s(c, lambda x: x[1]))
    # print(bc)
    # print(bs)
    for k in bc:
        if bc[k] != bs[k]:
            return "Not stable"
    return "Stable" def is_stable(c, s): 
    bc = to_group(c)
    bs = to_group(s(c, lambda x: x[1]))
    # print(bc)
    # print(bs)
    for k in bc:
        if bc[k] != bs[k]:
            return "Not stable"
    return "Stable" is_stable def selection_sort(cs, f):
    c = cs[:]
    for i in range(len(c)):
        minj = i
        for j in range(i, len(c)):
            if f(c[j]) < f(c[minj]):
                minj = j
        swap(c, i, minj)
        # print(c)
    return c def selection_sort(cs, f):
    c = cs[:]
    for i in range(len(c)):
        minj = i
        for j in range(i, len(c)):
            if f(c[j]) < f(c[minj]):
                minj = j
        swap(c, i, minj)
        # print(c)
    return c selection_sort def to_group(c):
    b = {}
    for i in c:
        if not(i[1] in b):
            b[i[1]] = [i]
        else:
            b[i[1]] = b[i[1]] + [i]
    return b def to_group(c):
    b = {}
    for i in c:
        if not(i[1] in b):
            b[i[1]] = [i]
        else:
            b[i[1]] = b[i[1]] + [i]
    return b to_group
#! /usr/local/bin/python3
# coding: utf-8

def swap(c, i, j):
    t = c[i]
    c[i] = c[j]
    c[j] = t

def bubble_sort(cs, f):
    c = cs[:]
    for i in range(len(c) - 1):
        for j in range(len(c) - 1, i, -1):
            if f(c[j - 1]) > f(c[j]):
                swap(c, j - 1, j)
            # print(i, j, c)
    return c

def selection_sort(cs, f):
    c = cs[:]
    for i in range(len(c)):
        minj = i
        for j in range(i, len(c)):
            if f(c[j]) < f(c[minj]):
                minj = j
        swap(c, i, minj)
        # print(c)
    return c

def to_group(c):
    b = {}
    for i in c:
        if not(i[1] in b):
            b[i[1]] = [i]
        else:
            b[i[1]] = b[i[1]] + [i]
    return b

def is_stable(c, s): 
    bc = to_group(c)
    bs = to_group(s(c, lambda x: x[1]))
    # print(bc)
    # print(bs)
    for k in bc:
        if bc[k] != bs[k]:
            return "Not stable"
    return "Stable"

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

print(" ".join(bubble_sort(c, lambda x: x[1])))
print(is_stable(c, bubble_sort))

print(" ".join(selection_sort(c, lambda x: x[1])))
print(is_stable(c, selection_sort))


