#!/usr/bin/env python
# -*- coding:utf-8 -*-
from __future__ import print_function
import time
import sys
import io
import re
import math
start = time.clock()

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

sys.stdin.readline()
data1=sys.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 print_function import time time import sys sys import io io import re re import math math start = time.clock() start time.clock() time.clock time clock 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 sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline data1=sys.stdin.readline().split() data1 sys.stdin.readline().split() sys.stdin.readline().split sys.stdin.readline() sys.stdin.readline sys.stdin sys 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' data2=data1[:] data1[:] data2 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 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 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 orders=[o for o in orders if len(c)>1] [o for o in orders if len(c)>1] orders 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=sys.stdin.readline().split() sys.stdin.readline().split() data1 start = time.clock() time.clock() start
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from __future__ import print_function
import time
import sys
import io
import re
import math
start = time.clock()

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

sys.stdin.readline()
data1=sys.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')