Problem:
Stable Sort

Let's arrange a deck of cards. There are totally 36 cards of 4 suits(S, H, C, D) and 9 values (1, 2, ... 9). For example, 'eight of heart' is represented by H8 and 'one of diamonds' is represented by D1.

Your task is to write a program which sorts a given set of cards in ascending order by their values using the Bubble Sort algorithms and the Selection Sort algorithm respectively. These algorithms should be based on the following pseudocode:

BubbleSort(C)
1 for i = 0 to C.length-1
2 for j = C.length-1 downto i+1
3 if C[j].value < C[j-1].value
4 swap C[j] and C[j-1]

SelectionSort(C)
1 for i = 0 to C.length-1
2 mini = i
3 for j = i to C.length-1
4 if C[j].value < C[mini].value
5 mini = j
6 swap C[i] and C[mini]

Note that, indices for array elements are based on 0-origin.

For each algorithm, report the stability of the output for the given input (instance). Here, 'stability of the output' means that: cards with the same value appear in the output in the same order as they do in the input (instance).

Input

The first line contains an integer N, the number of cards.

N cards are given in the following line. Each card is represented by two characters. Two consecutive cards are separated by a space character.

Output

In the first line, print the arranged cards provided by the Bubble Sort algorithm. Two consecutive cards should be separated by a space character.

In the second line, print the stability ("Stable" or "Not stable") of this output.

In the third line, print the arranged cards provided by the Selection Sort algorithm. Two consecutive cards should be separated by a space character.

In the fourth line, print the stability ("Stable" or "Not stable") of this output.

Constraints

1 ≤ N ≤ 36

Sample Input 1

5
H4 C9 S4 D2 C3

Sample Output 1

D2 C3 H4 S4 C9
Stable
D2 C3 S4 H4 C9
Not stable

Sample Input 2

2
S1 H1

Sample Output 2

S1 H1
Stable
S1 H1
Stable

Solution in JAVA (judge verdict: Wrong Answer)

import java.util.ArrayList;
import java.util.List;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class Main {

	public static void main(String[] args) {

		InputOutput stdIO = new InputOutput();

		int n = stdIO.getValue();
		List<String> slist = stdIO.getLineString();

		List<Cards> values1 = new ArrayList<>();
		List<Cards> values2 = new ArrayList<>();

		Main my = new Main();

		my.listConverter(slist, values1);
		my.listConverter(slist, values2);

		my.bubbleSort(values1, n);
		stdIO.outputLine(values1);
		System.out.println("Stable");

		my.selectionSort(values2, n);
		stdIO.outputLine(values2);

		my.checkOrder(values1, values2);

		stdIO.close();
	}

	public void bubbleSort(List<Cards> values, int n) {
		for (int i = n; i > 1; i--) {
			for (int j = 0; j < i - 1; j++) {
				if (values.get(j).getNum() > values.get(j + 1).getNum()) {
					values.add(j, values.get(j + 1));
					values.remove(j + 2);
				}
			}
		}
	}

	private void selectionSort(List<Cards> values, int n) {
		for (int i = 0; i < n - 1; i++) {
			Cards minj = values.get(i);
			int minindex = 0;
			for (int j = i; j < n; j++) {
				if (minj.getNum() > values.get(j).getNum()) {
					minj = values.get(j);
					minindex = j;
				}
			}
			if(values.get(i).getNum() > minj.getNum()) {
			values.set(minindex, values.get(i));
			values.set(i, minj);
			}
		}
	}

	private void listConverter(List<String> slist, List<Cards> values) {
		for(int i = 0; i < slist.size(); i++) {
			String suit = slist.get(i).substring(0,1);
			int num = Integer.parseInt(slist.get(i).substring(1, 2));
			values.add(new Cards(suit, num));
		}
	}

	private void checkOrder(List<Cards> values1, List<Cards> values2) {
		for(int i = 0; i < values1.size(); i++) {
			if (!(values1.get(i).toString().equals(values2.get(i).toString()))) {
				System.out.println("Not stable");
				System.exit(0);
			}
		}
		System.out.println("stable");
	}
}

class Cards {
	private final String suit;
	private final int num;

	public Cards(String suit, int num) {
		super();
		this.suit = suit;
		this.num = num;
	}
	@Override
	public String toString() {
		return suit + "" + num;
	}
	public int getNum() {
		return this.num;
	}
}

class InputOutput {
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

	public int getValue() {
		int n = 0;
		try {
			n = Integer.parseInt(br.readLine());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return n;
	}

	public List<Integer> getValueLines(int n) {
		List<Integer> values = new ArrayList<>();
		for (int i = 0; i < n; i++) {
			values.add(getValue());
		}
		return values;
	}

	public List<Integer> getLine() {
		try {
			String[] s = br.readLine().trim().split(" ");
			List<Integer> ary = new ArrayList<>();
			for (int i = 0; i < s.length; i++) {
				ary.add(Integer.parseInt(s[i]));
			}
			return ary;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public List<String> getLineString() {
		try {
			String[] s = br.readLine().trim().split(" ");
			List<String> ary = Arrays.asList(s);
			return ary;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public void close() {
		try {
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public <E> void outputLine(List<E> values) {
		StringBuilder line = new StringBuilder();
		for (int k = 0; k < values.size(); k++) {
			line.append(values.get(k) + " ");
		}
		System.out.println(line.delete(line.length() - 1, line.length()));
	}
}