• Selection Sort Aizu


    Write a program of the Selection Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode:

    SelectionSort(A)
    1 for i = 0 to A.length-1
    2 mini = i
    3 for j = i to A.length-1
    4 if A[j] < A[mini]
    5 mini = j
    6 swap A[i] and A[mini]
    Note that, indices for array elements are based on 0-origin.

    Your program should also print the number of swap operations defined in line 6 of the pseudocode in the case where i ≠ mini.

    Input

    The first line of the input includes an integer N, the number of elements in the sequence.

    In the second line, N elements of the sequence are given separated by space characters.

    Output

    The output consists of 2 lines.

    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

    6
    5 6 4 2 1 3

    Sample Output 1

    1 2 3 4 5 6
    4

    Sample Input 2

    6
    5 2 4 6 1 3

    Sample Output 2

    1 2 3 4 5 6
    3

    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 —— Selection Sort Aizu - ALDS1_2_B .cpp created by VB_KoKing on 2019,04,28,18.
    /* Procedural objectives:
    
     Variables required by the program:
    
     Procedural thinking:
    
     Functions required by the program:
    
    */
    /* 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],mini;
    
    void SelectionSort(int A[], int n) {
        for (int i = 0; i < n; i++) {
            mini=i;
            for (int j = i; j < n; j++) {
                if (A[j]<A[mini])
                    mini = j;
            }
            swap(A[i],A[mini]);
            if (i!=mini) ans++;
        }
    }
    
    int main() {
        int n;
        cin >> n;
        memset(array, 0, sizeof(array));
        for (int i = 0; i < n; i++)
            cin >> array[i];
        SelectionSort(array, n);
        for (int i = 0; i < n; i++) {
            cout << array[i];
            if (i != n - 1) cout << ' ';
        }
        cout << endl << ans << endl;
        return 0;
    }
    
  • 相关阅读:
    聚类算法
    shell编程之运算符
    linux shell编程子bash变量
    java SSM框架单元测试最佳实战代码
    在maven项目中使用Junit进行单元测试(一)
    JMeter之Http协议接口性能测试
    TestNG学习笔记新的
    单元测试最佳实战项目代码下载
    单元测试中使用mock最好不要使用easymock而应该使用powermock
    springboot 读写excel
  • 原文地址:https://www.cnblogs.com/AlexKing007/p/12338400.html
Copyright © 2020-2023  润新知