博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Aizu - ALDS1_1_A Insertion Sort 插入排序
阅读量:3904 次
发布时间:2019-05-23

本文共 1369 字,大约阅读时间需要 4 分钟。

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

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.

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.

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

Output

The output consists of N lines. 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

65 2 4 6 1 3

Sample Output 1

5 2 4 6 1 32 5 4 6 1 32 4 5 6 1 32 4 5 6 1 31 2 4 5 6 31 2 3 4 5 6

Sample Input 2

31 2 3

Sample Output 2

1 2 31 2 31 2 3
#include 
#include
#include
#include
using namespace std;const int maxn=105;int n;int a[maxn];void InsertSort (){ for (int i=0;i
=0&&a[loc]>temp) { a[loc+1]=a[loc]; loc--; } a[loc+1]=temp; for (int j=0;j

 

 

转载地址:http://spaen.baihongyu.com/

你可能感兴趣的文章
Android开发填坑之setUseWideViewPort
查看>>
前期准备:搭建代码阅读环境(Mac上搭建OpenGrok查看JDK源码)
查看>>
有关使用xsl输出csv格式文档的实践小结
查看>>
在Ubuntu 12.04 为 Eclipse 添加快速启动项
查看>>
GCC强大背后
查看>>
Android x86模拟器Intel Atom x86 System Image配置与使用方法
查看>>
shell脚本兼容linux/unix与windows/cygwin的基础(注意处理好CR, LF, CR/LF 回车 换行的问题)
查看>>
【分享】手把手教你使用U盘安装Ubuntu系统
查看>>
Ubuntu下adb无法识别android设备的解决方法
查看>>
使用U盘安装Ubuntu系统的实践小结
查看>>
编译cscope-15.8a遇到的问题与解决方案
查看>>
ubuntu下海信Hisense E920 usb连接不上的处理与adb的连接
查看>>
findbugs的ant脚本实践
查看>>
Ubuntu 12.04 安装 Subversion 1.7
查看>>
scp port 22: Connection refused
查看>>
ubuntu12.04命令行下安装RabbitVCS
查看>>
自定义cscope-index
查看>>
(ubuntu)在andorid andk工程中使用ccache加速编译速度
查看>>
android graphics system学习资料汇总
查看>>
GDB
查看>>