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())
li = list(map(int, input().split()))

# N = 6
# li = [5, 2, 4, 6, 1, 3]

for i in range(1, N):
    print(*li)
    v = li[i]
    for j in reversed(range(0, i)):
        if li[j] > v:
            li[j + 1] = li[j]
            li[j] = v
        else:
            break

print(*li)

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()))
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 insertSort(n, a):
  for i in range(n):
    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)

insertSort(int(input()), [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):
    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()))
for i in range(1, N):
    print(*A)
    for j in range(i):
        if A[j] > A[i]:
            break
    else:
        continue
    tmp = A[i]
    for k in range(i, j, -1):
        A[k] = A[k - 1]
    A[j] = tmp
print(*A)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

def insertion(a:list, n:int) -> None:
    if not isinstance(a, list):
        return None

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

insertion(a, n)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

def print_vec(vec):
    print(" ".join(map(str, vec)))

N = int(input())
A = list(map(int, input().split()))
for i in range (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_vec(A)

Submission status: Accepted

Solution code:
N = int(input())

nums = list(map(int, input().split()))

for i in range(len(nums)):
    num = nums[i]
    j = i - 1
    while j >= 0 and nums[j] > num:
        nums[j + 1] = nums[j]
        j -= 1
    nums[j + 1] = num

    print(*nums)

Submission status: Accepted

Solution code:
N = int(input())
data = list(map(int, input().split()))
    
for i in range(N):
    tmp = data[i]
    j = i - 1
    while j >= 0 and data[j] > tmp:
        data[j+1] = data[j]
        j -= 1
    data[j+1] = tmp
    
    for j in range(N):
        print("%d" %(data[j]), end="")
        if j != N-1:
            print(" ", end="")
    print()

Submission status: Accepted

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

print(*a)
for i in range(1,n):
    for j in range(i,0,-1):
        if a[j]<a[j-1]:
            a[j], a[j-1] = a[j-1], a[j]
    print(*a)

Submission status: Accepted

Solution code:
def insertion_sort(A,N):
	prin(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
		prin(A)
def prin(A):
	print(' '.join(list(map(str, A))))
N = int(input())
A = [int(i) for i in input().split(' ')]
insertion_sort(A,N)

Submission status: Accepted

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

#%%
def insertionSort(A, N):
    for i in range(1, N):
        v = A[i]    # これからソートしたい1つの数字（i番目）
        j = i - 1   # ソート済み(i-1番目まで)のインデックス
        print(' '.join(map(str, A)))

        # j以下のものを順番に右にずらしていきながら、挿入する値のインデックスを決める
        while j >= 0 and A[j] > v:
            A[j+1] = A[j] # 値を上書きしていく（右にずらしていく処理）
            j -= 1  # jがi-1を始点として、先頭に向かって減少する
        A[j+1] = v # いい感じのところで[..., 3, 3, ...]みたいになるので、左側を上書きする
    
    return A

a = insertionSort(A[:], N)
print(' '.join(map(str, a)))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
num_list = [int(item) for item in input().split()]

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)

insertionSort(num_list, N)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def insertsort(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)

insertsort(A,N)

Submission status: Accepted

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

print(" ".join([str(i) for i in a]))
for i in range(1, n, 1):
    for j in range(i):
        if a[i] < a[j]:
            a.insert(j, a[i])
            a.pop(i+1)
    print(" ".join([str(i) for i in a]))

Submission status: Accepted

Solution code:
N, *A = map(int, open(0).read().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:
n = int(input())
A = [int(s) for s 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:
import sys

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

insertionSort(A, 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([str(i) for i in A]))

Submission status: Accepted

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

Submission status: Accepted

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

    insertionSort(A, N)


def insertionSort(A, N):
    print(*A)

    for i in range(1, N):
        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)


main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
A=list(int(x) for x 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
    for i in range(N-1):
        print(A[i],end=" ")
    print(A[N-1])

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# insertionSort(A, N) // N個の要素を含む0-オリジンの配列A
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)

N = int(input())
A = list(map(int, input().split())) # 「1 2 3…」のような配列入力
# print(*A) # リストの出力を数字とスペースのみにしてくれる
InsertionSort(A, N)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
print(' '.join([str(a) for a in A]))

for i in range(1,N):
    key = A[i]
    j = i-1
    # j=0で先頭まで来たらもう比較しない
    while j>=0 and A[j] > key:
        # より小さい項と比較する
        A[j+1] = A[j]
        
        j -= 1
    # keyの位置は確定したので代入(while抜ける前にj-=1されているのでA[j+1])
    A[j+1] = key
    print(' '.join([str(a) for a in A]))

Submission status: Accepted

Solution code:
num = int(input())
an = [ int(i) for i in input().split()]
print(" ".join([str(n) for n in an]))

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

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

Submission status: Accepted

Solution code:
N = int(input())
A = 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 A[j] > v and j >= 0:
        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 InsertionSort(seq):
    print(*seq)
    for i in range(len(seq) - 1):
        j = i

        while j >= 0:
            if seq[j + 1] < seq[j]:
                seq[j + 1], seq[j] = seq[j], seq[j + 1]
                
            j -= 1 

        print(*seq)

def main():
    n = int(input())
    seq = [int(a) for a in input().split()]
    InsertionSort(seq)

main()

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

#入力dataの挿入
input_N = int(input())
input_A = list(map(int, input().split()))
print(" ".join(map(str, input_A)))

#処理の実行
insertionSort(input_A, input_N)

Submission status: Accepted

Solution code:
# Algorithm and Data Structures 1
# Name: Ryuya Asada
# ID: s1260064


def InsertionSort(A: list):
    for i in range(1, len(A)):
        key = A[i]
        j = i - 1
        print(*A, sep=" ")
        while (j >= 0 and A[j] > key):
            A[j+1] = A[j]
            j -= 1
        A[j+1] = key


def main():
    length = int(input())
    A = [int(e) for e in input().split(" ")]

    InsertionSort(A)
    print(*A, sep=" ")


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# 入力
n = int(input().rstrip())
data = list(map(int,input().split()))

# ソート
for i in range(1, n):
    for s in range(n):
        if s != n-1:
            print(data[s], end=" ")
        else:
            print(data[s])
    min = i
    j = i-1
    while j >= 0:
        if data[min] < data[j]:
            tmp = data[j]
            data[j] = data[min]
            data[min] = tmp
            min = j
        j -= 1

for s in range(n):
    if s != n - 1:
        print(data[s], end=" ")
    else:
        print(data[s])

Submission status: Accepted

Solution code:
def show(A):
    N = len(A)
    for i in range(N):
        if i :
            print(' ', end = '')
        print(A[i], end = '')
    print()

def insertionSort(A, N):
    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] 
            j = j - 1
        A[j+1] = v
        show(A)

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

insertionSort(A, N)

Submission status: Accepted

Solution code:
def printData(data) :
    for i, value in enumerate(data) :
        if i > 0 :
            print(" ", end = "")
        print(value, end = "")
    print()
    
def insertionSort(data, N) :
    for i in range(1, N) :
        target = data[i]
        j = i -1
        while j >= 0 and target < data[j] :
            data[j + 1] = data[j]
            j -= 1
        data[j + 1] = target
        printData(data)
        
N = int(input())
data = [int(char) for char in input().split()]
printData(data)
insertionSort(data, N)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

n = int(input())
numbers = input().split()

numbers = [int(num) for num in numbers]

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

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

    numbers[j+1] = key
show(numbers)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def insertionSort(A, N): # N個の要素を含む0-オリジンの配列A
    for i in range(1, N):
        print(' '.join([str(p) for p 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
    print(' '.join([str(p) for p in A]))
        
n = int(input())
a_list = [int(i) for i in input().split()]
insertionSort(a_list, n)

Submission status: Accepted

Solution code:
#insertion sort

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

print(*A)

for i in range(1,n):
    j = i
    while(j > 0):
        if A[j-1] > A[j]:
            A[j-1],A[j] = A[j],A[j-1] #SWAP
            j -= 1
        else:
            break
    
    print(*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 = 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:

def InsertionSort(A):
    i = 0
    j = 0
    print(' '.join([str(x) for x in A]))
    for i in range(1, N):
        movenum = A[i]
        j = i - 1
        while j >= 0 and movenum < A[j] : 
            
            A[j+1] = A[j]
        
            j -= 1

        A[j + 1] = movenum
        print(' '.join([str(x) for x in A]))

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
li = list(map(int,input().split()))
print(*li)

for i in range(1,N):
    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)

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 = list(map(int, input().split()))
s = ''
for a in A:
    s += str(a) + ' '
print(s.rstrip())

for i in range(1, N):
    for j in range(0, i):
        if A[i] < A[j]:
            A.insert(j, A.pop(i))
            break
    s = ''
    for a in A:
        s += str(a) + ' '
    print(s.rstrip())

Submission status: Accepted

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

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

insertion_sort(a=a, n=n)


"""
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
"""

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
for i, a in enumerate(A):
    temp = a
    i -= 1
    while i >= 0 and A[i] >temp:
        A[i+1] = A[i]
        A[i] = temp
        i -= 1
    L=[str(a) for a in A]   
    p = ' '.join(L)
    print(p)

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
        for j in range(N):
            if j == N-1:
                print(A[j])
            else:
                print(A[j], end=' ')
    return(A)

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

InsertionSort(A,N)

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def insertingSort(a, n):
    for i in range(1, 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(list(map(str, a))))        

n = int(input().strip())
a = list(map(int, input().strip().split()))
print(' '.join(list(map(str, a))))
insertingSort(a, n)

Submission status: Accepted

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

Submission status: Accepted

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

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

pr = str(a[0])
for i in range(n-1):
    i = i + 1
    pr += " " + str(a[i])
print(pr)

insertion_sort(a, n)

Submission status: Accepted

Solution code:
N = int(input())
lst = list(map(int, input().split()))
print(" ".join(list(map(str, lst))))
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 -= 1
    lst[j+1] = v
    print(" ".join(list(map(str, lst))))

Submission status: Accepted

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

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

def f(a, n):
  for i in range(n):
    v = a[i]
    j = i-1
    while (a[j] > v) & (j>=0):
      a[j+1] = a[j]
      j-=1
    a[j+1] = v
    p(a, n)
    print()
f(a, n)

Submission status: Accepted

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

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

insertSort(A,N)

Submission status: Accepted

Solution code:
from sys import stdin
#stdin = open("input.txt")

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)

if __name__ == "__main__":
    N = int(stdin.readline())
    A = [int(item) for item in stdin.readline().split()]
    
    insertionSort(A, N)

Submission status: Accepted

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


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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i, a in enumerate(A):
    j = i - 1
    while j >= 0 and a < A[j]:
        A[j + 1] = A[j]
        j -= 1
    else:
        A[j + 1] = a
        print(*A)

Submission status: Accepted

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

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

    print(*a)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def insert_sort(A):
    a = list(A)
    print(' '.join(map(str, a)))
    for move_index in range(1, len(a)):
        compare_index = move_index -1
        while compare_index >= 0 and a[compare_index] > a[move_index]:
            a[compare_index], a[move_index] = int(a[move_index]), int(a[compare_index])
            compare_index -= 1
            move_index -= 1
        print(' '.join(map(str, a)))
    return a

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i 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:
def InsertionSort(a:list, n:int):
    for i in range(0,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
        print(" ".join(map(str,a)))
    return a

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


if __name__ == "__main__":
    main()

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def print_list(a):
    out = ''
    for ai in a:
        out += str(ai) + ' '
    else:
        out = out[:-1]
    print(out)

def insertion_sort(n, a):
    for i in range(1, n):
        print_list(a)
        v = a[i]
        sorted_list_idx = i
        while sorted_list_idx > 0 and a[sorted_list_idx - 1] > v:
            a[sorted_list_idx] = a[sorted_list_idx - 1]
            sorted_list_idx -= 1
        a[sorted_list_idx] = v
    return a


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

a = insertion_sort(n, a)

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


n = int(input())
l = list(map(int, input().split()))
print(*l)
insertionSort(l, n)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

print(*working_list)

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

Submission status: Accepted

Solution code:
n = input()
b = int(n) - 1

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

#calc func
for j in range(int(n)):
	if j != 0:
		for c2 in reversed(range(0,j)):
			if nlist[c2] > nlist[j]:
				stk = nlist[c2]
				nlist[c2] = nlist[j]
				nlist[j] = stk
				j = j - 1
				c2 = j - 1

#print func
	for i in range(int(b)):
		print(nlist[i], end=" ")

	print(nlist[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 = j - 1
        A[j+1] = v
        for j in range(0,N-1):
            print("{0} ".format(A[j]),end = '')
        print(A[N-1])


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

for i in range(0,N-1):
    print("{0} ".format(A[i]),end = '')
print(A[N-1])

InsertionSort(A,N)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def main():
    
    n = int(input())
    a = input().split()
    
    A = []
    for i in range(n):
        A.append(int(a[i]))
        
    display(A)
    insertSort(A)
        
def insertSort(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
        display(A)
            
def display(A):
    for i in range(len(A)):
        if i != len(A) - 1:
            endStr = ' '
        else:
            endStr = '\n'
        print(str(A[i]), end=endStr)
                
if __name__ == "__main__":
    main()

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 = [str(n) for n in a]
    print(" ".join(b))