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:
def insertionSort(data):
	for old in range(len(data)):
		new = old
		while new>0 and data[old]<data[max(0,new-1)]:
			new -= 1
		i = data.pop(old)
		data.insert(new,i)
		yield data

input()
data = [int(s) for s in input().split()]
[print(' '.join([str(i) for i in d])) for d in insertionSort(data)]

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
    # ?????????????????¨????????????????????§????????????
    # ????±? > ??????????±????????????????????±???????????????????1?????????????????? j ?????????????????????
    # ????±? < ??????????±??????????break ??????????????´??????
    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 main():
    N = int(input())
    A = tuple(map(int, input().split()))

    a = list(A)
    print(*a, sep=' ')
    insertionSort(a, N)

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, sep=' ')

main()

Submission status: Accepted

Solution code:
n = int(input())
k = [int(i) for i in input().split()]

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

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
    B=A.copy()
    B=list(map(str,B))
    print(" ".join(B))

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:
def insertionSort(a):
    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(*a)

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

insertionSort(a)

Submission status: Accepted

Solution code:
n = int(input())
*A, = 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()))
print(*A)

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)

insertionSort(A, N)

Submission status: Accepted

Solution code:
n = raw_input()
A = map(int, raw_input().split())
for i in range(0, len(A)):
        if i == len(A)-1:
            print A[i]
        else:
            print A[i],
            
for i in range(1, len(A)):
    j = i - 1
    tmp = A[i]
    while A[j] > tmp and j >= 0:
        tmp2 = A[j]
        A[j] = tmp
        A[j+1] = tmp2
        j -= 1
        
    for i in range(0, len(A)):
        if i == len(A)-1:
            print A[i]
        else:
            print A[i],

Submission status: Accepted

Solution code:
# your code goes here
N=int(input())
A=[int(i) for i in input().split()]
B=""
for k in range(N-1):
    B+=str(A[k])+" "
B+=str(A[N-1])
print(B)
for i in range(1,N):
    j=i-1
    v=A[i]
    while j>=0 and A[j]>v:
        A[j+1]=A[j]
        j-=1
    A[j+1]=v
    B=""
    for k in range(N-1):
        B+=str(A[k])+" "
    B+=str(A[N-1])
    print(B)

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(str(A).replace(",",'').replace("[",'').replace("]",''))


N=int(input())
sl=input()
print(sl)
lis=list(map(int,sl.split()))
insertionSort(lis,N)

Submission status: Accepted

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

        j-=1
        z[j+1]=v
    print(' '.join(list(map(str, z))))

Submission status: Accepted

Solution code:
# Aizu Problem ALDS_1_1_A: Insertion Sort
#
import sys, math, os

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input2.txt", "rt")


def printA(A):
    print(' '.join([str(a) for a in A]))

def insertion_sort(A):
    for i in range(1, len(A)):
        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 -= 1
        A[j+1] = key
        printA(A)

        
N = int(input())
A = [int(_) for _ in input().split()]
printA(A)
insertion_sort(A)

Submission status: Accepted

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

for i in range(num):
    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(" ".join(map(str,array)))

Submission status: Accepted

Solution code:
def output(list):
    for i,row in enumerate(list):
        if i == 0:
            print(row,end='')
        else:
            print('',row,end='')
    
def main():
    s = input()
    s_list = []
    s_list = list(map(int,input().split()))

    output(s_list)
    print()

    i = 0
    for count in range(len(s_list)-1):

#   Judge: now > next
        if s_list[i] > s_list[i+1]:

#       Change: now > next
            ins_num = s_list[i+1]
            s_list[i+1] = s_list[i]
            s_list[i]   = ins_num

            j = i - 1

            while j >= 0:
                if s_list[j] > ins_num:
                    s_list[j+1] = s_list[j]
                    s_list[j]   = ins_num

                j -= 1
            output(s_list)
        else:
           output(s_list)        
    
        print()
        i += 1

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def sort(a):
    n = len(a)
    for i in range(1,n):
        for j in range(i):
            p = i-j
            if a[p] < a[p-1]:
                a[p], a[p-1] = a[p-1], a[p]
        print ' '.join(map(str, a))

n = int(raw_input())
a = map(int, raw_input().split(' '))
print ' '.join(map(str, a))
sort(a)

Submission status: Accepted

Solution code:
def print_1(array):
    for i in range(len(array)):
        if i == len(array) - 1:
            print(array[i])
        else:
            print(array[i], end=" ")

N = int(input())
A = [int(n) for n in input().split()]
print_1(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_1(A)

Submission status: Accepted

Solution code:
n = int(input())
a = map(int,raw_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 " ".join(map(str,a))

Submission status: Accepted

Solution code:
def insertionSort(A, N):
    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

N = int(raw_input())
A = map(int, raw_input().split())

insertionSort(A, N)

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

Submission status: Accepted

Solution code:
def insertionSort(A, N):
    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


N = int(raw_input())
A = map(int, raw_input().split())

insertionSort(A, N)

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

Submission status: Accepted

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

for i in range( nums ):
    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(*array)

Submission status: Accepted

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

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]
            j -= 1
        A[j+1] = v
        print(*A)

insertion_sort(data, n)

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(" ".join(list(map(str, A))))

Submission status: Accepted

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

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())

array = input().split()

intarr = [int(x) for x in array]
for x in intarr:
    if x == intarr[-1]:
        print(x)
    else:
        print(x,end=' ')


for c in range(1,n):
    v = intarr[c]
    j = c-1
    while j>=0 and intarr[j]>v:
        intarr[j+1] = intarr[j]
        j -= 1
    intarr[j+1] = v
    for x in intarr:
        if x == intarr[-1]:
            print(x)
        else:
            print(x, end=' ')

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    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())
data = list(map(int, input().split()))

def insertionsort(a, n):
    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)

insertionsort(data, n)

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 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 inserts(N):
    k = 1
    for n in N:
        print(n, end='')
        if k < len(N):
            print(' ', end='')
        k += 1

    print('')
    for i in range(1,len(N)):
        # ?????????????´??????????????????£????????¨?????????
        v = N[i]
        j = i - 1
        while j >= 0 and N[j] > v:
            N[j + 1] = N[j]
            j -= 1
        N[j + 1] = v
        k = 1
        for n in N:
            print(n, end='')
            if k < len(N):
                print(' ', end='')
            k += 1
        print('')


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

inserts(numbers)

Submission status: Accepted

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

def insertionSort(A,N):
    for i in range(1,N):
        print(" ".join(map(str, A)))
        v = A[i]
        j = i -1
        
        while (j >= 0) & (A[j]>v):
            A[j+1] = A[j]
            j -= 1
            
        A[j+1] = v
    print(" ".join(map(str, A)))
    
insertionSort(A, N)

Submission status: Accepted

Solution code:
def inputInline():
    N = int(input())
    numbers = list(map(int, input().split(" ")))
    return numbers


def insertionSort(numbers):
    N = len(numbers)
    for i in range(1, N):
        print(" ".join(list(map(str, numbers))))
        j = i - 1
        temp = numbers[i]
        while True:
            if j == -1:
                numbers[0] = temp
                break
            if temp < numbers[j]:
                numbers[j + 1] = numbers[j]
                j -= 1
            else:
                numbers[j + 1] = temp
                break
    return " ".join(list(map(str, numbers)))


print(insertionSort(inputInline()))

Submission status: Accepted

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

aaa=" ".join(map(str,a))
print(aaa)

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=j-1
    a[j+1]=key
    aaa=" ".join(map(str,a))
    print(aaa)

Submission status: Accepted

Solution code:
#! /usr/bin/env python
# -*- coding : utf-8 -*-
input()
seq = [int(x) for x in input().split()]
for i in range(0, len(seq)):
    key = seq[i]
    j = i - 1
    assert isinstance(i, int)
    while j >= 0 and seq[j] > key:
        seq[j + 1] = seq[j]
        j -= 1
    seq[j + 1] = key
    print(' '.join(map(str,seq)))

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
    for k in range(n):
        if k == n-1:
            print(a[k],end='')
        else:
            print(a[k],end=' ')
    print()

Submission status: Accepted

Solution code:
N, A = int(input()), list(map(int, input().split()))
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]))

Submission status: Accepted

Solution code:
def pd(A):
    print(" ".join(map(str,A)))

def insertionSort(A, N):
    pd(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
        pd(A)

    return A

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

Submission status: Accepted

Solution code:
def insertionSort(A,N):
    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(" ".join(map(str,A)))

N = int(input())
A = []
A = list(map(int,input().split()))
insertionSort(A,N)

Submission status: Accepted

Solution code:
a = []
def outputList(a):
	for i in range(len(a)):
		if i != len(a)-1:
			print("%d"%a[i],end=' ')
		else:
			print("%d"%a[i],end='\n')

n = int(input().strip())
for i in input().split():
	a.append(int(i))
	if len(a)==n:
		break
for i in range(1,len(a)):
	outputList(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
outputList(a)

Submission status: Accepted

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


if __name__ == "__main__":
    n = int(input())
    lst = [int(n) for n in input().split()]
    print(" ".join([str(n) for n in lst]))
    insertionSort(lst, n)

Submission status: Accepted

Solution code:
def insertionSort(lst, n):
    print(" ".join([str(n) for n in lst]))
    for i in range(1, n):
        v = lst[i]
        for j in range(0, i):
            if lst[j] > v:
                lst.insert(j, lst.pop(i))
                break

        print(" ".join([str(n) for n in lst]))

    return lst


if __name__ == "__main__":
    n = int(input())
    lst = [int(n) for n in input().split()]
    insertionSort(lst, n)

Submission status: Accepted

Solution code:
def insersionSort(A, N):
    for i in range(1, N):
        print " ".join([str(k) for k in 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
    return A

if __name__ == "__main__":
    N = int(raw_input())
    A = map(int, raw_input().split())
    A = insersionSort(A, N)
    print " ".join([str(k) for k in 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 -= 1
    A[j+1]= v
    print(" ".join(map(str,A)))

Submission status: Accepted

Solution code:
kazu=int(input())
A=[int(i) for i in input().split()]
# kazu=6
# A=[5,2,4,6,1,3]

j=0
for i in range(kazu - 1):
    print(A[i], end=" ")
print(A[kazu-1])

for i in range(1,kazu):
    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(kazu-1):
        print(A[i],end=" ")
    print(A[kazu-1])

Submission status: Accepted

Solution code:
def insertion_sort(A, N):
    for i in range(1, N):
        v = A[i]
        j = i - 1
        print(*A)

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

    return A

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

A = insertion_sort(A, N)
print(*A)

Submission status: Accepted

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

def print_list(list):
	for i in range(len(list)-1):
		print(list[i]),
	print(list[len(list)-1])

#print("")
print_list(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_list(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 -= 1
    A[j+1] = v
    print(*A)

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:
import sys
readline = sys.stdin.readline
def insertionSort(n, 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(readline())
A = [int(j) for j in readline().split(" ")]
insertionSort(n, A)

Submission status: Accepted

Solution code:
def main():

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

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


main()

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int, input().split()))
for i in range(1,n+1):
    l[0:i]=sorted(l[0:i])
    print(*l)

Submission status: Accepted

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

Submission status: Accepted

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

def intersection(A,N):

    for i in range(len(A)): #output
        if i==len(A)-1:
            print(A[i])
        else:
            print(A[i], end=' ')

    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 i in range(len(A)):
            if i == len(A) - 1: #output
                print(A[i])
            else:
                print(A[i], end=' ')

intersection(A,num)

Submission status: Accepted

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

for i in range(len(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([str(i) for i in a]))

Submission status: Accepted

Solution code:
def main() :
    n = int(input())
    lst = [int(i) for i in input().split()]

    print(" ".join(map(str, lst)));

    for i in range(1,n) :
        for j in reversed(range(i+1)) :
            if j == 0 :
                break
            elif lst[j] < lst[j-1]:
                lst[j], lst[j-1] = lst[j-1], lst[j]
        print(" ".join(map(str, lst)))






if __name__ == '__main__' :
    main()

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:
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())
numbs = list(map(int,input().split()))
print(' '.join(map(str,numbs)))
for i in range(1,len(numbs)):
    key = numbs[i]
    j = i-1
    while j >=0 and numbs[j] > key:
        numbs[j+1] = numbs[j]
        j -= 1
    numbs[j+1] = key
    print(' '.join(map(str,numbs)))

Submission status: Accepted

Solution code:
def insertionSort(A, N):
    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
        t = str(A[0])
        for i in range(1,N):
            t = t + " " + str(A[i])
        print(t)


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

insertionSort(a,n)

Submission status: Accepted

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

def main():
	N = int(input())
	A = list(map(int, input().split()))
	insertionSort(A, N)
	print(' '.join(map(str, A)))

def insertionSort(A, N):
	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
			
if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
NN=raw_input()
N=raw_input()
N = [int(i) for i in N.split()]
print ' '.join([str(k) for k in N])

for i in range(1,int(NN)):
    key = N[i]
    j = i - 1
    while (j >= 0) and (N[j] > key):
        N[j+1] = N[j]
        j=j-1
    N[j+1] = key
    print ' '.join([str(k) for k in N])

Submission status: Accepted

Solution code:
def show_list(target):
    for i, item in enumerate(target):
        if i == len(target) - 1:
            print(item)
        else:
            print(item, end=" ")

def insection_sort(target):
    for i in range(1, len(target)):
        show_list(target)
        j = i - 1
        while j >= 0:
            if target[j] > target[j+1]:
                target[j], target[j+1] = target[j+1], target[j]
                j -= 1
            else:
                break
    return target

if __name__ == "__main__":
    n = input()
    a = [int(x) for x in input().split()]
    answer = insection_sort(a)
    show_list(answer)

Submission status: Accepted

Solution code:
n = int(input())
line = input()
arr = line.split(" ")
nums = []
for c in range(n):
    nums.append(int(arr[c]))

def trace(n_list):
    n_list_str = ""
    for num in n_list:
        n_list_str += str(num)
        n_list_str += " "
    print(n_list_str.rstrip(" "))
            
print(line)
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
    trace(nums)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

ERROR_INPUT = 'input is invalid'


def main():
    n = get_length()
    arr = get_array(length=n)
    print(*arr)

    insetionSort(li=arr, length=n)
    return 0


def get_length():
    n = int(input())
    if n < 0 or n > 100:
        print(ERROR_INPUT)
        sys.exit(1)
    else:
        return n


def get_array(length):
    nums = input().split(' ')
    return [str2int(string=n) for n in nums]


def str2int(string):
    n = int(string)
    if n < 0 or n > 1000:
        print(ERROR_INPUT)
        sys.exit(1)
    else:
        return n


def insetionSort(li, length):
    for i in range(1, length):
        v = li[i]
        j = i - 1
        while j >= 0 and li[j] > v:
            li[j + 1] = li[j]
            j -= 1

        li[j + 1] = v
        print(*li)


main()

Submission status: Accepted

Solution code:
import sys

ERROR_INPUT = 'input is invalid'


def main():
    n = get_length()
    arr = get_array(length=n)

    insetionSort(li=arr, length=n)
    return 0


def get_length():
    n = int(input())
    if n < 0 or n > 100:
        print(ERROR_INPUT)
        sys.exit(1)
    else:
        return n


def get_array(length):
    nums = input().split(' ')
    return [str2int(string=n) for n in nums]


def str2int(string):
    n = int(string)
    if n < 0 or n > 1000:
        print(ERROR_INPUT)
        sys.exit(1)
    else:
        return n


def insetionSort(li, length):
    for n in range(0, length):
        print(*(sorted(li[0:n + 1]) + li[n + 1:length]))


main()

Submission status: Accepted

Solution code:
# coding:utf-8

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
        for i in A:
            print i,
        else:
            print

N = input()
A = map(int, raw_input().split())
for i in A:
    print i,
else:
    print
insertionSort(A,N)

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())
a=list(map(int,input().split()))
for i in range(n):
 k=a[i]
 j=i-1
 while j>=0 and a[j]>k:
  a[j+1]=a[j]
  j-=1
 a[j+1]=k
 print(*a)

Submission status: Accepted

Solution code:
n = int(input())
A = [int(x) for x in input().split()]
print(" ".join([str(x) for x in 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([str(x) for x in A]))

Submission status: Accepted

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

def insertionSort(a,n):
    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(" ".join(map(str, a)))
        
insertionSort(a,n)

Submission status: Accepted

Solution code:
#coding: UTF-8

import sys

class Algo:
  @staticmethod
  def insersion_sort(A, N):
    for i in range(1, N):
      for k in A:
        if k==A[len(A)-1]:
          print(k)
        else:
          print(k, " ", sep="", end="")
      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 k in A:
      if k==A[len(A)-1]:
        print(k)
      else:
        print(k, " ", sep="", end="")

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

Submission status: Accepted

Solution code:
import sys
def print_list(A):
    for k in range(len(A)):
        sys.stdout.write(str(A[k]))
        if not k == len(A) -1: sys.stdout.write(" ")
    print()
def insert_sort(A,N):
    print_list(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_list(A)


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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
for i in range(1,N):
    print(*A)
    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(*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 = [int(i) for i in input().split()]
    print(*A)
    insertionSort(A,N)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i 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(*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(*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(*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)