#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	stable_sort
# CreatedDate:  2020-07-08 14:29:32 +0900
# LastModified: 2020-07-08 14:56:46 +0900
#


import os
import sys
from copy import deepcopy
# import numpy as np
# import pandas as pd


def BubbleSort(a, n):
    for i in range(n-1):
        for j in range(n-1, i, -1):
            if a[j-1][1] > a[j][1]:
                tmp = a[j-1]
                a[j-1] = a[j]
                a[j] = tmp


def SelectionSort(b, n):
    for i in range(n):
        minj = i
        for j in range(i, n):
            if b[j][1] < b[minj][1]:
                minj = j
        tmp = b[minj]
        b[minj] = b[i]
        b[i] = tmp



def main():
    n = int(input())
    s = list(map(str, input().split()))
    a = deepcopy(s)
    b = deepcopy(s)
    BubbleSort(a, n)
    print(a[0], end='')
    for i in range(1, n):
        print(" {}".format(a[i]), end='')
    print()
    print("Stable")

    SelectionSort(b, n)
    print(b[0], end='')
    for i in range(1, n):
        print(" {}".format(b[i]), end='')
    print()
    for a_, b_ in zip(a, b):
        if a_!=b_:
            print("Not stable")
            return
    print("Stable")


if __name__ == "__main__":
    main()
 import os os import sys sys from copy import deepcopy def BubbleSort(a, n):
    for i in range(n-1):
        for j in range(n-1, i, -1):
            if a[j-1][1] > a[j][1]:
                tmp = a[j-1]
                a[j-1] = a[j]
                a[j] = tmp BubbleSort for i in range(n-1):
        for j in range(n-1, i, -1):
            if a[j-1][1] > a[j][1]:
                tmp = a[j-1]
                a[j-1] = a[j]
                a[j] = tmp i range(n-1) range n-1 n 1 for j in range(n-1, i, -1):
            if a[j-1][1] > a[j][1]:
                tmp = a[j-1]
                a[j-1] = a[j]
                a[j] = tmp j range(n-1, i, -1) range n-1 n 1 i -1 if a[j-1][1] > a[j][1]:
                tmp = a[j-1]
                a[j-1] = a[j]
                a[j] = tmp a[j-1][1] > a[j][1] a[j-1][1] [j-1] a j-1 j 1 1 a[j][1] [j] a j 1 tmp = a[j-1] tmp a[j-1] a j-1 j 1 a[j-1] = a[j] a[j-1] a j-1 j 1 a[j] a j a[j] = tmp a[j] a j tmp a a n n def SelectionSort(b, n):
    for i in range(n):
        minj = i
        for j in range(i, n):
            if b[j][1] < b[minj][1]:
                minj = j
        tmp = b[minj]
        b[minj] = b[i]
        b[i] = tmp SelectionSort for i in range(n):
        minj = i
        for j in range(i, n):
            if b[j][1] < b[minj][1]:
                minj = j
        tmp = b[minj]
        b[minj] = b[i]
        b[i] = tmp i range(n) range n minj = i minj i for j in range(i, n):
            if b[j][1] < b[minj][1]:
                minj = j
         j range(i, n) range i n if b[j][1] < b[minj][1]:
                minj = j
         b[j][1] < b[minj][1] b[j][1] [j] b j 1 b[minj][1] [minj] b minj 1 minj = j minj j tmp = b[minj] tmp b[minj] b minj b[minj] = b[i] b[minj] b minj b[i] b i b[i] = tmp b[i] b i tmp b b n n def main():
    n = int(input())
    s = list(map(str, input().split()))
    a = deepcopy(s)
    b = deepcopy(s)
    BubbleSort(a, n)
    print(a[0], end='')
    for i in range(1, n):
        print(" {}".format(a[i]), end='')
    print()
    print("Stable")

    SelectionSort(b, n)
    print(b[0], end='')
    for i in range(1, n):
        print(" {}".format(b[i]), end='')
    print()
    for a_, b_ in zip(a, b):
        if a_!=b_:
            print("Not stable")
            return
    print("Stable") main n = int(input()) n int(input()) int input() input s = list(map(str, input().split())) s list(map(str, input().split())) list map(str, input().split()) map str input().split() ().split () input split a = deepcopy(s) a deepcopy(s) deepcopy s b = deepcopy(s) b deepcopy(s) deepcopy s BubbleSort(a, n) BubbleSort a n print(a[0], end='') print a[0] a 0 end='' end '' for i in range(1, n):
        print(" {}".format(a[i]), end='')
     i range(1, n) range 1 n print(" {}".format(a[i]), end='') print " {}".format(a[i]) " {}".format " {}" format a[i] a i end='' end '' print() print print("Stable") print "Stable" SelectionSort(b, n) SelectionSort b n print(b[0], end='') print b[0] b 0 end='' end '' for i in range(1, n):
        print(" {}".format(b[i]), end='')
     i range(1, n) range 1 n print(" {}".format(b[i]), end='') print " {}".format(b[i]) " {}".format " {}" format b[i] b i end='' end '' print() print for a_, b_ in zip(a, b):
        if a_!=b_:
            print("Not stable")
            return
     a_ b_ zip(a, b) zip a b if a_!=b_:
            print("Not stable")
            return
     a_!=b_ a_ b_ print("Not stable") print "Not stable" return print("Stable") print "Stable" if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main def BubbleSort(a, n):
    for i in range(n-1):
        for j in range(n-1, i, -1):
            if a[j-1][1] > a[j][1]:
                tmp = a[j-1]
                a[j-1] = a[j]
                a[j] = tmp def BubbleSort(a, n):
    for i in range(n-1):
        for j in range(n-1, i, -1):
            if a[j-1][1] > a[j][1]:
                tmp = a[j-1]
                a[j-1] = a[j]
                a[j] = tmp BubbleSort def main():
    n = int(input())
    s = list(map(str, input().split()))
    a = deepcopy(s)
    b = deepcopy(s)
    BubbleSort(a, n)
    print(a[0], end='')
    for i in range(1, n):
        print(" {}".format(a[i]), end='')
    print()
    print("Stable")

    SelectionSort(b, n)
    print(b[0], end='')
    for i in range(1, n):
        print(" {}".format(b[i]), end='')
    print()
    for a_, b_ in zip(a, b):
        if a_!=b_:
            print("Not stable")
            return
    print("Stable") def main():
    n = int(input())
    s = list(map(str, input().split()))
    a = deepcopy(s)
    b = deepcopy(s)
    BubbleSort(a, n)
    print(a[0], end='')
    for i in range(1, n):
        print(" {}".format(a[i]), end='')
    print()
    print("Stable")

    SelectionSort(b, n)
    print(b[0], end='')
    for i in range(1, n):
        print(" {}".format(b[i]), end='')
    print()
    for a_, b_ in zip(a, b):
        if a_!=b_:
            print("Not stable")
            return
    print("Stable") main def SelectionSort(b, n):
    for i in range(n):
        minj = i
        for j in range(i, n):
            if b[j][1] < b[minj][1]:
                minj = j
        tmp = b[minj]
        b[minj] = b[i]
        b[i] = tmp def SelectionSort(b, n):
    for i in range(n):
        minj = i
        for j in range(i, n):
            if b[j][1] < b[minj][1]:
                minj = j
        tmp = b[minj]
        b[minj] = b[i]
        b[i] = tmp SelectionSort
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	stable_sort
# CreatedDate:  2020-07-08 14:29:32 +0900
# LastModified: 2020-07-08 14:56:46 +0900
#


import os
import sys
from copy import deepcopy
# import numpy as np
# import pandas as pd


def BubbleSort(a, n):
    for i in range(n-1):
        for j in range(n-1, i, -1):
            if a[j-1][1] > a[j][1]:
                tmp = a[j-1]
                a[j-1] = a[j]
                a[j] = tmp


def SelectionSort(b, n):
    for i in range(n):
        minj = i
        for j in range(i, n):
            if b[j][1] < b[minj][1]:
                minj = j
        tmp = b[minj]
        b[minj] = b[i]
        b[i] = tmp



def main():
    n = int(input())
    s = list(map(str, input().split()))
    a = deepcopy(s)
    b = deepcopy(s)
    BubbleSort(a, n)
    print(a[0], end='')
    for i in range(1, n):
        print(" {}".format(a[i]), end='')
    print()
    print("Stable")

    SelectionSort(b, n)
    print(b[0], end='')
    for i in range(1, n):
        print(" {}".format(b[i]), end='')
    print()
    for a_, b_ in zip(a, b):
        if a_!=b_:
            print("Not stable")
            return
    print("Stable")


if __name__ == "__main__":
    main()

