AOJ_ALDS_1_2_C

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

代码

#include<stdio.h>
#include<iostream>
using namespace std;

struct Card{
	char suit, value;
};
void bubbleSort(struct Card A[], int N) {
	for (int i = 0; i < N; i++) {
		for (int j = N - 1; j > i; j--) {
			if (A[j-1].value > A[j].value) {
				swap(A[j - 1], A[j]);
			}
		}
	}
}
void selectSort(struct Card A[], int N) {
	int minj;
	for (int i = 0; i < N; i++) {
		minj = i;
		for (int j = i; j < N; j++) {
			if (A[j].value < A[minj].value) {
				minj = j;
			}
		}
		swap(A[minj], A[i]);
	}
}

void print(Card A[], int N) {
	for (int i = 0; i < N; i++) {
		if (i != 0) cout << " ";
		cout << A[i].suit << A[i].value ;
	}
	cout << endl;
}
int isstable(Card C1[], Card C2[], int N) {
	int tag = 1;
	for (int i = 0; i < N; i++) {
		if (C1[i].suit != C2[i].suit||C1[i].value!=C2[i].value) {
			tag = false;
		}
	}
	return tag;
}
int main() {
	Card C1[100],C2[100];
	int N;
	char ch;

	cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> C1[i].suit >> C1[i].value;
	}
	for (int i = 0; i < N; i++) {
		C2[i] = C1[i];
	}
	bubbleSort(C1, N);
	selectSort(C2, N);

	print(C1, N);
	cout << "Stable" << endl;
	print(C2, N);
	if (isstable(C1,C2,N)){
		cout << "Stable" << endl;
	}
	else {
		cout << "Not stable" << endl;
	}
	
	return 0;
}

要点

  • 对于扑克牌这种非基本结构,需要用结构体存储,本题主要练习了结构体的操作,对于结构体的定义:

  • struct Card{
    	char suit, value;
    };
    

    我们定义了一个Card结构体类型,里面有suit,value两个成员,我们可以通过.suit,.value来访问他们

  • 这样我们就能将扑克牌存储在这样的结构体数组中

  • 我们以Card A[i].value为排序的元素,对其进行冒泡和选择排序,方法和之前的数组操作基本类似

  • 对是否是稳定排序,我们已经知道冒泡是一个稳定排序对于选择排序,我们可以通过和冒泡比较来发现这一排序是都是稳定的

书上代码

因为这次做题参考了书上代码,所以函数定义基本一样,只是函数的具体实现有一些差异

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值