Problem:


Insertion Sort

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

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



Sample input:
6
5 2 4 6 1 3


Sample output:
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

Submission status: Accepted

Solution code:
x = int(input())
li = list(map(int, input().split()))

for i in range(x):
    v = li[i]
    j = i - 1
    while j >= 0 and li[j] > v:
        li[j+1] = li[j]
        j -= 1
    li[j+1] = v
    for j in range(x):
        if j != x-1:
            print(li[j], end=" ")
        elif j == x-1:
            print(li[j], end="\n")

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))

print(' '.join(map(str,a)))

for i in range(1,n):
	v=a[i]
	j=i-1
	while j>=0 and a[j]>v:
		a[j+1]=a[j]
		j=j-1
	a[j+1]=v
	print(' '.join(map(str,a)))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))

for i in range(n):
	v=a[i]
	j=i-1
	while j>=0 and a[j]>v:
		a[j+1]=a[j]
		j=j-1
	a[j+1]=v
	print(' '.join(map(str,a)))

Submission status: Accepted

Solution code:
n=int(input())
A=[0 for i in range(n)]

A=input().split()

for i in range(n):
    A[i]=(int)(A[i])

for i in range(n):
    key=A[i]
    j=i-1
    while j>=0 and A[j]>key:
        A[j+1]=A[j]
        j=j-1
        A[j+1]=key

    for i in range(n):
        if i!=n-1:
            print(A[i],end=" ")
        if i==n-1:
            print(A[i])

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
print(*a)
for i in range(1, len(a)):
  v = a[i]
  j = i - 1
  while 0 <= j and v < a[j]:
    a[j+1] = a[j]
    j -= 1
  a[j+1] = v
  print(*a)

Submission status: Accepted

Solution code:
n=int(input())

list=list(map(int,input().split()))
for i2 in range(len(list)):
    if i2!=len(list)-1:
        print(list[i2],end=' ')
    else :
        print(list[i2])

for i in range(1,n):
    v=list[i]
    j=i-1
    while j>=0 and list[j]>v:
        list[j+1]=list[j]
        j=j-1
    list[j+1]=v
    for i2 in range(len(list)):
        if i2!=len(list)-1:
            print(list[i2],end=' ')
        else :
            print(list[i2])

Submission status: Accepted

Solution code:
#! python3
# insertion_sort.py

N = int(input())
nums = [int(x) for x in input().split(' ')]

print(' '.join([str(x) for x in nums]))
for i in range(1, N):
    v = nums[i]
    j = i-1
    while j >= 0 and nums[j] > v:
        nums[j+1] = nums[j]
        j -= 1
    nums[j+1] = v
    print(' '.join([str(x) for x in nums]))

Submission status: Accepted

Solution code:
def print_array(a):
    s = str(a[0])
    for i in range(1,n):
        s += ' '+str(a[i])
    print(s)
    return 0
n = int(input())
a = list(map(int, input().split()))

print_array(a)

for i in range(1,n):
    v = a[i]
    j = i -1
    while(j>=0 and a[j]>v):
        a[j+1] = a[j]
        j -= 1
    a[j+1] = v
    print_array(a)

Submission status: Accepted

Solution code:
def print_tmp(a_li):
    s = [str(a) for a in a_li]
    t = ' '.join(s)
    print(t)


n = int(input())
a = input()
A = [int(i) for i in a.split(' ')]
print_tmp(A)
for i in range(1, n):
    v = A[i]
    j = i -1
    while j >= 0 and A[j] > v:
        A[j+1] = A[j]
        j -= 1
    A[j+1] = v
    print_tmp(A)

Submission status: Accepted

Solution code:
N=int(input())
A=[0 for i in range(N)]
A=input().split()
for i in range(N):
    A[i]=(int)(A[i])
for i in range(N):
    v=A[i]
    j=i-1
    while j>=0 and A[j]>v:
        A[j+1]=A[j]
        j=j-1
        A[j+1]=v
    for i in range(N):
        if i!=N-1:
            print(A[i],end=" ")
        if i==N-1:
            print(A[i])

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
print(" ".join(map(str, a)))
for i in range(1, n):
    v = a[i]
    j = i - 1
    while j >= 0 and a[j] > v:
        a[j + 1] = a[j]
        j -= 1
    a[j + 1] = v
    print(" ".join(map(str, a)))

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]

print(' '.join(map(str, A)))
    
for i in range(1, N):

    v = A[i]
    j = i - 1
        
    while j >= 0 and A[j] > v:
        A[j+1] = A[j]
        j -= 1
            
    A[j+1] = v
    print(' '.join(map(str, A)))

Submission status: Accepted

Solution code:
n = int(input())
a = (list(map(int, input().split())))

print(' '.join(list(map(str, a))))
for i in range(1, n):
    key = a[i]
    j = i - 1
    while j >= 0 and a[j] > key:
        a[j + 1] = a[j]
        j -= 1
    a[j + 1] = key
    print(' '.join(list(map(str, a))))

Submission status: Accepted

Solution code:
_ = int(input())
a = list(map(int, input().split()))

for i, v in enumerate(a):

    j = i - 1 

    while j >= 0 and a[j] > v:
        a[j+1] = a[j]
        j -= 1

    a[j+1] = v
    print(" ".join(map(str, a)))

Submission status: Accepted

Solution code:

def InsertionSort(l):
    print(" ".join(map(str, l)))
    
    for idx in range(1, len(l)):
        jdx = idx - 1
        while jdx >= 0 and l[idx] < l[jdx]:
            l[jdx], l[idx] = l[idx], l[jdx]
            idx -= 1
            jdx -= 1

        print(" ".join(map(str, l)))

def main():
    _ = input()
    in_list = list(map(int, input().split(' ')))
    
    InsertionSort(in_list)


main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


def show(A):
    for k in range(N):
        if k == N - 1:
            print('{0}'.format(A[k]))
        else:
            print('{0}'.format(A[k]), end=' ')


N = int(input())
A = list(map(int, input().split()))
show(A)

for i in range(1, N):
    v = A[i]
    j = i - 1
    while (j >= 0) and (A[j] > v):
        A[j + 1], A[j] = A[j], v
        j -= 1

    show(A)

Submission status: Accepted

Solution code:
import sys

n = int(input())

line_number = [int(i) for i in sys.stdin.readline().split()]

print(' '.join([str(s) for s in line_number]))

for i in range(1, n):
    v = line_number[i]
    j = i-1
    while (j >= 0) and (line_number[j] > v):
        line_number[j+1] = line_number[j]
        j = j-1
    line_number[j+1] = v
    print(' '.join([str(s) for s in line_number]))

Submission status: Accepted

Solution code:
N = int(input())
ary = list(map(int, input().split(' ')))

def insertion_sort(array, n):
    print_out(array)
    for i in range(1,n):
        v = array[i]
        j = i-1
        while(j>=0 and array[j] > v):
            array[j+1] = array[j]
            j -= 1
        array[j+1] = v
        print_out(array)

def print_out(array):
    array_str = [str(i) for i in array]
    print(" ".join(array_str))

insertion_sort(ary, N)

Submission status: Accepted

Solution code:
"""
挿入ソート
"""
def insertion_sort(args):
    results = []
    for i in range(0, len(args)):
        v = args[i]
        j = i - 1
        while (j >= 0 and args[j] > v):
            args[j+1] = args[j]
            j -= 1
        args[j+1] = v
        results.append(args[:])
    return results

if __name__ == '__main__':
    n = int(input())
    args = list(map(int, input().split()))[0:n]
    results = insertion_sort(args)
    for result in results:
        print(' '.join(map(str, result)))

Submission status: Accepted

Solution code:
def insertion_sort(l):
    print(' '.join(map(str, l)))
    for i in range(1, len(l)):
        tmp = l[i]
        for j in range(i-1, -1, -1):
            if l[j] > tmp:
                l[j+1] = l[j]
            else:
                l[j+1] = tmp
                break
        else:
            l[0] = tmp
        print(' '.join(map(str, l)))
    return l

if __name__ == '__main__':
    N = int(input())
    l = list(map(int, input().split()))

    insertion_sort(l)

Submission status: Accepted

Solution code:
def insertion_sort(l):
    for i in range(1, len(l)):
        tmp = l[i]
        j = i - 1
        while j >= 0 and l[j] > tmp:
            l[j+1] = l[j]
            j -= 1
        l[j+1] = tmp
        print(' '.join(map(str, l)))
    return l



if __name__ == '__main__':
    N = int(input())
    l = list(map(int, input().split()))

    print(' '.join(map(str, l)))
    insertion_sort(l)

Submission status: Accepted

Solution code:
def print_A():
    for k, e in enumerate(A):
        if k == N - 1:
            print(e)
        else:
            print(e, end=' ')


N = int(input())
A = [int(n) for n in input().split()]
print_A()
for i in range(1, N):
    v = A[i]
    j = i - 1
    while j >= 0 and A[j] > v:
        A[j+1] = A[j]
        j -= 1
    A[j+1] = v
    print_A()

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
print(' '.join(map(str,a)))
for i in range(1,N):
    v = a[i]
    j = i - 1
    while j >= 0 and a[j] > v:
        a[j + 1] = a[j]
        j-=1
        a[j + 1] = v
    print(' '.join(map(str,a)))

Submission status: Accepted

Solution code:
N = int(input())
ary = [int(_) for _ in input().split()]


def insertion_sort(ary, verbose=True):

    for i in range(1, N):

        if verbose:
            print(' '.join([str(_) for _ in ary]))

        v = ary[i]
        j = i - 1

        while j >= 0 and ary[j] > v:
            ary[j + 1] = ary[j]
            j -= 1

        ary[j + 1] = v

    print(' '.join([str(_) for _ in ary]))


insertion_sort(ary)

Submission status: Accepted

Solution code:
# ALDS_1_A.

def intinput():
    a = input().split()
    for i in range(len(a)):
        a[i] = int(a[i])
    return a

def show(a):
    # 配列aの中身を出力する。
    _str = ""
    for i in range(len(a) - 1): _str += str(a[i]) + " "
    _str += str(a[len(a) - 1])
    print(_str)

def main():
    n = int(input())
    a = intinput()
    # a[0]から順に、挿入していく。
    for i in range(n):
        # a[i]を順次前の数とswapしていき、
        # 前の数の方が大きくなかったら止める。
        cur = i
        while cur > 0:
            if a[cur - 1] > a[cur]:
                a[cur - 1], a[cur] = a[cur], a[cur - 1]
            else: break
            cur -= 1
        show(a)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ALDS_1_A.

def intinput():
    a = input().split()
    for i in range(len(a)):
        a[i] = int(a[i])
    return a

def show(a):
    # 配列aの中身を出力する。
    _str = ""
    for i in range(len(a) - 1): _str += str(a[i]) + " "
    _str += str(a[len(a) - 1])
    print(_str)

def main():
    n = int(input())
    a = intinput()
    # a[0]から順に、挿入していく。
    for i in range(n):
        # a[i]を順次前の数とswapしていき、
        # 前の数の方が大きくなかったら止める。
        cur = i
        while cur > 0:
            if a[cur - 1] > a[cur]:
                a[cur - 1], a[cur] = a[cur], a[cur - 1]
            else: break
            cur -= 1
        show(a)

if __name__ == "__main__":
    main()

# Accepted.

Submission status: Accepted

Solution code:
#coding:utf-8
n = int(input())
A = list(map(int, input().split()))

B = " ".join([str(num) for num in A])
print(B)

for i in range(1, n):   
    v = A[i]
    j = i - 1
    while j >= 0 and A[j] > v:
        A[j+1], A[j] = A[j], A[j+1]
        j -= 1
        
    B = " ".join([str(num) for num in A])
    print(B)

Submission status: Accepted

Solution code:
def print_A():
    for i, n in enumerate(A):
        if i == N - 1:
            print(n)
        else:
            print(n, end=' ')

def insert():
    for i in range(1, N):
        v = A[i]
        j = i - 1
        while j >= 0 and A[j] > v:
            A[j+1] = A[j]
            j -= 1
        A[j+1] = v
        print_A()

N = int(input())
A = list(map(int, input().split()))
print_A()
insert()

Submission status: Accepted

Solution code:
def insertion_sort(a, n):
    for i in range(1, n):
        v = a[i]
        j = i - 1
        while j >= 0 and a[j] > v:
            a[j + 1] = a[j]
            j -= 1
        a[j + 1] = v
        print_list_split_whitespace(a)


def print_list_split_whitespace(a):
    for x in a[:-1]:
        print(x, end=' ')
    print(a[-1])


n = int(input())
a = input().split()
for i in range(n):
    a[i] = int(a[i])

print_list_split_whitespace(a)
insertion_sort(a, n)

Submission status: Accepted

Solution code:
n = int(input())
x = input().split()
y=[]
for i in range(n):
    y += [int(x[i])]
for i in range(n-1):
    print(y[i],end=' ')
print(y[n-1])

for i in range(1,n):
    v = y[i]
    j = i-1
    while j >= 0 and y[j] > v:
        y[j+1] = y[j]
        j -= 1
    y[j+1] = v
    for j in range(n-1):
        print(y[j],end=' ')
    print(y[n-1])

Submission status: Accepted

Solution code:
def trace(A, N):
    print(*A)

def insertion_sort(A, N):
    for i in range(1, N):
        v = A[i]
        j = i -1
        while j >= 0 and A[j] > v:
            A[j+1] = A[j]
            j -= 1
        A[j+1] = v
        trace(A, N)


N = int(input())
A = list(map(int, input().split()))
trace(A, N)
insertion_sort(A, N)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
#-*- coding: utf-8 -*-

if __name__ == '__main__':
    raw_input()
    l = map(int, raw_input().split())
    for i in range(1, len(l)):
        print ' '.join(map(str, l))
        v = l[i]
        j = i - 1
        while j >= 0 and l[j] > v:
            l[j + 1] = l[j]
            j -= 1
        l[j + 1] = v
    print ' '.join(map(str, l))

Submission status: Accepted

Solution code:
def print_list(array, N):
    for i, e in enumerate(array):
        if i == N - 1:
            print(e)
        else:
            print(e, end=" ")

N = int(input())
a = [int(e) for e in input().split()]
print_list(a, N)
for i in range(1, N):
    v = a[i]
    j = i - 1
    while j >= 0 and a[j] > v:
        a[j+1] = a[j]
        j -= 1
    a[j+1] = v
    print_list(a, N)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))

for i in range(1, N):
    print(' '.join(map(str, A)))

    v = A[i]
    j = i - 1
    while j >= 0 and A[j] > v:
        A[j+1] = A[j]
        j -= 1
    A[j+1] = v

print(' '.join(map(str, A)))

Submission status: Accepted

Solution code:
from sys import stdin
def stdinput():
    return stdin.readline().strip()

def main():
    n = int(stdinput())
    a = list(map(int, stdinput().split()))

    for i in range(0, n):
        v = a[i]
        j = i - 1
        while j >= 0 and a[j] > v:
            a[j+1] = a[j]
            j -= 1
        a[j + 1] = v
        print(' '.join(map(str, a)))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
L = [int(x) for x in input().split()]

print(' '.join(map(str, L)))

for i in range(1, N):
    v = L[i]
    j = i - 1
    while j >= 0 and L[j] > v:
        L[j + 1] = L[j]
        j -= 1
    L[j + 1] = v
    print(' '.join(map(str, L)))

Submission status: Accepted

Solution code:
import sys
n = int(input())
arr = list(map(int, input().split()))

def print_arr(arr):
	for i in range(len(arr)):
		sys.stdout.write(str(arr[i]))
		if i != len(arr) - 1:
			sys.stdout.write(' ')
	print()

for i in range(n):
	key = arr[i]
	j = i - 1
	while j >= 0 and arr[j] > key:
		arr[j + 1] = arr[j]
		j = j - 1
	arr[j + 1] = key
	print_arr(arr)

Submission status: Accepted

Solution code:
import sys

def print_arr(arr):
	for i in range(len(arr)):
		sys.stdout.write(str(arr[i]))
		if i != len(arr) - 1:
			sys.stdout.write(' ')
	print()

n = int(input())
arr = list(map(int, input().split()))

for i in range(n):
	key = arr[i]
	j = i - 1
	while j >= 0 and arr[j] > key:
		arr[j + 1] = arr[j]
		j = j - 1
	arr[j + 1] = key
	print_arr(arr)

Submission status: Accepted

Solution code:
import sys
n = input()
data = map(int, raw_input().split())
for i in range(1,n):
	for k in range(0,n-1):
		sys.stdout.write(str(data[k])+" ")
	sys.stdout.write(str(data[n-1]))
	print
	temp = data[i]
	j = i-1
	while j >= 0 and data[j] > temp:
		data[j+1] = data[j]
		j = j - 1
		data[j+1] = temp
for k in range(0,n-1):
	sys.stdout.write(str(data[k])+" ")
sys.stdout.write(str(data[n-1]))
print

Submission status: Accepted

Solution code:
import sys
def print_arr(arr):
	for i in range(len(arr)):
		sys.stdout.write(str(arr[i]))
		if i != len(arr) - 1:
			sys.stdout.write(' ')
	print()

n = int(input())
arr = list(map(int, input().split()))

for i in range(n):
	key = arr[i]
	j = i - 1
	while j >= 0 and arr[j] > key:
		arr[j + 1] = arr[j]
		j -= 1
	arr[j + 1] = key
	print_arr(arr)

Submission status: Accepted

Solution code:
n = int(input())
list = list(map(int, input().split()))

print(' '.join(map(str,list)))
for i in range(1, n):
    insertNum = list[i]
    j = i - 1
    while j >= 0 and list[j] > insertNum:
        list[j+1] = list[j]
        j-=1
    list[j+1] = insertNum
    print(' '.join(map(str,list)))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))

if __name__ == '__main__':
    print(" ".join(map(str, A)))
    for i in range(1, N):
        v = A[i]
        j = i - 1
        while j >= 0 and A[j] > v:
            A[j+1] = A[j]
            j -= 1
            A[j+1] = v
        print(" ".join(map(str, A)))

Submission status: Accepted

Solution code:
n=int(input())
a=input().split()
for i in range(n):
 k=int(a[i])
 j=i-1
 while j>=0 and int(a[j])>k:
  a[j+1]=a[j]
  j-=1
 a[j+1]=k
 print(*a)

Submission status: Accepted

Solution code:
N = int(input())
arr = [int(n) for n in input().split()]

print(' '.join(map(str, arr)))
for i in range(1, N):
    v = arr[i]
    j = i - 1
    while(j >= 0 and arr[j] > v):
        arr[j + 1] = arr[j]
        j -= 1
    arr[j + 1] = v
    print(' '.join(map(str, arr)))

Submission status: Accepted

Solution code:
import sys

def print_arr(arr):
	for i in range(len(arr)):
		sys.stdout.write(str(arr[i]))
		if i != len(arr) - 1:
			sys.stdout.write(' ')
	print()

n = int(input())
arr = list(map(int, input().split()))

for i in range(n):
	key = arr[i]
	j = i - 1
	while j >= 0 and arr[j] > key:
		arr[j + 1] = arr[j]
		j -= 1
	arr[j + 1] = key
	print_arr(arr)

Submission status: Accepted

Solution code:
def insertionSort(A,N):
    print(' '.join([str(a) for a in A]))
    for i in range(1, N):
        v = A[i]
        j = i - 1
        while j >= 0 and A[j] > v:
            A[j+1] = A[j]
            j -= 1
        A[j+1] = v
        print(' '.join([str(a) for a in A]))

def run():
    N = int(input())
    A = [int(i) for i in input().split()]
    insertionSort(A,N)

if __name__ == '__main__':
    run()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
for i in range(N):
    v = A[i]
    j = i - 1
    while j >= 0 and A[j] > v:
        A[j+1] = A[j]
        j -= 1
    A[j+1] = v
    print(*A)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))

if __name__ == '__main__':
    print(" ".join(map(str, A)))
    for i in range(1, N):
        target_num = A[i]
        for j in range(i):
            if A[j] > target_num:
                tmp_idx = i
                while j < tmp_idx:
                    A[tmp_idx] = A[tmp_idx-1]
                    tmp_idx -= 1
                A[j] = target_num
                break
        print(" ".join(map(str, A)))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))

if __name__ == '__main__':
    print(" ".join(map(str, A)))
    for i in range(1, N):
        target_num = A[i]
        j = i - 1
        while j >= 0 and A[j] > target_num:
            A[j+1] = A[j]
            j -= 1
            A[j+1] = target_num
        print(" ".join(map(str, A)))

Submission status: Accepted

Solution code:
# Insertion Sort

n = int(input())
integers = [int(i) for i in input().rstrip().split()]

def show(a):
    for i in range(len(a)):
        if i < len(a) - 1:
            print(a[i], end = ' ')
    return a[len(a) - 1]

def insertionSort(a, n):
    for i in range(1, n):
        print(show(a))
        v = a[i]
        j = i - 1
        while j >= 0 and v < a[j]:
            a[j + 1] = a[j]
            j -= 1
        a[j + 1] = v
    return show(a)

print(insertionSort(integers, n))

Submission status: Accepted

Solution code:
def insertionSort(A, N):
    print(" ".join(list(map(str, A))))
    for i in range(1, N):
        v = A[i]
        j = i - 1

        while j >= 0 and A[j] > v:
            A[j + 1] = A[j]
            j -= 1

        A[j + 1] = v

        print(" ".join(list(map(str, A))))

def main():
    N = int(input())
    A = [int(i) for i in input().split()]

    insertionSort(A, N)

main()

Submission status: Accepted

Solution code:
def insertionSort(a):
    n = len(a)
    print(*a)
    for i in range(1, n):
        v = a[i]
        j = i-1
        while j >= 0 and a[j] > v:
            a[j+1] = a[j]
            j += -1
        a[j+1] = v
        print(*a)


n = int(input())
a = [int(x) for x in input().split()]

insertionSort(a)

Submission status: Accepted

Solution code:
n = int(input())
nlist = list(map(int, input().split()))
for i in range(n):
    j = i-1
    v = nlist[i]
    while (j >= 0 and nlist[j] > v):
        nlist[j+1] = nlist[j]
        j -= 1
    nlist[j+1] = v
    for k in range(n):
        if (k == n-1):
            print(nlist[k], end = '')
        else:
            print(nlist[k], end = ' ')
    print()

Submission status: Accepted

Solution code:
N = int(input())
arr = list(map(int, input().split()))

for i in range(len(arr)):
    for j in range(i,0,-1):
        if(arr[j] < arr[j-1]):
            temp = arr[j]
            arr[j] = arr[j-1]
            arr[j-1] = temp
        else:
            continue
    print(' '.join(map(str,arr)))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

def insertionSort(A, N):
    """N個の要素を含む0-オリジンの配列A"""
    print(' '.join(map(str, A)))
    for i in range(1, N):
        v = A[i]
        j = i - 1
        while j >= 0 and A[j] > v:
            A[j+1] = A[j]
            j -= 1
        A[j+1] = v
        print(' '.join(map(str, A)))

N = int(input())
A = [int(w) for w in input().split()]

insertionSort(A, N)

Submission status: Accepted

Solution code:
N = int(input())
a = input().split()
b = [int(n) for n in a]


for i in range(N):
    v = b[i]
    j = i-1
    while j>=0 and b[j]>v:
        b[j+1] = b[j]
        j = j-1
        b[j+1] = v
    print(" ".join([str(n) for n in b]))

Submission status: Accepted

Solution code:
def print_row (nums):
    for i in range(len(nums)):
        if i != len(nums) -1 :
            print(nums[i], end = ' ')
        else:
            print(nums[i])
 
n = int(input())
nums = list(map(int, input().split()))
print_row(nums)

for i in range(1,n):
    v = nums[i]
    j = i - 1
    while (j >= 0 and nums[j] > v):
        nums[j+1] = nums[j]
        j -= 1
    nums[j+1] = v
    print_row(nums)

Submission status: Accepted

Solution code:
def main():
    N = int(raw_input())
    A = map(int, raw_input().split())
    sort(A)

def sort(A):
    N = len(A)
    display(A)
    for j in range(1, N):
        key = A[j]
        i = j - 1
        while i >= 0 and A[i] > key:
            A[i+1] = A[i]
            i -= 1
        A[i+1] = key
        display(A)

def display(A):
    print(" ".join(map(str, A)))

main()

Submission status: Accepted

Solution code:
def trace(x):
    for e in x:
        print e,
    print

n = input()
x = map(int, raw_input().split())
trace(x)

for j in range(1, n):
    key = x[j]
    for i in range(j)[::-1]:
        if x[i] > key:
            x[i+1] = x[i]
        else:
            i += 1
            break
    else:
        i = 0
    x[i] = key
    trace(x)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin
stdin.readline()
ar = [int(s) for s in stdin.readline().split()]
print(*ar)
for j in range(1, len(ar)):
    key = ar[j]
    i = j - 1
    while i >= 0 and ar[i] > key:
        ar[i+1] = ar[i]
        i -= 1
    ar[i+1] = key
    print(*ar)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#from __future__ import print_function
import time
import sys
import io
import re
import math
#i = 0
N=int(raw_input())
l=map(int, raw_input().split())
print ' '.join(map(str, l))
for j in range(1,len(l)):
    key=l[j]
    i=j-1
    while i>=0 and l[i] > key:
       l[i+1]=l[i]
       i-=1
    l[i+1]=key
    print ' '.join(map(str, l))

Submission status: Accepted

Solution code:
n = input()
a = map(int, raw_input().split())
print ' '.join(map(str, a))
for j in range(1, len(a)):
	key = a[j]
	i = j - 1
	while (i > -1) and (a[i] > key):
		a[i+1] = a[i]
		i = i - 1
	a[i+1] = key
	print ' '.join(map(str, a))

Submission status: Accepted

Solution code:
n = int(raw_input())
A = map(int,raw_input().split())
print " ".join(map(str,A))
for j in range(1,n):
    key = A[j] 
    i = j - 1  
    while i>=0 and A[i]>key:
        A[i+1] = A[i]
        i = i-1
    A[i+1] = key
    print " ".join(map(str,A))

Submission status: Accepted

Solution code:
N = input()
A = map(int,raw_input().split())
print ' '.join(map(str,A))
for j in range(1,N):
    key = A[j]
    i = j - 1
    while i >= 0 and A[i] > key:
       A[i+1] = A[i]
       i -= 1
    A[i+1] = key
    print ' '.join(map(str,A))

Submission status: Accepted

Solution code:
n = int(raw_input())
a = map(int, raw_input().split())
print " ".join(map(str, a))
for j in range(1,n):
    key = a[j]
    i = j-1
    while i >= 0 and a[i] > key:
        a[i+1] = a[i]
        i = i - 1
    a[i+1] = key
    print " ".join(map(str, a))

Submission status: Accepted

Solution code:
def isort(n,a):
    print " ".join(map(str, a))
    for j in range(1,n):
        key = a[j]
        i = j-1
        while i >= 0 and a[i] > key:
            a[i+1] = a[i]
            i = i - 1
        a[i+1] = key
        print " ".join(map(str, a))

n = int(raw_input())
a = map(int, raw_input().split())
isort(n,a)

Submission status: Accepted

Solution code:
def isort(n,a):
    p(a)
    for j in range(1,n):
        key = a[j]
        i = j-1
        while i >= 0 and a[i] > key:
            a[i+1] = a[i]
            i = i - 1
        a[i+1] = key
        p(a)

def p(a):
    print " ".join(map(str, a))

n = int(raw_input())
a = map(int, raw_input().split())
isort(n,a)

Submission status: Accepted

Solution code:
n = int(raw_input())
a = map(int, raw_input().split())
print " ".join(map(str, a))
for j in range(1,len(a)):
    key = a[j]
    i = j -1
    while i >= 0 and a[i] > key:
        a[i + 1] = a[i]
        i = i -1
    a[i+1]= key
    print " ".join(map(str, a))

Submission status: Accepted

Solution code:
def insertion_sort(A, N):
    for j in range(1, N):
        key = A[j]
        i = j - 1
        while i >= 0 and A[i] > key:
            A[i+1] = A[i]
            i = i - 1
        A[i+1] = key
        print " ".join(map(str, A))

if __name__ == "__main__":
    import sys
    N = int(sys.stdin.readline().strip())
    A = [ int(e) for e in sys.stdin.readline().strip().split() ]
    print " ".join(map(str, A))
    insertion_sort(A, N)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().strip().split()))

for i in range(1, len(a)):
    print(' '.join(map(str, a)))
    key = a[i]
    j = i - 1;
    while j >= 0 and a[j] > key:
        a[j + 1] = a[j]
        j -= 1
    a[j + 1] = key
print(' '.join(map(str, a)))

Submission status: Accepted

Solution code:
num = int(input())
number_list = list(map(int, input().split(" ")))


for i in range(num):
    v = number_list[i]
    j = i - 1
    while j >= 0 and number_list[j] > v:
        number_list[j + 1] = number_list[j]
        j -= 1
    number_list[j + 1] = v
    print(*number_list)

Submission status: Accepted

Solution code:
n = int(input())
N = list(map(int,input().split()))
ans = list(map(str,N))
print(" ".join(ans))
for i in range(n-1):
	v = N[i+1]
	j = i
	while j >= 0 and N[j] > v:
		N[j+1] = N[j]
		j -= 1
		N[j+1] = v
	ans = list(map(str,N))
	print(" ".join(ans))

Submission status: Accepted

Solution code:
n=int(input())
a=[int(x) for x in input().split()]

for j in range(n):
    if j<n-1:
      print(a[j],end=" ")
    else:
      print(a[j])
      
for i in range(1,n):
  v=a[i]
  j=i-1
  while j>=0 and a[j]>v:
    a[j+1]=a[j]
    j-=1
  a[j+1]=v
  for j in range(n):
    if j<n-1:
      print(a[j],end=" ")
    else:
      print(a[j])

Submission status: Accepted

Solution code:
def insertion_sort(a, n):
    print(*a)
    for i in range(1, n):
        v = a[i]
        j = i - 1
        while j >= 0 and a[j] > v:
            a[j+1], a[j] = a[j], a[j+1]
            j -= 1
        print(*a)

n = int(input())
a = list(map(int, input().split()))

insertion_sort(a, n)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
print(*a)
for i in range(1,n):
  v=a[i]
  j=i-1
  while j>=0 and a[j]>v:
    a[j+1]=a[j]
    j-=1
  a[j+1]=v
  print(*a)

Submission status: Accepted

Solution code:
def insertionSort(A, N):
    for i in range(1, N):
        v = A[i]
        j = i - 1
        while j >= 0 and A[j] > v:
            A[j+1] = A[j]
            j -= 1
        A[j+1] = v
        print(*A)
        
if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    print(*A)
    insertionSort(A, N)

Submission status: Accepted

Solution code:
N = int(input())
l = list(map(int, input().split()))
for i in range(N):
    v = l[i]
    j = i - 1
    while j >= 0 and l[j] > v:
        l[j+1] = l[j]
        j -= 1
    l[j+1] = v
    print(' '.join(map(str, l)))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
for i in range(N):
	x = A[i]
	for j in range(i, -1, -1):
		if j != 0 and A[j - 1] > x:
			A[j] = A[j - 1]
		else:
			A[j] = x
			break
	s = ""
	for j in range(N):
		if j != 0:
			s += " "
		s += str(A[j])
	print(s)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))

def output(a):
    print(" ".join(map(str, a)))


def insertionSort(a:list, n:int):
    for i in range(1, n):
        now = a[i] #未ソート部分の先頭（未ソート部分の左端)
        j = i - 1 #jはiのひとつ前から始まる
        while j >= 0 and a[j] > now: #未ソート部分の先頭(now)より左にあり、かつnowよりも大きい要素を後方に移動するためのループ
            #一つ右にjの位置にある要素を代入
            a[j+1] = a[j]
            #jを左に動かしてwhile条件判定
            j -= 1
        #jの位置にはnowを入れる
        a[j + 1] = now
        output(a)
output(a)       
insertionSort(a, n)

Submission status: Accepted

Solution code:
n = int(input())
a = input().split()
for s in range(len(a)-1):
        print(a[s],end=' ')
print(a[-1])
for i in range(1,n):
    x = int(a[i])
    j = i-1
    while j >=0 and int(a[j])>x:
        t = int(a[j])
        r = int(a[j+1])
        a[j+1] = t
        a[j] = r
        j -=1
    s = 0
    for s in range(len(a)-1):
        print(int(a[s]),end=' ')
    print(int(a[-1]))