前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Stable Sort

Stable Sort

作者头像
废江_小江
发布2022-09-05 13:17:01
1350
发布2022-09-05 13:17:01
举报
文章被收录于专栏:总栏目

Question

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:

代码语言:javascript
复制
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]
代码语言:javascript
复制
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

Meaning

题目的意思是要求我们用冒泡排序和选择排序来对一副扑克牌进行排序,仅仅排序扑克牌中的数字,花色不管。先用冒泡排序进行输出,然后输出排序结果是否稳定,接着用选择排序进行输出,然后输出排序结果是否稳定

Solution

扑克牌的花色可以用一个结构体来存储,在排序算法比较的时候只需要比较扑克牌中的值即可。难点在于如何判断选择排序算法排序后的结果是否是稳定的。其实做法很简单可以和冒泡排序后的结果比较一下就可以了

Coding

代码语言:javascript
复制
#if 1
#include<iostream>
using namespace std;
//结构体中的字符存扑克牌的花色,数字存扑克牌的大小
typedef struct {
	char word;
	int value;
}Card;
//冒泡排序算法
void BubbleSort(Card a[], int n) {
	int i, j;
	for (i = 0; i < n; i++) {
		for (j = n - 1; j > i; j--) {
			if (a[j].value < a[j - 1].value)
			{
				swap(a[j], a[j - 1]);
			}
		}
	}
}
//选择排序算法
void SelectionSort(Card a[], int n) {
	int i, j, min;
	for (i = 0; i < n - 1; i++) {
		//第一层循环为无序区
		min = i;//k记录当前的下标,到时需要交换
		for (j = i + 1; j < n; j++) {
			//第二层循环为有序区
			if (a[j].value < a[min].value)
				min = j; //此时min便是最小的数
		}
		if (min != i) {
			//为了防止最小数有两个,即最外层for循环i是最小数,无序区也有一个最小数,这样交换过来排序便不再稳定
			swap(a[min], a[i]);
		}
	}
}
void Print(Card a[], int n) {
	for (int i = 0; i < n; i++) {
		if (i > 0) cout << " ";
		cout << a[i].word<<a[i].value;
	}
}
bool Stable(Card c1[], Card c2[] ,int n) {
	int i;
	for (i = 0; i < n; i++) {
		if (c1[i].word != c2[i].word)
			return false;
	}
	return true;
}
int main() {
	Card c1[100], c2[100];
	int n; cin >> n;
	for (int i = 0; i < n; i++)
		cin >> c1[i].word >> c1[i].value;
	for (int i = 0; i < n; i++)
		c2[i] = c1[i];
	BubbleSort(c1, n);
	SelectionSort(c2, n);
	Print(c1, n);
	cout << endl << "Stable" << endl;//冒泡排序得到的结论总是稳定的
	Print(c2, n);
	bool bl=Stable(c1, c2, n);
	if (bl)
		cout<<endl << "Stable" << endl;
	else
		cout<<endl << "Not stable" << endl;
	
}
#endif

废江博客 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权

转载请注明原文链接:Stable Sort

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-03-28),如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Question
  • Meaning
  • Solution
  • Coding
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档