zoukankan      html  css  js  c++  java
  • Insertion Sort Aizu

    Write a program of the Insertion Sort algorithm which sorts a sequence A in ascending order.
    编写插入排序算法的程序,按升序对序列A排序。
    The algorithm should be based on the following pseudo code:
    该算法应基于以下伪代码:

    for i = 1 to A.length-1
    key = A[i]
    /* insert A[i] into the sorted sequence A[0,…,j-1] */
    j = i - 1
    while j >= 0 and A[j] > key
    A[j+1] = A[j]
    j–
    A[j+1] = key
    Note that, indices for array elements are based on 0-origin.
    注意,数组元素的索引基于0原点。

    To illustrate the algorithms, your program should trace intermediate result for each step.
    为了说明算法,您的程序应该跟踪每个步骤的中间结果。

    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 a single space.
    在第二行中,序列的n个元素由一个空格分隔。

    Output 输出

    The output consists of N lines.
    输出由n行组成。
    Please output the intermediate sequence in a line for each step.
    请为每个步骤一行输出中间序列。
    Elements of the sequence should be separated by single space.
    序列的元素应该用单个空格分隔。

    Constraints 约束条件

    1 ≤ N ≤ 100

    Sample Input 1
    6
    5 2 4 6 1 3
    Sample Output 1
    5 2 4 6 1 3
    2 5 4 6 1 3
    2 4 5 6 1 3
    2 4 5 6 1 3
    1 2 4 5 6 3
    1 2 3 4 5 6
    Sample Input 2
    3
    1 2 3
    Sample Output 2
    1 2 3
    1 2 3
    1 2 3
    Hint
    Template in C

    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 —— Insertion Sort Aizu - ALDS1_1_A.cpp created by VB_KoKing on 2019,04,28,08.
    /* Procedural objectives:
    
     Procedural thinking:
    
     Functions required by the program:
    
     Variables 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 n, A[100];
    
    void print() {
        for (int i = 0; i < n; i++) {
            cout << A[i];
            if (i != n - 1) cout << ' ';
        }
        cout << endl;
    }
    
    void insertionSort(int A[], int N) {
        for (int i = 1; i < N; i++) {
            //从第一个元素开始处理,到第n-1个元素结束
            int v = A[i], j = i - 1;
            while (j >= 0 && A[j] > v) {//当j>=0并且前一个元素大于当前元素的值时进入循环
                A[j + 1] = A[j];
                j--;
            }//前一个元素不大于当前元素的时退出循环
            A[j + 1] = v;
            print();
        }
    }
    
    int main() {
        cin >> n;
        memset(A, 0, sizeof(A));
        for (int i = 0; i < n; i++)
            cin >> A[i];
        print();
        insertionSort(A, n);
        return 0;
    }
    
  • 相关阅读:
    pip不是内部或外部命令也不是可运行的程序或批处理文件的问题
    动态规划 leetcode 343,279,91 & 639. Decode Ways,62,63,198
    动态规划 70.climbing Stairs ,120,64
    (双指针+链表) leetcode 19. Remove Nth Node from End of List,61. Rotate List,143. Reorder List,234. Palindrome Linked List
    建立链表的虚拟头结点 203 Remove Linked List Element,82,147,148,237
    链表 206 Reverse Linked List, 92,86, 328, 2, 445
    (数组,哈希表) 219.Contains Duplicate(2),217 Contain Duplicate, 220(3)
    重装系统
    java常用IO
    端口
  • 原文地址:https://www.cnblogs.com/AlexKing007/p/12338407.html
Copyright © 2011-2022 走看看