def buble_sort(seq):
    l = len(seq)
    res = seq[:]
    for i in range(l):
        for j in range(i+1, l)[::-1]:
            if res[j][-1] < res[j-1][-1]:
                res[j], res[j-1] = res[j-1], res[j]
    return res


def selection_sort(seq):
    l = len(seq)
    res = seq[:]
    for i in range(l):
        mi = i
        for j in range(i, l):
            if res[j][-1] < res[mi][-1]:
                mi = j
        if i is not mi:
            res[i], res[mi] = res[mi], res[i]
    return res


def is_stable(seq, sorted_seq):
    card_dic = {s[-1]: [] for s in set(seq)}
    for s in seq:
        key = s[-1]
        card_dic[key].append(s[0])

    for ss in sorted_seq:
        key = ss[-1]
        val = card_dic[key].pop(0)
        if not val == ss[0]:
            return False
    return True


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

b_sorted = buble_sort(C)
print(' '.join(b_sorted))
print('Stable' if is_stable(C, b_sorted) else 'Not stable')

s_sorted = selection_sort(C)
print(' '.join(s_sorted))
print('Stable' if is_stable(C, s_sorted) else 'Not stable') def buble_sort(seq):
    l = len(seq)
    res = seq[:]
    for i in range(l):
        for j in range(i+1, l)[::-1]:
            if res[j][-1] < res[j-1][-1]:
                res[j], res[j-1] = res[j-1], res[j]
    return res buble_sort l = len(seq) l len(seq) len seq res = seq[:] res seq[:] seq : for i in range(l):
        for j in range(i+1, l)[::-1]:
            if res[j][-1] < res[j-1][-1]:
                res[j], res[j-1] = res[j-1], res[j]
     i range(l) range l for j in range(i+1, l)[::-1]:
            if res[j][-1] < res[j-1][-1]:
                res[j], res[j-1] = res[j-1], res[j]
     j range(i+1, l)[::-1] (i+1, l) range i+1 i 1 l ::-1 -1 if res[j][-1] < res[j-1][-1]:
                res[j], res[j-1] = res[j-1], res[j]
     res[j][-1] < res[j-1][-1] res[j][-1] [j] res j -1 res[j-1][-1] [j-1] res j-1 j 1 -1 res[j], res[j-1] = res[j-1], res[j] res[j] res j res[j-1] res j-1 j 1 res[j-1] res j-1 j 1 res[j] res j return res res seq seq def selection_sort(seq):
    l = len(seq)
    res = seq[:]
    for i in range(l):
        mi = i
        for j in range(i, l):
            if res[j][-1] < res[mi][-1]:
                mi = j
        if i is not mi:
            res[i], res[mi] = res[mi], res[i]
    return res selection_sort l = len(seq) l len(seq) len seq res = seq[:] res seq[:] seq : for i in range(l):
        mi = i
        for j in range(i, l):
            if res[j][-1] < res[mi][-1]:
                mi = j
        if i is not mi:
            res[i], res[mi] = res[mi], res[i]
     i range(l) range l mi = i mi i for j in range(i, l):
            if res[j][-1] < res[mi][-1]:
                mi = j
         j range(i, l) range i l if res[j][-1] < res[mi][-1]:
                mi = j
         res[j][-1] < res[mi][-1] res[j][-1] [j] res j -1 res[mi][-1] [mi] res mi -1 mi = j mi j if i is not mi:
            res[i], res[mi] = res[mi], res[i]
     i is not mi i mi res[i], res[mi] = res[mi], res[i] res[i] res i res[mi] res mi res[mi] res mi res[i] res i return res res seq seq def is_stable(seq, sorted_seq):
    card_dic = {s[-1]: [] for s in set(seq)}
    for s in seq:
        key = s[-1]
        card_dic[key].append(s[0])

    for ss in sorted_seq:
        key = ss[-1]
        val = card_dic[key].pop(0)
        if not val == ss[0]:
            return False
    return True is_stable s[-1]: [] for s in set(seq) for s in set(seq) s set(seq) set seq for s in set(seq) s[-1] s -1 [] card_dic = {s[-1]: [] for s in set(seq)} card_dic {s[-1]: [] for s in set(seq)} for s in seq:
        key = s[-1]
        card_dic[key].append(s[0])

     s seq key = s[-1] key s[-1] s -1 card_dic[key].append(s[0]) [key].append [key] card_dic key append s[0] s 0 for ss in sorted_seq:
        key = ss[-1]
        val = card_dic[key].pop(0)
        if not val == ss[0]:
            return False
     ss sorted_seq key = ss[-1] key ss[-1] ss -1 val = card_dic[key].pop(0) val card_dic[key].pop(0) [key].pop [key] card_dic key pop 0 if not val == ss[0]:
            return False
     not val == ss[0] val == ss[0] val ss[0] ss 0 return False False return True True seq seq sorted_seq sorted_seq n = int(input()) n int(input()) int input() input C = input().split() C input().split() ().split () input split b_sorted = buble_sort(C) b_sorted buble_sort(C) buble_sort C print(' '.join(b_sorted)) print ' '.join(b_sorted) ' '.join ' ' join b_sorted print('Stable' if is_stable(C, b_sorted) else 'Not stable') print 'Stable' if is_stable(C, b_sorted) else 'Not stable' is_stable(C, b_sorted) is_stable C b_sorted 'Stable' 'Not stable' s_sorted = selection_sort(C) s_sorted selection_sort(C) selection_sort C print(' '.join(s_sorted)) print ' '.join(s_sorted) ' '.join ' ' join s_sorted print('Stable' if is_stable(C, s_sorted) else 'Not stable') print 'Stable' if is_stable(C, s_sorted) else 'Not stable' is_stable(C, s_sorted) is_stable C s_sorted 'Stable' 'Not stable' def selection_sort(seq):
    l = len(seq)
    res = seq[:]
    for i in range(l):
        mi = i
        for j in range(i, l):
            if res[j][-1] < res[mi][-1]:
                mi = j
        if i is not mi:
            res[i], res[mi] = res[mi], res[i]
    return res def selection_sort(seq):
    l = len(seq)
    res = seq[:]
    for i in range(l):
        mi = i
        for j in range(i, l):
            if res[j][-1] < res[mi][-1]:
                mi = j
        if i is not mi:
            res[i], res[mi] = res[mi], res[i]
    return res selection_sort C = input().split() input().split() C n = int(input()) int(input()) n def is_stable(seq, sorted_seq):
    card_dic = {s[-1]: [] for s in set(seq)}
    for s in seq:
        key = s[-1]
        card_dic[key].append(s[0])

    for ss in sorted_seq:
        key = ss[-1]
        val = card_dic[key].pop(0)
        if not val == ss[0]:
            return False
    return True def is_stable(seq, sorted_seq):
    card_dic = {s[-1]: [] for s in set(seq)}
    for s in seq:
        key = s[-1]
        card_dic[key].append(s[0])

    for ss in sorted_seq:
        key = ss[-1]
        val = card_dic[key].pop(0)
        if not val == ss[0]:
            return False
    return True is_stable s_sorted = selection_sort(C) selection_sort(C) s_sorted def buble_sort(seq):
    l = len(seq)
    res = seq[:]
    for i in range(l):
        for j in range(i+1, l)[::-1]:
            if res[j][-1] < res[j-1][-1]:
                res[j], res[j-1] = res[j-1], res[j]
    return res def buble_sort(seq):
    l = len(seq)
    res = seq[:]
    for i in range(l):
        for j in range(i+1, l)[::-1]:
            if res[j][-1] < res[j-1][-1]:
                res[j], res[j-1] = res[j-1], res[j]
    return res buble_sort b_sorted = buble_sort(C) buble_sort(C) b_sorted
def buble_sort(seq):
    l = len(seq)
    res = seq[:]
    for i in range(l):
        for j in range(i+1, l)[::-1]:
            if res[j][-1] < res[j-1][-1]:
                res[j], res[j-1] = res[j-1], res[j]
    return res


def selection_sort(seq):
    l = len(seq)
    res = seq[:]
    for i in range(l):
        mi = i
        for j in range(i, l):
            if res[j][-1] < res[mi][-1]:
                mi = j
        if i is not mi:
            res[i], res[mi] = res[mi], res[i]
    return res


def is_stable(seq, sorted_seq):
    card_dic = {s[-1]: [] for s in set(seq)}
    for s in seq:
        key = s[-1]
        card_dic[key].append(s[0])

    for ss in sorted_seq:
        key = ss[-1]
        val = card_dic[key].pop(0)
        if not val == ss[0]:
            return False
    return True


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

b_sorted = buble_sort(C)
print(' '.join(b_sorted))
print('Stable' if is_stable(C, b_sorted) else 'Not stable')

s_sorted = selection_sort(C)
print(' '.join(s_sorted))
print('Stable' if is_stable(C, s_sorted) else 'Not stable')