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)))


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)))
print('Stable' if isStable(arr, b_arr) else 'Not stable')
s_arr = SelectionSort(arr)
print(' '.join(map(str, s_arr)))
print('Stable' if isStable(arr, s_arr) else 'Not stable')
 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) Card def __init__(self, mark, value):
        self.mark = mark
        self.value = value

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

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

     __lt__ return self.value < other.value self.value < other.value self.value self value other.value other value self self other other def __str__(self):
        return self.mark + str(self.value) __str__ return self.mark + str(self.value) self.mark + str(self.value) self.mark self mark str(self.value) str self.value self value self self 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 BubbleSort Card(c.mark, c.value) for c in arr for c in arr c arr for c in arr Card(c.mark, c.value) Card c.mark c mark c.value c value res = [Card(c.mark, c.value) for c in arr] res [Card(c.mark, c.value) for c in arr] swap_flg = True swap_flg True i = 0 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
     swap_flg swap_flg = False 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
         j range(len(res) - 1, i, -1) range len(res) - 1 len(res) len res 1 i -1 if (res[j] < res[j - 1]):
                res[j], res[j - 1] = res[j - 1], res[j]
                swap_flg = True
         res[j] < res[j - 1] res[j] res j res[j - 1] res j - 1 j 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 swap_flg = True swap_flg True i += 1 i 1 return res res arr arr 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 SelectionSort Card(c.mark, c.value) for c in arr for c in arr c arr for c in arr Card(c.mark, c.value) Card c.mark c mark c.value c value res = [Card(c.mark, c.value) for c in 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]
     i range(0, len(res)) range 0 len(res) len res minj = i minj i for j in range(i, N):
            if res[j] < res[minj]:
                minj = j
         j range(i, N) range i N if res[j] < res[minj]:
                minj = j
         res[j] < res[minj] res[j] res j res[minj] res minj minj = j minj j if (i != minj):
            res[i], res[minj] = res[minj], res[i]
     i != minj i minj res[i], res[minj] = res[minj], res[i] res[i] res i res[minj] res minj res[minj] res minj res[i] res i return res res arr arr 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))) isStable stable_arr = BubbleSort(in_arr) stable_arr BubbleSort(in_arr) BubbleSort in_arr return all(out_arr[i] == stable_arr[i] for i in range(0, len(out_arr))) all(out_arr[i] == stable_arr[i] for i in range(0, len(out_arr))) all out_arr[i] == stable_arr[i] out_arr[i] out_arr i stable_arr[i] stable_arr i in_arr in_arr out_arr out_arr N = int(input()) N int(input()) int input() input Card(s[0], int(s[1])) for s in input().split() for s in input().split() s input().split() ().split () input split for s in input().split() Card(s[0], int(s[1])) Card s[0] s 0 int(s[1]) int s[1] s 1 arr = [Card(s[0], int(s[1])) for s in input().split()] arr [Card(s[0], int(s[1])) for s in input().split()] b_arr = BubbleSort(arr) b_arr BubbleSort(arr) BubbleSort arr print(' '.join(map(str, b_arr))) print ' '.join(map(str, b_arr)) ' '.join ' ' join map(str, b_arr) map str b_arr print('Stable' if isStable(arr, b_arr) else 'Not stable') print 'Stable' if isStable(arr, b_arr) else 'Not stable' isStable(arr, b_arr) isStable arr b_arr 'Stable' 'Not stable' s_arr = SelectionSort(arr) s_arr SelectionSort(arr) SelectionSort arr print(' '.join(map(str, s_arr))) print ' '.join(map(str, s_arr)) ' '.join ' ' join map(str, s_arr) map str s_arr print('Stable' if isStable(arr, s_arr) else 'Not stable') print 'Stable' if isStable(arr, s_arr) else 'Not stable' isStable(arr, s_arr) isStable arr s_arr 'Stable' 'Not stable' s_arr = SelectionSort(arr) SelectionSort(arr) s_arr 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) 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) Card 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 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))) isStable b_arr = BubbleSort(arr) BubbleSort(arr) b_arr 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 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 BubbleSort N = int(input()) int(input()) N arr = [Card(s[0], int(s[1])) for s in input().split()] [Card(s[0], int(s[1])) for s in input().split()] arr 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 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 SelectionSort
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)))


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)))
print('Stable' if isStable(arr, b_arr) else 'Not stable')
s_arr = SelectionSort(arr)
print(' '.join(map(str, s_arr)))
print('Stable' if isStable(arr, s_arr) else 'Not stable')

