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

Submission status: Accepted

Solution code:
def insertion_sort(A, N):
    for i in range(1, N):
        for s in A:
            tmp = str(s) if A[0] == s else " "+str(s)
            print(tmp, end="")
        print()

        j = i-1
        v = A[i]
        while v < A[j] and j >= 0:
            A[j+1] = A[j]
            j = j-1
        A[j+1] = v
    for s in A: 
        tmp = str(s) if A[0] == s else " "+str(s) 
        print(tmp, end="")
    print()
    
if __name__ == "__main__":
    N = int(input())
    A = [int(x) for x in input().split()]

    insertion_sort(A, N)

Submission status: Accepted

Solution code:
def insersion_sort(a):
    print(*a)
    n = len(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)

input()
insersion_sort([int(i) for i in input().split()])

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

Submission status: Accepted

Solution code:
N=int(input())
arr=list(map(int,input().split()))
print(' '.join(map(str,arr)))
for key in range(1,len(arr)):
    temp=arr[key]
    i=key-1
    while i>=0 and arr[i]>temp:
        arr[i+1]=arr[i]
        i-=1
    arr[i+1]=temp
    print(*arr)

Submission status: Accepted

Solution code:
import sys

a = ""
l = []
for input in sys.stdin:
    a += input
l = a.split()
b = int(l[0])
l = l[1:]
for i in range(0,b):
    key = int(l[i])
    j = i - 1
    while(j >= 0 and int(l[j]) > key):
        l[j+1] = l[j]
        j = j - 1
    l[j+1] = key
    if(b > 0):
        for data in l:
            print int(data),
        print
    b = b-1

Submission status: Accepted

Solution code:
def listprint(lists):
	global N
	count = 0
	for i in lists:
		count += 1
		if count < N:
			print(i, end = ' ')
		if count == N:
			print(i)

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

	for i in range(N):
		l[i] = int(l[i])

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

Submission status: Accepted

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

res = v.copy()

for i in range(n):
    if i == 0:
        pass
    else:
        for j in range(i):
            if res[j] > v[i]:
                res.pop(i)
                res.insert(j, v[i])
                break

    print(" ".join([str(x) for x in res]))

Submission status: Accepted

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

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
    
    return A

if __name__ == '__main__':

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

    A = insertionSort(A, N)
    print(" ".join(map(str, A)))

Submission status: Accepted

Solution code:
n=int(input())
s=input().split(' ')

print(' '.join(s))

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

Submission status: Accepted

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

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

Submission status: Accepted

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

def pri(lst):
    for i in range(len(lst)-1):
        print(lst[i],end=" ")
    print(lst[-1])

pri(lst)
for i in range(1,n):
    v = lst[i]
    j = i-1
    while True:
        if j < 0:
            lst[0] = v
            break
        if lst[j] > v:
            lst[j+1] = lst[j]
            j -= 1
        else:
            lst[j+1] = v
            break
    pri(lst)

Submission status: Accepted

Solution code:
n = int(input())
nums = []
nums = input().split()
intNums = list(map(int,nums))

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

insertationSort(nums,intNums)

Submission status: Accepted

Solution code:
count = int(input());
data = [int(n) for n in input().split(" ")];

def insert_sort(data):
    show(data);
    for i in range(1, len(data)):
        n = data[i];
        j = i - 1;
        while j >= 0 and data[j] > n:
            data[j + 1] = data[j];
            j -= 1;
        data[j + 1] = n;
        show(data);

def show(data):
    print(" ".join(str(n) for n in data));

insert_sort(data);

Submission status: Accepted

Solution code:
def trace(A,input_num):
    for i in range(input_num):
        if i != input_num - 1:
            print(A[i],end = " ")
        else:
            print(A[i])

def insertionSort(A,input_num):
    for i in range(input_num):
        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,input_num)
        
input_num = int(input())
A = list(map(int, input().split()))

insertionSort(A,input_num)

Submission status: Accepted

Solution code:
def insertionSort(A,input_num):
    for i in range(input_num):
        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)
        
input_num = int(input())
A = list(map(int, input().split()))

insertionSort(A,input_num)

Submission status: Accepted

Solution code:
# coding: UTF-8
def insertsort(A, N):
    print " ".join(A)
    A = map(int,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
        out_A = map(str,A)
        print " ".join(out_A)
    return A

n =  int(raw_input())
array = raw_input().split()
insertsort(array, n)

Submission status: Accepted

Solution code:
def insertSort(a):
    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(x) for x in a]))

if __name__ == '__main__':

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

    insertSort(a)

Submission status: Accepted

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

for i in range(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(map(str, A)))

Submission status: Accepted

Solution code:
# coding: UTF-8
def pprint(numlist):
  print(" ".join(map(str,numlist)))
N = int(input())
A = list(map(int, input().split()))
pprint(A)
for i in range(1,N):
  j = i
  v = A[i]
  while A[j] < A[j-1] and j >= 1:
    A[j] = A[j-1]
    A[j-1] = v
    j -= 1
  pprint(A)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
num = int(input())
data = list(map(int, input().split()))
for i in range(num):
    for j in range(i, 0, -1):
        if data[j] < data[j-1]:
            data[j-1], data[j] = data[j], data[j-1]
    print(' '.join(map(str, data)))

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 (' '.join([str(x) for x in A]))

if __name__ == '__main__':
    length = int(input())
    hoge = [int(x) for x in input().split()]
    print (' '.join([str(x) for x in hoge]))
    insertionSort(hoge, length)

Submission status: Accepted

Solution code:
def insertionsort(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,sep=" ")
    return A

N = int(input())
A = [int(i) for i in input().split()]
print(*A,sep=" ")
insertionsort(A)

Submission status: Accepted

Solution code:
n = int(input())
A = [int(i) for i in 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:
N = int(input())
A_str = input()
A = []
for s in A_str.split():
    A.append(int(s))

print(" ".join(map(str, A)))
for i in range(1, len(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:
N = int(input())
A = list(map(int, input().split()))

print(" ".join(map(str, A)))
for i in range(1, len(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:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys

def prt_list(A):
    s_list = list(map(str, A))
    print(' '.join(s_list))


def insert_sort(A):
    prt_list(A)
    for i in range(1, len(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
        prt_list(A)
    return

n = int(sys.stdin.readline())
a = list(map(int, sys.stdin.readline().split()))

insert_sort(a)

Submission status: Accepted

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

length = int(input())
array = [int(x) for x in input().split()]

for i in range(length):
    v = array[i]
    j = i-1
    while j >= 0 and array[j] > v:
        array[j+1] = array[j]
        j -= 1
    array[j+1] = v
    show(array)

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):
    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:
def insertion_sort(array):
    for i in range(len(array)):
        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)))
    return array

if __name__ == '__main__':
    n = int(input())
    array = list(map(int, input().split()))[0:n]
    insertion_sort(array)

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(' '.join(str(i) for i in a))


def main():
    n = int(input())
    a = [int(i) for i in input().split(' ')]
    print(' '.join(str(i) for i in a))
    insertion_sort(a, n)


main()

Submission status: Accepted

Solution code:
import sys

input_len = int(input())
l = input().split()
sort_line = [0 for i in range(input_len)]
for i in range(input_len):
	sort_line[i] = int(l[i])
	

for k in range(input_len):
	if (k > 0):
		sys.stdout.write(" ")
	sys.stdout.write(str(sort_line[k]))
print()

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

	for k in range(input_len):
		if (k > 0):
			sys.stdout.write(" ")
		sys.stdout.write(str(sort_line[k]))
	print()

Submission status: Accepted

Solution code:
def InsertionSort(A, N):
    ans = ' '.join(map(str, A)) + '\n'
    i = 1
    while i < 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
        ans += ' '.join(map(str, A)) + '\n'
        i += 1
    return ans[:-1]

n = int(input())
A = list(map(int, input().split(' ')))
ans = InsertionSort(A, n)
print(ans)

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 = j -1
        A[j+1] = v
        print(' '.join(map(str, A)))

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

Submission status: Accepted

Solution code:
def showlist(A):
    for i in range(len(A)):
        if i==len(A)-1:
            print(A[i])
        else:
            print(A[i], end=' ')

N = eval(input())
A = list(map(int, input().split()))
showlist(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
    showlist(A)

Submission status: Accepted

Solution code:
n = input()
nums = input()
nums = nums.split()
nums = list(map(int, nums))
print(' '.join(map(str, nums)))

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

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
print(' '.join(str(x) for x in a))
for i in range(1, N):
    j = i - 1
    b = a[i]
    while j >= 0 and b < a[j]:
        a[j+1] = a[j]
        j -= 1
    a[j+1] = b
    print(' '.join(str(x) for x in 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()))

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

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(map(str,A)))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

y = list(map(str, array))
y = " ".join(y)
print(y)

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
    x = list(map(str, array))
    x = " ".join(x)
    print(x)

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 insertion_sort(A):
    for i in range(1, len(A)):
        v = A[i]
        j = i - 1
        print(*A)
        while j >= 0 and A[j] > v:
            A[j+1] = A[j]
            A[j] = v
            j -= 1
    return A

n = input()
A = [int(x) for x in input().split()]
print(*insertion_sort(A))

Submission status: Accepted

Solution code:
def insertion_sort(alist):
    """sort alist by insertion sort.
    print the intermediate sequence for each step.

    >>> insertion_sort([5, 2, 4, 6, 1, 3])
    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
    [1, 2, 3, 4, 5, 6]
    """
    for i, key in enumerate(alist):
        for j in reversed(range(i)):
            if alist[j] <= key:
                alist[j+1] = key
                break
            else:
                alist[j+1] = alist[j]
        else:
            alist[0] = key
        print(" ".join([str(i) for i in alist]))

    return alist


def run():
    _ = int(input())  # flake8: noqa
    nums = [int(i) for i in input().split()]

    insertion_sort(nums)


if __name__ == '__main__':
    run()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
num = int(input())
cards = [int(i) for i in input().split()]

def insertion_sort(num, cards):
	for i in range(num):
		v = cards[i]
		j = i - 1
		while j >= 0 and cards[j] > v:
			cards[j+1] = cards[j]
			j -= 1
		cards[j+1] = v
		output(cards)

def output(cards):
	sorting = [str(i) for i in cards]
	print(" ".join(sorting))

insertion_sort(num, cards)

Submission status: Accepted

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

def insertion_sort(A):
	for i in range(1,len(A)):
		for elem in A[:-1]:
			print(elem, end=' ')
		print(A[-1])
		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 elem in A[:-1]:
		print(elem, end=' ')
	print(A[-1])

insertion_sort(A)

Submission status: Accepted

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

for x in range(1, len(s)):
    print(*s, sep=' ')
    stash = s[x]
    if stash < s[x-1]:
        y = x
        while True:
            s[y] = s[y-1]
            y -= 1
            if y == 0 or stash > s[y-1]:
                break
        s[y] = stash
print(*s, sep=' ')

Submission status: Accepted

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

"""
1 insertionSort(A, N) // N個の要素を含む0-オリジンの配列A
2   for i が 1 から N-1 まで
3     v = A[i]
4     j = i - 1
5     while j >= 0 かつ A[j] > v
6       A[j+1] = A[j]
7       j--
8     A[j+1] = v
"""
mm = ''
for i in range(n):
    mm = mm + ' ' + str(l[i])
print(mm.strip())

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

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 a[j]>v and j>=0:
        a[j+1] = a[j]
        j -=1

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

Submission status: Accepted

Solution code:
# 16D8101022L 小林勇希 ID:k_mb python


def insertion_sort(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 = j-1
        A[j+1] = v
        for i in range(n-1):
            print(A[i],"",end="")
        print(A[n-1])

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

insertion_sort(A_idx,n)

Submission status: Accepted

Solution code:
def insertionSort(a,n):
    for i in range(1,n):
        for k in range(n):
            print(a[k],end='')
            if k==n-1:
                print()
            else:
                print(' ',end='')

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

n=int(input())
r=input()
data=r.split(' ')
for i in range(n):
    data[i]=int(data[i])
insertionSort(data,n)

Submission status: Accepted

Solution code:
def pr(a):
    for i in range(len(a)):
        print(a[i],end="")
        if i == len(a)-1:
            print("")
        else :
            print(" ",end="")
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
        pr(a)

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

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

def prt(A):
    for i in A:
        if i != A[len(A)-1]: print(i, end=" ")
        else: print(i)

n = int(input())
A = input().split()
A = [int(i) for i in A]
prt(A)
insertionsort(A, n)

Submission status: Accepted

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

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
        pr(A,n)

n=int(input())
A=input().split()
A=[int(i)for i in A]
pr(A,n)
insertionSort(A,n)

Submission status: Accepted

Solution code:
N=int(input())
line=input().split()
A=[0]*N
for i in range(0,N):
    A[i]=int(line[i])
for i in range(N):
    """
    for k in range(N):
        print(A[k],end="")
        if k==N-1:
            print()
        else:
            print(" ",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 range(N):
        print(A[k],end="")
        if k==N-1:
            print()
        else:
            print(" ",end="")

Submission status: Accepted

Solution code:
# 16D8101022L 小林勇希 ID:k_mb python3


def insertion_sort(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 = j-1
        A[j+1] = v
        for i in range(n-1):
            print(A[i],"",end="")
        print(A[n-1])

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

insertion_sort(A_idx,n)

"""実行結果

6
4 6 3 2 1 5
4 6 3 2 1 5
4 6 3 2 1 5
3 4 6 2 1 5
2 3 4 6 1 5
1 2 3 4 6 5
1 2 3 4 5 6

"""

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 k in range(N):
        if k<N-1:
            print(A[k],end=" ")
        if k==N-1:
            print(A[k])

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 k in range(N):
        if k<N-1:
            print(A[k],end=" ")
        if k==N-1:
            print(A[k])

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 k in range(N):
        if k<N-1:
            print(A[k],end=" ")
        if k==N-1:
            print(A[k])

Submission status: Accepted

Solution code:
#16D8101012J 伊藤惇　dj Python3

def Insertion_Sort():
    N=int(input())
    A=list(map(int,input().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=j-1
        A[j+1]=v
        
        print(A[0],end="")
        for i in range(1,N):
            print(" {}".format(A[i]),end="")
        print('\n',end="")
        


if __name__ == "__main__":
    Insertion_Sort()

Submission status: Accepted

Solution code:
#16D8101014F 久留米 竜之介 Utyan0604 python3
def insertionsort(n,A):
	for i in range(n):
		t=A[i]
		j=i-1
		while j>=0 and A[j] > t:
			A[j+1]=A[j]
			j-=1
			A[j+1]=t
			
			
	
		for k in range(n):
			if k<n-1:
				print(A[k],end=" ")
			else:
				print(A[k])
			
n=int(input())
A=list(map(int, input().split()))

insertionsort(n,A)

Submission status: Accepted

Solution code:
#16D8101012J 伊藤惇　dj Python3

def Insertion_Sort(N,A,flag=0):
    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=j-1
        A[j+1]=v
        if flag==1:
            print(A[0],end="")
            for i in range(1,N):
                print(" {}".format(A[i]),end="")
            print('\n',end="")
    return A    


if __name__ == "__main__":
    N=int(input())
    A=list(map(int,input().split()))
    Insertion_Sort(N,A,1)


"""
(py361) [16D8101012J@ise72c ~]$ python ALDS1_1_A.py
10
10 9 8 7 6 5 4 3 2 1
10 9 8 7 6 5 4 3 2 1
9 10 8 7 6 5 4 3 2 1
8 9 10 7 6 5 4 3 2 1
7 8 9 10 6 5 4 3 2 1
6 7 8 9 10 5 4 3 2 1
5 6 7 8 9 10 4 3 2 1
4 5 6 7 8 9 10 3 2 1
3 4 5 6 7 8 9 10 2 1
2 3 4 5 6 7 8 9 10 1
1 2 3 4 5 6 7 8 9 10
"""

Submission status: Accepted

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



for i in range(len(l)):
    v=l[i]
    j=i-1

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

    print(*l)

Submission status: Accepted

Solution code:
# リストの宣言
A = []

# 入力の最初の行に、数列の長さを表す整数 N が与えられます。
# 2 行目に、N 個の整数が空白区切りで与えられます。
NUM =int(input())
A = input().split()

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

NUMLIST = range (1, NUM)

# for i = 1 to A.length-1
for i in NUMLIST:
#    key = A[i]
  key = A[i]
  j = i - 1
  while j >= 0 and int(A[j]) > int(key):
    A[j+1] = A[j]
    j-=1
  A[j+1] = key
  print(" ".join(map(str, A)))

Submission status: Accepted

Solution code:
def insertion_sort(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 = j-1
        A[j+1] = v
        for i in range(n-1):
            print(A[i],"",end="")
        print(A[n-1])

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

insertion_sort(A_idx,n)

Submission status: Accepted

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

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


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

Submission status: Accepted

Solution code:
import copy


def parse_stdin(input_lines: int):
    data_list = []
    for i in range(input_lines):
        data_list.append(input().rstrip().split())
    return data_list


def insertion_sort_with_print(values):
    print_now_state(values)
    for i, value in enumerate(values[1:]):
        i += 1
        j = i - 1
        while j >= 0 and values[j] > value:
            values[j + 1] = copy.copy(values[j])
            j -= 1
        values[j + 1] = copy.copy(value)
        print_now_state(values)


def print_now_state(values):
    for val_print in values[:-1]:
        print("{}".format(val_print), end=' ')
    print("{}".format(values[-1]), end='')
    print()


def main():
    input()
    input_lists = parse_stdin(1)
    values = list(map(int, input_lists[0]))
    insertion_sort_with_print(values)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def Isort(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)
        
    
n = int(input())
a = list(map(int, input().split()))
print(*a)
Isort(a,n)