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^..     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 —— 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;
}

Insertion Sort Aizu - ALDS1_1_A相关推荐

  1. [Leetcode] Insertion Sort List

    Sort a linked list using insertion sort. 虽然算法很简单,但是链表操作起来实正是烦啊,特别要注意各种边界条件. 1 /** 2 * Definition for ...

  2. leetcode day2 -- Sort List Insertion Sort List

    1.Sort List Sort a linked list in O(n log n) time using constant space complexity. 分析:对链表排序不是第一次见,但是 ...

  3. [Leetcode]147. Insertion Sort List

    Sort a linked list using insertion sort. 链表的插入排序 思路,递归到链表尾,然后循环插入: 1 /** 2 * Definition for singly-l ...

  4. LeetCode 147. Insertion Sort List 链表插入排序 C++/Java

    LeetCode 147. Insertion Sort List 链表插入排序 C++/Java Sort a linked list using insertion sort. A graphic ...

  5. Java实现插入排序及其优化 insertion sort

    本文带来八大排序算法之插入排序. 插入排序(Insertion Sort)属于内部排序算法,是对于欲排序的元素以插入的方式找寻该元素的适当位置,以达到排序的目的. 插入排序基本思想: 把n个待排序的元 ...

  6. python实现排序算法_python实现·十大排序算法之插入排序(Insertion Sort)

    简介 插入排序(Insertion Sort)是一种简单直观的排序算法.它的工作原理是:通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入. 算法实现步骤 从第一个元素开 ...

  7. c++Insertion Sort插入排序的实现算法(附完整源码)

    C++Insertion Sort插入排序的实现算法 C++Insertion Sort插入排序的实现算法完整源码(定义,实现,main函数测试) C++Insertion Sort插入排序的实现算法 ...

  8. C语言插入排序Insertion Sort算法(附完整源码)

    插入排序Insertion Sort算法 插入排序Insertion Sort算法的完整源码(定义,实现,main函数测试) 插入排序Insertion Sort算法的完整源码(定义,实现,main函 ...

  9. C语言以递归实现插入排序Insertion Sort算法(附完整源码)

    以递归实现插入排序Insertion Sort算法 以递归实现插入排序Insertion Sort算法的完整源码(定义,实现,main函数测试) 以递归实现插入排序Insertion Sort算法的完 ...

最新文章

  1. 算法题:实现一个IP白名单过滤器
  2. About The FTP
  3. 【二分】防具布置/秦腾与教学评估(ybtoj 二分-1-2/jzoj 1253/luogu 4403)
  4. 关于 Docker ,你必须了解的核心都在这里了!
  5. jhat命令 Java Heap Analyse Tool
  6. HBase之重试机制
  7. ajax context this,如何使ajax里的this指向不改变
  8. 多线程模拟渡河 C语言 Linux
  9. vb怎么自动连接服务器,VB 如何制作连接服务器的进程
  10. SGI和Intel计划在2018年以前,将超级计算机速度提高500倍
  11. 20种最常见的网络安全攻击类型
  12. 机器人仿真论文阅读2
  13. 椭圆周长c语言,怎么计算上椭圆周长,下椭圆周长,和展开面周长
  14. SitePoint播客#57:不是负面
  15. 在HTML中怎么去掉超链接(标签 a)的下划线?
  16. Android App Bundle(aab)与UnsatisfiedLinkError
  17. mpvue开发微信小程序多级联动功能
  18. 【应用】1200PLC实现三层电梯模拟控制
  19. 从 Spec.到芯片_(数字IC、模拟IC、FPGA/CPLD设计的流程及EDA工具)
  20. 《程序员升职记》0.简介

热门文章

  1. respberry pi3 上手随记
  2. 57. Android之程序调试LogCat (转)
  3. EasyUI datagrid : 启用行号、固定列及多级表头后,头部行号位置单元格错位的问题...
  4. NewSQL数据库VoltDB特性简介
  5. Goodbye, 2010. Hello 2011...
  6. vscode更改插件路径_vscode插件分享
  7. mysql设置参数0和1_MySQL 8.0 首个自适应参数横空出世
  8. 30 个 OpenStack 经典面试问题和解答
  9. flask的第一个hello word 程序
  10. osgEarth使用没有DX的Triton库Triton-MT-DLL-NODX.lib