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

import sys

def bubble_sort(A):
    for i in range(len(A)):
        for x in range(len(A)-1, i, -1):
            if A[x][1] < A[x-1][1]:
                A[x], A[x-1] = A[x-1], A[x]

def selection_sort(A):
    for i in range(len(A)):
        min_val = A[i][1]
        for x in range(i, len(A)):
            if A[x][1] < min_val:
                min_val = A[x][1]
                min_index = x
        if min_val < A[i][1]:
            A[min_index], A[i] = A[i], A[min_index]

def check_stable(srt_list, org_list):
    prev_val = None
    prev_pos = -1
    for card in srt_list:
        cur_pos = org_list.index(card)
        # print(card, cur_pos)
        if card[1] == prev_val:
            if cur_pos < prev_pos:
                print('Not stable')
                return
        prev_val = card[1]
        prev_pos = cur_pos

    print('Stable')


n = int(sys.stdin.readline())
org_list = sys.stdin.readline().split()
_list1 = org_list[:]
_list2 = org_list[:]

bubble_sort(_list1)
selection_sort(_list2)

print(' '.join(_list1))
check_stable(_list1, org_list)
print(' '.join(_list2))
check_stable(_list2, org_list)
 import sys sys def bubble_sort(A):
    for i in range(len(A)):
        for x in range(len(A)-1, i, -1):
            if A[x][1] < A[x-1][1]:
                A[x], A[x-1] = A[x-1], A[x] bubble_sort for i in range(len(A)):
        for x in range(len(A)-1, i, -1):
            if A[x][1] < A[x-1][1]:
                A[x], A[x-1] = A[x-1], A[x] i range(len(A)) range len(A) len A for x in range(len(A)-1, i, -1):
            if A[x][1] < A[x-1][1]:
                A[x], A[x-1] = A[x-1], A[x] x range(len(A)-1, i, -1) range len(A)-1 len(A) len A 1 i -1 if A[x][1] < A[x-1][1]:
                A[x], A[x-1] = A[x-1], A[x] A[x][1] < A[x-1][1] A[x][1] [x] A x 1 A[x-1][1] [x-1] A x-1 x 1 1 A[x], A[x-1] = A[x-1], A[x] A[x] A x A[x-1] A x-1 x 1 A[x-1] A x-1 x 1 A[x] A x A A def selection_sort(A):
    for i in range(len(A)):
        min_val = A[i][1]
        for x in range(i, len(A)):
            if A[x][1] < min_val:
                min_val = A[x][1]
                min_index = x
        if min_val < A[i][1]:
            A[min_index], A[i] = A[i], A[min_index] selection_sort for i in range(len(A)):
        min_val = A[i][1]
        for x in range(i, len(A)):
            if A[x][1] < min_val:
                min_val = A[x][1]
                min_index = x
        if min_val < A[i][1]:
            A[min_index], A[i] = A[i], A[min_index] i range(len(A)) range len(A) len A min_val = A[i][1] min_val A[i][1] [i] A i 1 for x in range(i, len(A)):
            if A[x][1] < min_val:
                min_val = A[x][1]
                min_index = x
         x range(i, len(A)) range i len(A) len A if A[x][1] < min_val:
                min_val = A[x][1]
                min_index = x
         A[x][1] < min_val A[x][1] [x] A x 1 min_val min_val = A[x][1] min_val A[x][1] [x] A x 1 min_index = x min_index x if min_val < A[i][1]:
            A[min_index], A[i] = A[i], A[min_index] min_val < A[i][1] min_val A[i][1] [i] A i 1 A[min_index], A[i] = A[i], A[min_index] A[min_index] A min_index A[i] A i A[i] A i A[min_index] A min_index A A def check_stable(srt_list, org_list):
    prev_val = None
    prev_pos = -1
    for card in srt_list:
        cur_pos = org_list.index(card)
        # print(card, cur_pos)
        if card[1] == prev_val:
            if cur_pos < prev_pos:
                print('Not stable')
                return
        prev_val = card[1]
        prev_pos = cur_pos

    print('Stable') check_stable prev_val = None prev_val None prev_pos = -1 prev_pos -1 for card in srt_list:
        cur_pos = org_list.index(card)
        # print(card, cur_pos)
        if card[1] == prev_val:
            if cur_pos < prev_pos:
                print('Not stable')
                return
        prev_val = card[1]
        prev_pos = cur_pos

     card srt_list cur_pos = org_list.index(card) cur_pos org_list.index(card) org_list.index org_list index card if card[1] == prev_val:
            if cur_pos < prev_pos:
                print('Not stable')
                return
         card[1] == prev_val card[1] card 1 prev_val if cur_pos < prev_pos:
                print('Not stable')
                return
         cur_pos < prev_pos cur_pos prev_pos print('Not stable') print 'Not stable' return prev_val = card[1] prev_val card[1] card 1 prev_pos = cur_pos prev_pos cur_pos print('Stable') print 'Stable' srt_list srt_list org_list org_list n = int(sys.stdin.readline()) n int(sys.stdin.readline()) int sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline org_list = sys.stdin.readline().split() org_list sys.stdin.readline().split() sys.stdin.readline().split sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline split _list1 = org_list[:] _list1 org_list[:] org_list : _list2 = org_list[:] _list2 org_list[:] org_list : bubble_sort(_list1) bubble_sort _list1 selection_sort(_list2) selection_sort _list2 print(' '.join(_list1)) print ' '.join(_list1) ' '.join ' ' join _list1 check_stable(_list1, org_list) check_stable _list1 org_list print(' '.join(_list2)) print ' '.join(_list2) ' '.join ' ' join _list2 check_stable(_list2, org_list) check_stable _list2 org_list def selection_sort(A):
    for i in range(len(A)):
        min_val = A[i][1]
        for x in range(i, len(A)):
            if A[x][1] < min_val:
                min_val = A[x][1]
                min_index = x
        if min_val < A[i][1]:
            A[min_index], A[i] = A[i], A[min_index] def selection_sort(A):
    for i in range(len(A)):
        min_val = A[i][1]
        for x in range(i, len(A)):
            if A[x][1] < min_val:
                min_val = A[x][1]
                min_index = x
        if min_val < A[i][1]:
            A[min_index], A[i] = A[i], A[min_index] selection_sort def check_stable(srt_list, org_list):
    prev_val = None
    prev_pos = -1
    for card in srt_list:
        cur_pos = org_list.index(card)
        # print(card, cur_pos)
        if card[1] == prev_val:
            if cur_pos < prev_pos:
                print('Not stable')
                return
        prev_val = card[1]
        prev_pos = cur_pos

    print('Stable') def check_stable(srt_list, org_list):
    prev_val = None
    prev_pos = -1
    for card in srt_list:
        cur_pos = org_list.index(card)
        # print(card, cur_pos)
        if card[1] == prev_val:
            if cur_pos < prev_pos:
                print('Not stable')
                return
        prev_val = card[1]
        prev_pos = cur_pos

    print('Stable') check_stable n = int(sys.stdin.readline()) int(sys.stdin.readline()) n org_list = sys.stdin.readline().split() sys.stdin.readline().split() org_list def bubble_sort(A):
    for i in range(len(A)):
        for x in range(len(A)-1, i, -1):
            if A[x][1] < A[x-1][1]:
                A[x], A[x-1] = A[x-1], A[x] def bubble_sort(A):
    for i in range(len(A)):
        for x in range(len(A)-1, i, -1):
            if A[x][1] < A[x-1][1]:
                A[x], A[x-1] = A[x-1], A[x] bubble_sort _list2 = org_list[:] org_list[:] _list2 _list1 = org_list[:] org_list[:] _list1
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys

def bubble_sort(A):
    for i in range(len(A)):
        for x in range(len(A)-1, i, -1):
            if A[x][1] < A[x-1][1]:
                A[x], A[x-1] = A[x-1], A[x]

def selection_sort(A):
    for i in range(len(A)):
        min_val = A[i][1]
        for x in range(i, len(A)):
            if A[x][1] < min_val:
                min_val = A[x][1]
                min_index = x
        if min_val < A[i][1]:
            A[min_index], A[i] = A[i], A[min_index]

def check_stable(srt_list, org_list):
    prev_val = None
    prev_pos = -1
    for card in srt_list:
        cur_pos = org_list.index(card)
        # print(card, cur_pos)
        if card[1] == prev_val:
            if cur_pos < prev_pos:
                print('Not stable')
                return
        prev_val = card[1]
        prev_pos = cur_pos

    print('Stable')


n = int(sys.stdin.readline())
org_list = sys.stdin.readline().split()
_list1 = org_list[:]
_list2 = org_list[:]

bubble_sort(_list1)
selection_sort(_list2)

print(' '.join(_list1))
check_stable(_list1, org_list)
print(' '.join(_list2))
check_stable(_list2, org_list)

