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:
1 2 3 4 5 |
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] |
1 2 3 4 5 6 7 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
#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 |