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.Scanner;

//Stable Sort
public class Main {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int num = scan.nextInt();

		//準備
		Card[] card = new Card[num];
		for (int i = 0; i < num; i++) {
			card[i] = Card.newInstance(scan.next());
		}

		Card[] bubbleBefore = copyCard(card);
		Card[] selectBefore = copyCard(card);
		
		//バブルソート
		Card[] bubbleAfter = bubbleSort(bubbleBefore, num);
		//バブルソート出力
		System.out.print(bubbleAfter[0]);
		for (int i = 1; i < num; i++) {
			System.out.print(" " + bubbleAfter[i].toString());
		}
		System.out.println();
		System.out.println(stableCheck(bubbleAfter,card));

		//セレクションソート
		Card[] selectAfter = selectionSort(selectBefore,num);
		//セレクトソート出力
		System.out.print(selectAfter[0]);
		for (int i = 1; i < num; i++) {
			System.out.print(" " + selectAfter[i].toString());
		}
		System.out.println();
		System.out.println(stableCheck(selectAfter,card));
		System.out.println();
	}

	/**
	 * バブルソート
	 * 
	 * @param A
	 *            ソートする配列
	 * @param N
	 *            ソートする個数
	 * @return 昇順にソートされた配列
	 */
	public static Card[] bubbleSort(Card[] A, int N) {
		boolean flag = true;
		while (flag) {
			flag = false;
			for (int j = N - 1; j >= 1; j--) {
				if (A[j].getNumber() < A[j - 1].getNumber()) {
					Card tmp;
					tmp = A[j];
					A[j] = A[j - 1];
					A[j - 1] = tmp;
					flag = true;
				}
			}
		}
		return A;
	}



	/**
	 * セレクションソート。一番小さい値を選択して入れ替える。
	 * 
	 * @param A配列
	 * @param N配列の数
	 * @return入れ替え後の配列
	 */
	public static Card[] selectionSort(Card[] A, int N) {

		for (int i = 0; i < N; i++) {
			int mini = i;
			for (int j = i; j < N; j++) {
				if (A[j].getNumber() < A[mini].getNumber()) {
					mini = j;
				}
			}
			if (mini != i) {
				Card tmp;
				tmp = A[i];
				A[i] = A[mini];
				A[mini] = tmp;
			}

		}

		return A;
	}

	public   static String stableCheck(Card[] Aftersort, Card[] Input){
		int i;
		int temae_no_card_no_ichi=0;
		int ato_no_card_no_ichi =0;
		for(i=0;i<Input.length-1;i++){
			if((Aftersort[i].getNumber())==(Aftersort[i+1].getNumber())){
				for(int j = 0; j < Input.length;j++){
					if(Aftersort[i].toString().equals(Input[j].toString())){//H5 =H5
					 temae_no_card_no_ichi = j;
					}
					if(Aftersort[i+1].toString().equals(Input[j].toString())){
						 ato_no_card_no_ichi = j;
					}
				}
			}
		}
		if(temae_no_card_no_ichi > ato_no_card_no_ichi){
			return "Not Stable";
		}
		return "Stable";

	}
	
	public static Card[] copyCard(Card[] card){
		
		Card[] copycard =  new Card[card.length];
		for (int i = 0; i < card.length; i++) {
			copycard[i] = Card.newInstance(card[i].toString());
		}
		return copycard;
	}



	private static class Card {
		private String mark;
		private int number;

		private Card(String str) {
			mark = str.substring(0, 1);
			number = Integer.valueOf(str.substring(1));
		}

		public static Card newInstance(String str) {
			return new Card(str);
		}

		public int getNumber() {
			return number;
		}

		public String getMark() {
			return mark;
		}

		@Override
		public String toString() {
			return mark + number;
		}
	}

}