def BubbleSort(C,N):
    for i in range(N):
        j = N-1
        while True:
            if j == 0:
                break
            a,b = list(C[j]),list(C[j-1])
            if a[1] < b[1]:
                tmp = C[j-1]
                C[j-1] = C[j]
                C[j] = tmp
            j -= 1
    return C

def SelectionSort(D,N):
    for i in range(N):
        minj = i
        for j in range(i,N):
            a,b = list(D[j]),list(D[minj])
            if a[1] < b[1]:
                minj = j
        tmp = D[i]
        D[i] = D[minj]
        D[minj] = tmp
    return D

n = int(input())
d = input().split()
d2 = list(d)
d = BubbleSort(d,n)
for i in range(len(d)-1):
    print(d[i],end = ' ')
print(d[-1])
print('Stable')
d2 = SelectionSort(d2,n)
for i in range(len(d2)-1):
    print(d2[i],end = ' ')
print(d2[-1])
if d2 == d:
    print('Stable')
else:
    print('Not stable') def BubbleSort(C,N):
    for i in range(N):
        j = N-1
        while True:
            if j == 0:
                break
            a,b = list(C[j]),list(C[j-1])
            if a[1] < b[1]:
                tmp = C[j-1]
                C[j-1] = C[j]
                C[j] = tmp
            j -= 1
    return C BubbleSort for i in range(N):
        j = N-1
        while True:
            if j == 0:
                break
            a,b = list(C[j]),list(C[j-1])
            if a[1] < b[1]:
                tmp = C[j-1]
                C[j-1] = C[j]
                C[j] = tmp
            j -= 1
     i range(N) range N j = N-1 j N-1 N 1 while True:
            if j == 0:
                break
            a,b = list(C[j]),list(C[j-1])
            if a[1] < b[1]:
                tmp = C[j-1]
                C[j-1] = C[j]
                C[j] = tmp
            j -= 1
     True if j == 0:
                break
             j == 0 j 0 break a,b = list(C[j]),list(C[j-1]) a list(C[j]) list C[j] C j b list(C[j-1]) list C[j-1] C j-1 j 1 if a[1] < b[1]:
                tmp = C[j-1]
                C[j-1] = C[j]
                C[j] = tmp
             a[1] < b[1] a[1] a 1 b[1] b 1 tmp = C[j-1] tmp C[j-1] C j-1 j 1 C[j-1] = C[j] C[j-1] C j-1 j 1 C[j] C j C[j] = tmp C[j] C j tmp j -= 1 j 1 return C C C C N N def SelectionSort(D,N):
    for i in range(N):
        minj = i
        for j in range(i,N):
            a,b = list(D[j]),list(D[minj])
            if a[1] < b[1]:
                minj = j
        tmp = D[i]
        D[i] = D[minj]
        D[minj] = tmp
    return D SelectionSort for i in range(N):
        minj = i
        for j in range(i,N):
            a,b = list(D[j]),list(D[minj])
            if a[1] < b[1]:
                minj = j
        tmp = D[i]
        D[i] = D[minj]
        D[minj] = tmp
     i range(N) range N minj = i minj i for j in range(i,N):
            a,b = list(D[j]),list(D[minj])
            if a[1] < b[1]:
                minj = j
         j range(i,N) range i N a,b = list(D[j]),list(D[minj]) a list(D[j]) list D[j] D j b list(D[minj]) list D[minj] D minj if a[1] < b[1]:
                minj = j
         a[1] < b[1] a[1] a 1 b[1] b 1 minj = j minj j tmp = D[i] tmp D[i] D i D[i] = D[minj] D[i] D i D[minj] D minj D[minj] = tmp D[minj] D minj tmp return D D D D N N n = int(input()) n int(input()) int input() input d = input().split() d input().split() ().split () input split d2 = list(d) d2 list(d) list d d = BubbleSort(d,n) d BubbleSort(d,n) BubbleSort d n for i in range(len(d)-1):
    print(d[i],end = ' ') i range(len(d)-1) range len(d)-1 len(d) len d 1 print(d[i],end = ' ') print d[i] d i end = ' ' end ' ' print(d[-1]) print d[-1] d -1 print('Stable') print 'Stable' d2 = SelectionSort(d2,n) d2 SelectionSort(d2,n) SelectionSort d2 n for i in range(len(d2)-1):
    print(d2[i],end = ' ') i range(len(d2)-1) range len(d2)-1 len(d2) len d2 1 print(d2[i],end = ' ') print d2[i] d2 i end = ' ' end ' ' print(d2[-1]) print d2[-1] d2 -1 if d2 == d:
    print('Stable')
else:
    print('Not stable') d2 == d d2 d print('Stable') print 'Stable' print('Not stable') print 'Not stable' def SelectionSort(D,N):
    for i in range(N):
        minj = i
        for j in range(i,N):
            a,b = list(D[j]),list(D[minj])
            if a[1] < b[1]:
                minj = j
        tmp = D[i]
        D[i] = D[minj]
        D[minj] = tmp
    return D def SelectionSort(D,N):
    for i in range(N):
        minj = i
        for j in range(i,N):
            a,b = list(D[j]),list(D[minj])
            if a[1] < b[1]:
                minj = j
        tmp = D[i]
        D[i] = D[minj]
        D[minj] = tmp
    return D SelectionSort n = int(input()) int(input()) n d = input().split() input().split() d d2 = SelectionSort(d2,n) SelectionSort(d2,n) d2 def BubbleSort(C,N):
    for i in range(N):
        j = N-1
        while True:
            if j == 0:
                break
            a,b = list(C[j]),list(C[j-1])
            if a[1] < b[1]:
                tmp = C[j-1]
                C[j-1] = C[j]
                C[j] = tmp
            j -= 1
    return C def BubbleSort(C,N):
    for i in range(N):
        j = N-1
        while True:
            if j == 0:
                break
            a,b = list(C[j]),list(C[j-1])
            if a[1] < b[1]:
                tmp = C[j-1]
                C[j-1] = C[j]
                C[j] = tmp
            j -= 1
    return C BubbleSort d = BubbleSort(d,n) BubbleSort(d,n) d d2 = list(d) list(d) d2
def BubbleSort(C,N):
    for i in range(N):
        j = N-1
        while True:
            if j == 0:
                break
            a,b = list(C[j]),list(C[j-1])
            if a[1] < b[1]:
                tmp = C[j-1]
                C[j-1] = C[j]
                C[j] = tmp
            j -= 1
    return C

def SelectionSort(D,N):
    for i in range(N):
        minj = i
        for j in range(i,N):
            a,b = list(D[j]),list(D[minj])
            if a[1] < b[1]:
                minj = j
        tmp = D[i]
        D[i] = D[minj]
        D[minj] = tmp
    return D

n = int(input())
d = input().split()
d2 = list(d)
d = BubbleSort(d,n)
for i in range(len(d)-1):
    print(d[i],end = ' ')
print(d[-1])
print('Stable')
d2 = SelectionSort(d2,n)
for i in range(len(d2)-1):
    print(d2[i],end = ' ')
print(d2[-1])
if d2 == d:
    print('Stable')
else:
    print('Not stable')