//ALDS1_2_C_19140837_Ojima_Yukiya.cpp
//
//提出日 2020/07/01 wednesday
//
//プログラミング基礎演習2 CS 第8回課題 ALDS1_2_C
//
//学修番号 19140837　小島優希也

#include <iostream>
#include <vector>
using namespace std;

void display(vector<string> v) {
    for (int i = 0; i < v.size() - 1; i++) {
        cout << v.at(i) << " ";
    }
    cout << v.at(v.size() - 1) << endl;
}

void swap(string& a, string& b) {
    string temp = a;
    a = b;
    b = temp;
}

void selectionsort(vector<string> a) {
    vector<string> origi = a;
    for (int i = 0; i < a.size(); i++) {
        int minj = i;
        for (int j = i; j < a.size(); j++) {
            if (a.at(j).at(1) < a.at(minj).at(1)) {
                minj = j;
            }
        }
        if (minj != i) {
            swap(a.at(i), a.at(minj));
        }
    }
    display(a);
    bool stable = true;
    for (int i = 0; i < a.size() - 1; i++) {
        if (a.at(i).at(1) == a.at(i + 1).at(1)) {
            char value = a.at(i).at(1);

            int plus = 1;
            for (int j = i + 1; j < a.size() - 1; j++) {
                if (a.at(j).at(1) == a.at(j + 1).at(1)) {
                    plus++;
                }
                else {
                    break;
                }
            }

            for (int j = origi.size() - 1; j >= 0; j--) {
                if (origi.at(j).at(1) == value) {
                    if (origi.at(j).at(0) == a.at(i + plus).at(0)) {
                        stable = true;
                        plus--;
                        if (plus == -1) {
                            break;

                        }
                    }
                    else {
                        stable = false;
                        break;
                    }
                }
            }

        }
        if (!stable) {
            break;
        }
    }
    if (stable) {
        cout << "Stable" << endl;
    }
    else {
        cout << "Not stable" << endl;
    }
}

void bubblesort(vector<string> a) {
    vector<string> origi = a; //copy
    bool flag = true;
    while (flag) {
        flag = false;
        for (int j = a.size() - 1; j >= 1; j--) {
            if (a.at(j).at(1) < a.at(j - 1).at(1)) {
                swap(a.at(j), a.at(j-1));
                flag = true;
            }
        }
    }
    display(a);
    bool stable = true;
    for (int i = 0; i < a.size() - 1; i++) {
        if (a.at(i).at(1) == a.at(i + 1).at(1)) {
            char value = a.at(i).at(1);

            int plus = 1;
            for (int j = i + 1; j < a.size() - 1; j++) {
                if (a.at(j).at(1) == a.at(j + 1).at(1)) {
                    plus++;
                }
                else {
                    break;
                }
            }

            for (int j = origi.size() - 1; j >= 0; j--) {
                if (origi.at(j).at(1) == value) {
                    if (origi.at(j).at(0) == a.at(i + plus).at(0)) {
                        stable = true;
                        plus--;
                        if (plus == -1) {
                            break;

                        }
                    }
                    else {
                        stable = false;
                        break;
                    }
                }
            }

        }
        if (!stable) {
            break;
        }
    }
    if (stable) {
        cout << "Stable" << endl;
    }
    else {
        cout << "Not stable" << endl;
    }
}

int main(void) {
    vector<string> vec;
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        string s;
        cin >> s;
        vec.push_back(s);
    }
    bubblesort(vec);
    selectionsort(vec);
    return 0;
}


Measured memory_bytes: 3216