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^
                               ..     01
                              1.^     1.0
                             ^ 1  ^    ^0.1
                             1 ^        ^..^
                             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.1
                   0001  10 0   ^ 1.1                                   ^^^1111110
                   0^ 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.         ^.1
                   1 ^      11                             1.                ^^^     ^ ^        ..^
                  ^..^      ^1                             ^.^               ^^^       .0       ^.0
                  0..^      ^0                              01               ^^^       ..      0..^
                 1 ..        .1                             ^.^              ^^^       1 ^  ^0001
                ^  1.        00                              0.             ^^^        ^.0 ^.1
                . 0^.        ^.^                             ^.^            ^^^         ..0.0
               1 .^^.         .^                  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^     ^               01
     1 0             ^.               00.0^              ^00000   1.00.1              11
     . 1              0               1^^0.01                      ^^^                01
      .^              ^                1   1^^                                       ^.^
    1 1                                                                              0.
    ..                                                                              1 ^
     1                                                                               1
   ^ ^                                                                             .0
   1                                                                             ^ 1
   ..                                                          1.1            ^0.0
  ^ 0                                                           1..01^^100000..0^
  1 1                                                            ^ 1 ^^1111^ ^^
  0 ^                                                             ^ 1      1000^
  .1                                                               ^.^     .   00
  ..                                                                1.1    0.   0
  1.                                                                  .    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;
}
posted @ 2019-04-28 12:55  AlexKing007  阅读(97)  评论(0编辑  收藏  举报