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 printTable(table):
    for i in range(len(table)):
        print(table[i], end='')
        if i != len(table)-1:
            print(' ', end='')
    print()


def insertionSort(table):
    for i in range(1, len(table)):
        min = table[i]
        j = i - 1
        while j >= 0 and min < table[j]:
            table[j + 1] = table[j]
            table[j] = min
            j += -1
        printTable(table)


number = int(input())
table = list(map(int, input().split()))
printTable(table)
insertionSort(table)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin

def insertionSort(A, N):  #N個の要素を含む0−オリジンの配列
  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(stdin.readline().rstrip())
A = list(map(int, stdin.readline().split()))

insertionSort(A, N)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

N = int(input())
arr = list(map(int,input().split()))
#print(N,arr)
print(" ".join(str(x) for x in arr))
def insertionSort(N,arr):
    for i in range(1,N):
        v = arr[i]
        j = i - 1
        while j >= 0 and arr[j] > v:
            arr[j+1] = arr[j]
            j -= 1
        arr[j+1] = v
        
        #arr = " ".join(arr)
        print(" ".join(str(x) for x in arr))
    return arr
    

        
ans = insertionSort(N,arr)

Submission status: Accepted

Solution code:
n = input()
A = list(map(int, input().split()))
print(*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(*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):
    j = i
    while j >= 1 and A[j] < A[j-1]:
        A[j-1], A[j] = A[j], A[j-1]
        j -= 1
    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)
  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:
#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
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())
A = list(map(int, input().split()))
print(*A)
insertionSort(A, N)

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:
def insertionSort(A, N):
    print(' '.join(map(str, A)))
    for i in range(1, N):
        value = A[i]
        j = i - 1
        while j >= 0 and A[j] > value:
            A[j + 1] = A[j]
            j -= 1
        A[j + 1] = value
        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())
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(*A,sep=" ")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
cards = list(map(int,input().split()))
print(" ".join(map(str,cards)))

for i in range(1,N):
    tmp = cards[i]
    j = i - 1
    while j >= 0 and tmp < cards[j]:
        cards[j+1] = cards[j]
        cards[j] = tmp
        j -= 1
    print(" ".join(map(str,cards)))

Submission status: Accepted

Solution code:
N = int(input())
array = list(map(int,input().split()))
for dt in array:
    if dt==array[-1]:
        print(dt)
    else:print(dt,end=" ")
for n in range(1,N):
    for m in range(n-1,-1,-1):
        if array[m]>array[m+1]:
            tmp=array[m]
            array[m]=array[m+1]
            array[m+1]=tmp
    for dt in array:
        if dt==array[-1]:
            print(dt)
        else:print(dt,end=" ")

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

def print_space(li):
    for i in range(len(li)-1):
        print(li[i], end=' ')
    else:
        print(li[-1])

def InsertionSort(li):
    print_space(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
        else:
            li[j+1] = v
        print_space(li)
            

InsertionSort(a)

Submission status: Accepted

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

def insertion_sort(n,a):
    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)))
    return a
    
if __name__ == "__main__":
    n = int(input())
    a = list(map(int,input().split()))
    insertion_sort(n,a)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def show(A):
    output = ""
    for i in range(len(A)):
        output += str(A[i]) + " "
    print(output[:-1])

def insertionsort(A, N):
    for i in range(1, N):
        show(A)
        A[:i+1] = sorted(A[:i+1])
    show(A)
    
N = int(input())
A = list(map(int, input().split())) 

insertionsort(A, N)

Submission status: Accepted

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

insertionSort(int(input()),list(map(int,input().split())))

Submission status: Accepted

Solution code:
def print_list(A):
    for x in range(len(A) - 1):
        print(A[x],end = " ")
    print(A[len(A) - 1])

def insertionsort(A,n):
    count = 0
    for i in range(1,n):
        print_list(A)
        right = i
        left = i - 1
        while A[left] > A[right] and left >= 0:
            A[left],A[right] = A[right],A[left]
            right = left
            left -= 1
            count += 1
    print_list(A)
    return A

if __name__ == '__main__':
    n = int(input())
    A = [int(i) for i in input().split()]
    A = insertionsort(A,n)

Submission status: Accepted

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

N = int(input())
input_list = input().split(' ')
A = []
for i in input_list:
    A.append(int(i))
if len(input_list) != N:
    raise ValueError('N個の整数を入力してください。')

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

Submission status: Accepted

Solution code:
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_1_A

from sys import stdin
input = lambda: stdin.readline().rstrip()

N = int(input())

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

join = lambda a : ' '.join(map(str, a))
print(join(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(A))

Submission status: Accepted

Solution code:
def insertion_sort(A,N):
    sorted_A = A.copy()
    print(*sorted_A)
    for i in range(1,N):
        v = sorted_A[i]
        j = i-1
        while (j>=0)&(sorted_A[j]>v):
            sorted_A[j+1] = sorted_A[j]
            j -= 1
        sorted_A[j+1] = v
        print(*sorted_A)
        

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

insertion_sort(A,N)

Submission status: Accepted

Solution code:
# ALDS1_1_A.py
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([str(_) for _ in a]))
N = int(input())
A = list(map(int,input().split()))
insertionSort(A,N)

Submission status: Accepted

Solution code:
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy
import bisect
import functools

def InsertionSort(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(*A)
n = int(input())
A = list(map(int,input().split()))
print(*A)
InsertionSort(A)

Submission status: Accepted

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


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


resolve()

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

    for j in range(n):
        if j == n - 1 :
            print(A[j])
        else:
            print(str(A[j]) + " ", end="")

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
size = int(input())
l = []
for i in map(int, input().split()):
    l.append(i)
print(' '.join(map(str,l)))

for i in range(1,size):
    tmp = l[i]
    j = i - 1

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

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 insertion_sort(A: list, N: int):
    print(*A, sep=" ")
    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=" ")


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

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**10
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N = I()
    A = LI()

    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)

if __name__ == '__main__':
    resolve()

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())
numlist = [int(x) for x in input().split()]

insertionSort(numlist,n)

Submission status: Accepted

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

tmp=0
for i in range(n):
    j=i
    tmp=a[i]
    while a[j-1]>tmp and j>0:
        a[j]=a[j-1]
        j-=1
    a[j]=tmp
    print(*a)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def insertion_sort(seq):
    n = len(seq)
    for i in range(n):
        v = seq[i]
        j = i-1
        while j >= 0 and seq[j] > v:
            seq[j+1] = seq[j] #vの入る候補がindexのi-1
            j -= 1
        seq[j+1] = v
        print(seq[0], end='')
        for k in range(1, n):
            print(' '+str(seq[k]), end='')
        print()

n = int(input())
seq = list(map(int, input().split()))
insertion_sort(seq)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def show(lst):
    length = len(lst)
    for i in range(length-1):
        print(lst[i], end=" ")
    print(lst[-1])
        
    

n = int(input())
lst = list(map(int, input().split()))
def insertion_sort(lst, n):
    for i in range(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
        show(lst)
insertion_sort(lst, 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(map(str,A)))

Submission status: Accepted

Solution code:
n = int(input())

A = [int(x) for x 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 = [int(i) for i in input().split() ]
for j in range(len(a)):
  if j == len(a) -1:
    print(a[j],end = "")
  else:
    print(a[j],end = " ")    
print()
for i in range(1,len(a)):
  tmp = a[i]
  j = i- 1
  while j >= 0 and a[j] > tmp:
    a[j+1] = a[j]
    j -= 1
  a[j+1] = tmp
  for j in range(len(a)):
      if  j == len(a)-1 :
        print(a[j],end = "")
      else:
        print(a[j],end = " ")
  print()

Submission status: Accepted

Solution code:
#โชว์การ sort ในแต่ละบรรทัด
def show (seqs):
    for i in range(len(seqs)):
        if i!=len(seqs)-1:
            print(seqs[i],end=' ')
        else :
            print(seqs[i])

num = int(input())
seqs = list(map(int,input().split()))

show(seqs) #โชว์ข้อมูลที่รับ input มา

for i in range(1,num):#วน loop เริ่มจาก 1 จนถึงจำนวนข้อมูล
    element = seqs[i] #ให้ element = ข้อมูลในตำแหน่งที่ i ซึ่ง iเป็นได้ 0ถึงจำนวนข้อมูล-1
    j = i-1 
    while (j>=0 and seqs[j]>element): #ถ้าข้อมูลตัวด้านซ้ายมีค่ามากกว่าข้อมูลที่อยู่ด้านขวา
        seqs[j+1] = seqs[j] # swap ข้อมูลที่น้อยที่สุดไปยังด้านซ้ายสุดเสมอ
        j -=1
    #ถ้าหลุด loop while ทำการเปลี่ยนขa้อมูลให้เป็นข้อมูลของตัวที่ได้สลับไปก่อนหน้านี้    
    seqs[j+1]=element
    show(seqs)

Submission status: Accepted

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

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

    print(*s)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:

import sys
sys.setrecursionlimit(10 ** 7)
read = sys.stdin.buffer.read
inp = sys.stdin.buffer.readline
def inpS(): return inp().rstrip().decode()
readlines = sys.stdin.buffer.readlines
MOD = 10 ** 9 + 7
INF = 1 << 60

# ---------------------------------------------------
# ---------------------------------------------------
# ---------------------------------------------------

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


if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

Submission status: Accepted

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

def insertion_sort(A):
    for idx, target in enumerate(A[1:]):
        j = idx
        while j >= 0 and A[j] > target:
            A[j+1] = A[j]
            j -= 1
        A[j+1] = target
        disp(A)

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

disp(A)
insertion_sort(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] = A[j],A[j+1]
            # print(f'i={i} j={j}')
            j -= 1
        print(*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(*a)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def insert_sort(n:int, s:list):
    print(' '.join(map(str, s)))
    for i in range(1, len(s)):
        j = i - 1
        v = s[i]
        while (j >= 0 and s[j] > v):
            s[j + 1] = s[j]
            j -= 1
            s[j + 1] = v
        print(" ".join(map(str, s)))

insert_sort(int(input()),list(map(int,input().split(' '))))

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


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

insertionSort(A, N)

Submission status: Accepted

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


num = int(input())
s = list(map(int,input().split()))
Insertion_Sort(s,num)

Submission status: Accepted

Solution code:
import sys
import math
import itertools

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)

def main():
  N = int(input())
  numbers = list(map(int, input().split()))

  insertionSort(numbers, N)

if __name__ == '__main__':
  main()

Submission status: Accepted

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

for i in range(1, N+1):
    for j in reversed(range(1, i)):
        if A[j] < A[j-1]:
            tmp = A[j]
            A[j] = A[j-1]
            A[j-1] = tmp
        else:
            break

    result = [str(k) for k in A]
    print(' '.join(result))

Submission status: Accepted

Solution code:
from sys import stdin

N = int(stdin.readline())
A = list(map(int, stdin.readline().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 show (seqs):
    for i in range(len(seqs)):
        if i != len(seqs)-1:
            print(seqs[i], end = ' ')
        else :
            print(seqs[i])

num = int(input())
seqs = list(map(int,input().split()))

show(seqs)
for i in range(1,num):
    element = seqs[i]
    j = i-1
    while (j>=0 and seqs[j]>element):
        seqs[j+1] = seqs[j]
        j -=1
    seqs[j+1] = element
    show(seqs)

Submission status: Accepted

Solution code:
n = int(input())  # 6
a = list(map(int, input().split()))  # [1, 4, 3, 6, 2, 5]
print(' '.join([str(n) for n in a]))

for i in range(1, len(a)):
	j = a[i]
	for k in range(i - 1, -1, -1):
		if a[k] > j:
			a[k + 1] = a[k]
			a[k] = j
	print(' '.join([str(n) for n in a]))

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
        b = 0
        for i in A:
            b +=1
            if b == N:
                print(i, end ="")
            else:
                print(i, end =" ")
        print()
n = int(input())
a = [int(i) for i in input().split()]
insertionSort(a,n)

Submission status: Accepted

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

c = 1
for s in a:
    if c != n:
        print("{} ".format(s), end="")
    elif c == n:
        print(s)
    c += 1

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

    c = 1
    for s in a:
        if c != n:
            print("{} ".format(s), end="")
        elif c == n:
            print(s)
        c += 1

    i += 1

Submission status: Accepted

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

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

Submission status: Accepted

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

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
    s = [str(a) for a in A]
    s = ' '.join(s)
    print(s)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
[print(A[x],'',end='') for x in range(N-1)]
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-=1
    A[j+1]=v
    [print(A[x],'',end='') for x in range(N-1)]
    print(A[N-1])

Submission status: Accepted

Solution code:
def insort(n,A):
    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
        L=[str(a) for a in A]
        L=" ".join(L)
        print(L)

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

insort(n,A)

Submission status: Accepted

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

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

#insertion_sort(6, [5, 2, 4, 6, 1, 3])
main()

Submission status: Accepted

Solution code:
N = int(input())
aa = list(map(int, input().split()))
for i in range(N):
    tmpl = sorted(aa[:i+1]) + aa[i+1:]
    print(" ".join(map(str,tmpl)))

Submission status: Accepted

Solution code:
# https://onlinejudge.u-aizu.ac.jp/problems/ALDS1_1_A
import sys
from functools import reduce


def to_str(x, y):
    return str(x) + ' ' + str(y)


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

print(reduce(to_str, 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(reduce(to_str, A))

# A[i}がどこに入ればよいかという観点

Submission status: Accepted

Solution code:

n = int(input())
L = list(map(int,input().split()))
print(' '.join(map(str, L)))

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def printArray(A):
  strA = [str(i) for i in A]
  print(" ".join(strA))

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

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

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(1,num):
    for n, sl in enumerate(sort_list):
        if n != num - 1:
            print('{} '.format(sl),end='')
        else:
            print('{}'.format(sl))

    tmp = sort_list[i]
    j = i -1
    while j >= 0 and sort_list[j] >= tmp:
        buf = sort_list[j]
        del sort_list[j]
        sort_list.insert(j + 1, buf)
        j -= 1

for n, sl in enumerate(sort_list):
    if n != num - 1:
        print('{} '.format(sl),end='')
    else:
        print('{}'.format(sl))

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())
l = []
l = input().split()
l = list(map(int,l))
print(*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 = j - 1
    l[j+1] = v
    print(*l)

Submission status: Accepted

Solution code:
def insertSort(input_list):
    print(' '.join([str(i) for i in input_list]))
    for i in range(1, len(input_list)):
        tmp = input_list[i]
        j = i - 1

        while j >= 0 and input_list[j] > tmp:
            input_list[j + 1] = input_list[j]
            j -= 1
            input_list[j + 1] = tmp

        print(' '.join([str(i) for i in input_list]))


if __name__ == '__main__':
    N = int(input())

    L = input()
    input_list = [int(i) for i in L.split()]
    insertSort(input_list)