• 欢迎访问废江网站,承蒙遇见 QQ群
  • 本站将致力于推送优质的java知识以及算法,开源代码!

Stable Sort

AOJ 站点默认 4年前 (2020-03-28) 1529次浏览 已收录 1个评论 扫描二维码
文章目录[隐藏]

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:

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


废江博客 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Stable Sort
喜欢 (0)
[]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址