Bubble Sort Aizu - ALDS1_2_A
Write a program of the Bubble Sort algorithm which sorts a sequence A in ascending order.
編寫一個氣泡排序算法程序,按升序對序列A進行排序。
The algorithm should be based on the following pseudocode:
該算法應基于以下偽代碼:
BubbleSort(A)
1 for i = 0 to A.length-1
2 for j = A.length-1 downto i+1
3 if A[j] < A[j-1]
4 swap A[j] and A[j-1]
Note that, indices for array elements are based on 0-origin.
注意,數組元素的索引基于0原點。
Your program should also print the number of swap operations defined in line 4 of the pseudocode.
您的程序還應該打印偽代碼第4行中定義的交換操作數。
Input
The first line of the input includes an integer N, the number of elements in the sequence.
輸入的第一行包括一個整數n,即序列中的元素數。
In the second line, N elements of the sequence are given separated by spaces characters.
在第二行中,序列的n個元素由空格字符分隔。
Output
The output consists of 2 lines.
輸出由2條線組成。
In the first line, please print the sorted sequence.
在第一行,請打印排序順序。
Two contiguous elements of the sequence should be separated by a space character.
序列的兩個相鄰元素應使用空格字符分隔。
In the second line, please print the number of swap operations.
在第二行,請打印交換操作的數量。
Constraints
1 ≤ N ≤ 100
Sample Input 1
5
5 3 2 4 1
Sample Output 1
1 2 3 4 5
8
Sample Input 2
6
5 2 4 6 1 3
Sample Output 2
1 2 3 4 5 6
9
code
/*^....0^ .1 ^1^.. 011.^ 1.0^ 1 ^ ^0.11 ^ ^..^0. ^ 0^.0 1 .^.1 ^0 .........001^.1 1. .111100....01^00 ^ 11^ ^1. .1^1.^ ^0 0^.^ ^0..1.1 1..^1 .0 ^ ^^ 00. ^^0.^1 ^ 0 ^^110.^0. 0 ^ ^^^10.01^^ 010^ 1 1 ^^^1110.10001 10 0 ^ 1.1 ^^^11111100^ 10 . 01 ^^ ^^ ^^^1111^1.^ ^^^10 10^ 0^ ^^111^^^0.1^ 1....^11 0 ^^11^^^ 0.. ....1^ ^ ^1. 0^ ^11^^^ ^ 1 111^ ^ 0.10 00 11 ^^^^^ 1 0 1.0^ ^0 ^0 ^^^^ 0 0.0^ 1.0 .^ ^^^^ 1 1 .0^.^ ^^ 0^ ^1 ^^^^ 0. ^.11 ^ 11 1. ^^^ ^ ^ ..^^..^ ^1 ^.^ ^^^ .0 ^.00..^ ^0 01 ^^^ .. 0..^1 .. .1 ^.^ ^^^ 1 ^ ^0001^ 1. 00 0. ^^^ ^.0 ^.1. 0^. ^.^ ^.^ ^^^ ..0.01 .^^. .^ 1001 ^^ ^^^ . 1^. ^ ^. 11 0. 1 ^ ^^ 0.0 ^. 0 ^0 1 ^^^ 0.0.^ 1. 0^ 0 .1 ^^^ ...1 1. 00 . .1 ^^^ ..1 1. ^. 0 .^ ^^ ..0. 1. .^ . 0 ..1 1. 01 . . ^ 0^.^ 00 ^0 1. ^ 1 1.0 00 . ^^^^^^ ..^ 00 01 ..1. 00 10 1 ^^.1 00 ^. ^^^ .1.. 00 .1 1..01 ..1.1 00 1. ..^ 10^ 1^ 00 ^.1 0 1 1.1 00 00 ^ 1 ^. 00 ^.^ 10^ ^^1.1 00 00 10^..^ 1. ^. 1.0 1 ^. 00 00 .^^ ^. ^ 1 00 ^0000^ ^ 011 0 ^. 00.0^ ^00000 1.00.1 11. 1 0 1^^0.01 ^^^ 01.^ ^ 1 1^^ ^.^1 1 0... 1 ^1 1^ ^ .01 ^ 1.. 1.1 ^0.0^ 0 1..01^^100000..0^1 1 ^ 1 ^^1111^ ^^0 ^ ^ 1 1000^.1 ^.^ . 00.. 1.1 0. 01. . 1. .^1. 1 1. ^0^ . ^.1 00 01^.0 001. .^*/ // Virtual_Judge —— Bubble Sort Aizu - ALDS1_2_A.cpp created by VB_KoKing on 2019,04,28,12. /* Procedural objectives: 冒泡排序并統計交換次數Variables required by the program:1.全局變量ans計算交換次數2.局部變量n表示數組的大小3.存儲元素的數組array[100]Procedural thinking:1.冒泡程序偽代碼改成代碼Functions required by the program:1.冒泡排序函數 */ /* My dear Max said: "I like you, So the first bunch of sunshine I saw in the morning is you, The first hurricane that passed through your ear is you, The first star you see is also you. The world I see is all your shadow."FIGHTING FOR OUR FUTURE!!! */ #include <iostream> #include <cstring>using namespace std; int ans = 0, array[100];void BubbleSort(int A[], int n) {for (int i = 0; i < n - 1; i++)for (int j = n - 1; j > i; j--)if (A[j] < A[j - 1]) {swap(A[j], A[j - 1]);ans++;} }int main() {int n;cin >> n;memset(array, 0, sizeof(array));for (int i = 0; i < n; i++)cin >> array[i];BubbleSort(array, n);for (int i = 0; i < n; i++) {cout << array[i];if (i != n - 1) cout << ' ';}cout << endl << ans << endl;return 0; }總結
以上是生活随笔為你收集整理的Bubble Sort Aizu - ALDS1_2_A的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 1132:石头剪子布
- 下一篇: Stable Sort Aizu - A