#!/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, cards):
    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 c.endswith(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 stable_test(orders, data1) else 'Not stable')

selection(data2)
print(*data2)
print('Stable' if stable_test(orders, data2) else 'Not stable') 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] bubble 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] i range(len(cards)) range len(cards) 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] j range(len(cards)-1, i, -1) range len(cards)-1 len(cards) len cards 1 i -1 if cards[j][1] < cards[j-1][1]:
                cards[j], cards[j-1] = cards[j-1], cards[j] cards[j][1] < cards[j-1][1] cards[j][1] [j] cards j 1 cards[j-1][1] [j-1] cards j-1 j 1 1 cards[j], cards[j-1] = cards[j-1], cards[j] cards[j] cards j cards[j-1] cards j-1 j 1 cards[j-1] cards j-1 j 1 cards[j] cards j cards cards 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] selection 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] i range(len(cards)) range len(cards) len cards mini = i mini i for j in range(i, len(cards)):
            if cards[j][1] < cards[mini][1]:
                mini = j
         j range(i, len(cards)) range i len(cards) len cards if cards[j][1] < cards[mini][1]:
                mini = j
         cards[j][1] < cards[mini][1] cards[j][1] [j] cards j 1 cards[mini][1] [mini] cards mini 1 mini = j mini j cards[i], cards[mini] = cards[mini], cards[i] cards[i] cards i cards[mini] cards mini cards[mini] cards mini cards[i] cards i cards cards def stable_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 stable_test for order in orders:
        n = -1
        for c in order:
            p = n
            n = cards.index(c)
            if p > n:
                return False
     order orders n = -1 n -1 for c in order:
            p = n
            n = cards.index(c)
            if p > n:
                return False
     c order p = n p n n = cards.index(c) n cards.index(c) cards.index cards index c if p > n:
                return False
     p > n p n return False False return True True orders orders cards cards stdin.readline() stdin.readline stdin readline data1 = stdin.readline().split() data1 stdin.readline().split() stdin.readline().split stdin.readline() stdin.readline stdin readline split data2 = data1[:] data2 data1[:] data1 : [c for c in data1 if c.endswith(str(i))] for i in range(1, 10) for i in range(1, 10) i range(1, 10) range 1 10 for i in range(1, 10) [c for c in data1 if c.endswith(str(i))] orders = [[c for c in data1 if c.endswith(str(i))] for i in range(1, 10)] orders [[c for c in data1 if c.endswith(str(i))] for i in range(1, 10)] o for o in orders if len(c) > 1 for o in orders if len(c) > 1 o orders len(c) > 1 len(c) len c 1 if len(c) > 1 o orders = [o for o in orders if len(c) > 1] orders [o for o in orders if len(c) > 1] bubble(data1) bubble data1 print(*data1) print *data1 data1 print('Stable' if stable_test(orders, data1) else 'Not stable') print 'Stable' if stable_test(orders, data1) else 'Not stable' stable_test(orders, data1) stable_test orders data1 'Stable' 'Not stable' selection(data2) selection data2 print(*data2) print *data2 data2 print('Stable' if stable_test(orders, data2) else 'Not stable') print 'Stable' if stable_test(orders, data2) else 'Not stable' stable_test(orders, data2) stable_test orders data2 'Stable' 'Not stable' 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 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] bubble data1 = stdin.readline().split() stdin.readline().split() data1 def stable_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 def stable_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 stable_test data2 = data1[:] data1[:] data2 orders = [o for o in orders if len(c) > 1] [o for o in orders if len(c) > 1] orders 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 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] selection orders = [[c for c in data1 if c.endswith(str(i))] for i in range(1, 10)] [[c for c in data1 if c.endswith(str(i))] for i in range(1, 10)] orders
#!/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, cards):
    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 c.endswith(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 stable_test(orders, data1) else 'Not stable')

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