In the previous challenge, you wrote code to perform an Insertion Sort on an unsorted array. But how would you prove that the code is correct? I.e. how do you show that for any input your code will provide the right output?

Loop Invariant 

In computer science, you could prove it formally with a loop invariant, where you state that a desired property is maintained in your loop. Such a proof is broken down into the following parts:

Initialization: It is true (in a limited sense) before the loop runs.
Maintenance: If it's true before an iteration of a loop, it remains true before the next iteration.
Termination: It will terminate in a useful way once it is finished. 

Insertion Sort's Invariant 

Say, you have some InsertionSort code, where the outer loop goes through the whole array $\mbox{A}$:

for(int i = 1; i < A.length; i++){
//insertion sort code

You could then state the following loop invariant:

At the start of every iteration of the outer loop (indexed with $\boldsymbol{i}$), the subarray until $ar[i]$ consists of the original elements that were there, but in sorted order. 

To prove Insertion Sort is correct, you will then demonstrate it for the three stages:

Initialization - The subarray starts with the first element of the array, and it is (obviously) sorted to begin with.

Maintenance - Each iteration of the loop expands the subarray, but keeps the sorted property. An element $\mbox{V}$  gets inserted into the array only when it is greater than the element to its left. Since the elements to its left have already been sorted, it means $\mbox{V}$ is greater than all the elements to its left, so the array remains sorted. (In Insertion Sort 2 we saw this by printing the array each time an element was properly inserted.)  

Termination - The code will terminate after $\boldsymbol{i}$ has reached the last element in the array, which means the sorted subarray has expanded to encompass the entire array. The array is now fully sorted.

You can often use a similar process to demonstrate the correctness of many algorithms. You can see these notes for more information.

Challenge

In the InsertionSort code below, there is an error. Can you fix it? Print the array only once, when it is fully sorted. 

Input Format

There will be two lines of input:

$\boldsymbol{\mathrm{~S~}}$ - the size of the array
$\textbf{arr}$ - the list of numbers that makes up the array

Constraints

$1\leq s\leq1000$ 

$-1500\le V\le1500,V\in\textit{arr}$

Output Format

Output the numbers in order, space-separated on one line.

Sample Input
6
7 4 3 5 6 2

Sample Output
2 3 4 5 6 7

Explanation

The corrected code returns the sorted array.

Difficulty: EASY
Tags: Sorting
Skill types: Sorting

Starter code:

Solutions:
num_integers = int(input())
str_in = [int(x) for x in input().strip().split(' ')]
print(' '.join([str(x) for x in sorted(str_in)]))


r = int(input())
f = [int(t) for t in input().split() if t.isdigit()]
for i in sorted(f):
	print(i, end=' ')


import sys
array = [int(x) for x in sys.stdin.readlines()[1].split()]
for x in range(1, len(array)):
	val = array.pop(x)
	while x > 0 and array[x - 1] > val:
		x -= 1
	array.insert(x, val)
print(' '.join((str(x) for x in array)))


def in_loc(a, n):
	V = a[n]
	if n == 1:
		temp = a[0]
		a[0] = a[1]
		a[1] = temp
		return a
	for i in range(n - 1, -1, -1):
		if a[i] > V:
			a[i + 1] = a[i]
		else:
			a[i + 1] = V
			break
	if a[0] == a[1]:
		a[0] = V
	return a

def insertionSort(a, n):
	i = 1
	for i in range(1, n):
		if a[i] < a[i - 1]:
			a = in_loc(a, i)
	print(*a, sep=' ')

def main():
	n = int(input())
	a = [int(x) for x in input().split()]
	insertionSort(a, n)
main()


size = int(input())
v = [int(i) for i in input().strip().split()]
print(*sorted(v), sep=' ')


n = int(input())
l = []
l = input().split()
for i in range(0, n):
	l[i] = int(l[i])
l.sort()
for i in l:
	print(i, end=' ')


input()
nums = list(map(int, input().split()))
for count in range(len(nums) - 1):
	for i in range(count, -1, -1):
		if nums[i] > nums[i + 1]:
			(nums[i], nums[i + 1]) = (nums[i + 1], nums[i])
print(' '.join(map(str, nums)))


def main():
	s = int(input())
	ar = list(map(int, input().rstrip().split()))
	ar = sorted(ar)
	strar = map(str, ar)
	print(' '.join(strar))
	return 0
main()


nn = int(input())
vec = list(map(int, input().split()))
vec.sort()
print(' '.join(map(str, vec)))


size = input()
arr = map(int, input().strip().split(' '))
print(' '.join(map(str, sorted(arr))))


def insertion_sort(array):
	for (i, x) in enumerate(array[1:]):
		for (j, y) in enumerate(array[:i + 1]):
			if y > x:
				array[j + 1:i + 2] = array[j:i + 1]
				array[j] = x
				break
	return array

def main():
	array_size = int(input())
	array = list(map(int, input().split()))
	print(' '.join(('{}'.format(a) for a in insertion_sort(array))))
main()


n = int(input())
L = [int(s) for s in input().split()]
print(' '.join([str(i) for i in sorted(L)]))


s = input().split()
ar = list(map(int, input().split()))
V = ar[len(ar) - 1]
sort = len(ar) - 1
for index in range(1, len(ar)):
	value = ar[index]
	i = index - 1
	while i >= 0:
		if value < ar[i]:
			ar[i + 1] = ar[i]
			ar[i] = value
			i = i - 1
		else:
			break
print(*ar, sep=' ')


n = int(input())
array = [int(val) for val in input().split()]
for i in range(1, n):
	item = array[i]
	j = i
	while j > 0 and array[j - 1] > item:
		array[j] = array[j - 1]
		j -= 1
	array[j] = item
[print(val, end=' ') for val in array]
print()


import sys
N = int(sys.stdin.readline())
xs = [int(X) for X in sys.stdin.readline().split()]
[print(X, end=' ') for X in sorted(xs)]


size = int(input())
ar = list(map(int, input().split()))
for i in range(0, len(ar)):
	j = i
	while j >= 1 and ar[j] < ar[j - 1]:
		(ar[j], ar[j - 1]) = (ar[j - 1], ar[j])
		j -= 1
print(*ar)


import sys

def mToString(mArray):
	startStr = ''
	for el in mArray:
		startStr = startStr + str(el) + ' '
	return startStr[:-1]

def insertToArrayWithElement(numArray, myElementIndex):
	lastNum = numArray[myElementIndex]
	lastNumIndex = myElementIndex
	while lastNumIndex > 0 and lastNum < numArray[lastNumIndex - 1]:
		numArray[lastNumIndex] = numArray[lastNumIndex - 1]
		lastNumIndex = lastNumIndex - 1
	numArray[lastNumIndex] = lastNum
lines = []
for line in sys.stdin:
	lines.append(line)
arrayLen = int(lines[0])
numArray = list(map(int, lines[1].split()))
for i in range(1, arrayLen):
	insertToArrayWithElement(numArray, i)
print(mToString(numArray))


x = input()
y = input().split()
y = [int(i) for i in y]
y.sort()
y = [str(i) for i in y]
print(' '.join(y))


def insert_sort(A):
	m = len(A)
	for x in range(1, m):
		k = A[x]
		j = x
		while j > 0 and A[j - 1] > k:
			A[j] = A[j - 1]
			j -= 1
		A[j] = k
	return A
i = int(input())
N = [int(c) for c in input().split()]
N = insert_sort(N)
for a in N:
	print(a, end=' ')


s = int(input())
assert 1 <= s <= 1000
array = [int(x) for x in input().split(' ')]
sortz = array[:1]
for i in range(1, len(array)):
	sortz.append(array[i])
	j = len(sortz) - 1
	while sortz[j] < sortz[j - 1] and j > 0:
		temp = sortz[j]
		sortz[j] = sortz[j - 1]
		sortz[j - 1] = temp
		j -= 1
print(' '.join((str(x) for x in sortz)))


N = int(input())
arr = [int(x) for x in input().split(' ')]
arr.sort()
op = ''
for x in arr:
	op += str(x) + ' '
print(op)


n = int(input())
x = [0] * n
x = sorted(list(map(int, input().split())))
print(' '.join((str(i) for i in x)))


import sys

def insertion_sort(arr, s):
	for v in range(1, len(array)):
		value = arr[v]
		x = v - 1
		while x >= 0 and arr[x] > value:
			arr[x + 1] = arr[x]
			x -= 1
		arr[x + 1] = value
	print(' '.join([str(i) for i in arr]))
size = int(sys.stdin.readline().strip())
array = sys.stdin.readline().strip().split()
array = [int(x) for x in array]
insertion_sort(array, size)


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

def swap(a, x, y):
	(a[x], a[y]) = (a[y], a[x])
for i in range(len(arr)):
	j = i
	while j > 0 and arr[j] < arr[j - 1]:
		swap(arr, j - 1, j)
		j -= 1
print(' '.join(map(str, arr)))


T = int(input())
array = input().split()
newarray = []
for item in array:
	newarray.append(int(item))
newarray.sort()
print(' '.join((str(x) for x in newarray)))


s = int(input())
ar = [int(i) for i in str(input()).split()]
for i in range(1, s):
	x = ar[i]
	j = i
	while j > 0 and ar[j - 1] > x:
		ar[j] = ar[j - 1]
		j -= 1
	ar[j] = x
print(' '.join([str(e) for e in ar]))


input()
lst = list(map(int, input().split()))
lst.sort()
print(' '.join(map(str, lst)))


n = int(input())
a = []
s = input()
new = s.split()
s = []
for ele in new:
	s.append(int(ele))
new = sorted(s)
p = ' '.join(map(str, new))
print(p)


def get_and_return_list():
	return list(map(lambda x: int(x), input().split(' ')))

def insert(sort, elem):
	md = False
	for i in range(len(sort)):
		if sort[i] > elem:
			sort.insert(i, elem)
			md = True
			break
	if not md:
		sort.append(elem)
	return sort
s = int(input())
ar = get_and_return_list()
sort = []
sort.insert(0, ar.pop(0))
while len(ar) > 0:
	element = ar.pop(0)
	sort = insert(sort, element)
elems = '{}'
for i in sort[:-1]:
	elems = elems.format('%s {}' % i)
elems = elems.format(sort[-1])
print(elems)


input()
li = list(map(int, input().split()))
li.sort()
print(*li)


int(input())
nums = sorted([int(x) for x in str(input()).split()])
print(' '.join([str(x) for x in nums]))


import sys
data = sys.stdin.readlines()
temp = data[1].split(' ')
unsorted = [int(a) for a in temp]
for _ in data[1:]:
	unsorted.sort()
	result = ''
	for i in unsorted:
		result += str(i) + ' '
	result = result.strip()
	print(result)


N = int(input())
toys = [int(x) for x in input().split()]
toys.sort()
print(' '.join(map(str, toys)))


input()
numbers = list(map(lambda x: int(x), input().split(' ')))
sn = sorted(numbers)
print(' '.join(list(map(lambda x: str(x), sn))))


a = input()
b = input().split(' ')
b = [int(i) for i in b]
b.sort()
print(*b)


s = int(input())
ar = input().split()
ar = [int(ar[i]) for i in range(s)]
ar.sort()
for i in range(s):
	print(ar[i], end=' ')


N = int(input())
arr = [int(x) for x in input().split()]
for i in range(1, len(arr)):
	for j in range(1, i + 1):
		if arr[i - j] > arr[i - j + 1]:
			temp = arr[i - j]
			arr[i - j] = arr[i - j + 1]
			arr[i - j + 1] = temp
		else:
			break
printstr = ''
for x in arr:
	printstr += str(x)
	printstr += ' '
print(printstr)


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def backSwap(arr, j):
	A = arr[j]
	B = arr[j - 1]
	arr[j] = B
	arr[j - 1] = A

def insertion_sort(l):
	for i in range(1, len(l)):
		j = i
		while j > 0 and l[j] < l[j - 1]:
			backSwap(l, j)
			j = j - 1
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j > -1:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(len(l)):
		j = i
		key = l[i]
		while l[j - 1] > key and j > 0:
			l[j] = l[j - 1]
			j -= 1
		l[j] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(L):
	for i in range(1, len(L)):
		j = i - 1
		key = L[i]
		while L[j] > key and j >= 0:
			L[j + 1] = L[j]
			j -= 1
			L[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j > 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
ar.sort()
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(u) for u in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(len(l) - 1):
		j = i
		key = l[i + 1]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while j >= 0 and l[j] > key:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(len(l)):
		j = i
		while l[j] < l[j - 1] and j > 0:
			(l[j], l[j - 1]) = (l[j - 1], l[j])
			j -= 1
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i
		key = l[i]
		while j > 0 and l[j - 1] > key:
			l[j] = l[j - 1]
			j -= 1
		l[j] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while j >= 0 and l[j] > key:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i
		key = l[i]
		while (j > 0 and l[j - 1]) > key:
			l[j] = l[j - 1]
			j -= 1
		l[j] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i
		while l[j] < l[j - 1] and j > 0:
			(l[j - 1], l[j]) = (l[j], l[j - 1])
			j -= 1
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
			l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while -1 < j and key < l[j]:
			l[j + 1] = l[j]
			l[j] = key
			j -= 1
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


input()
a = map(int, input().split(' '))
print(*sorted(a))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while j > -1 and key < l[j]:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
input()
ar = list(map(int, input().split()))
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertionSort(theSeq):
	n = len(theSeq)
	for i in range(1, n):
		value = theSeq[i]
		pos = i
		while pos > 0 and value < theSeq[pos - 1]:
			theSeq[pos] = theSeq[pos - 1]
			pos -= 1
		theSeq[pos] = value
	for i in theSeq:
		print(i, end=' ')
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertionSort(ar)


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input())
ar = [int(i) for i in input().split()]
insertion_sort(ar)
print(*ar)


m = input().split()
ar = [int(i) for i in input().split()]
ar.sort()
a = [str(i) for i in ar]
print(' '.join(a))


def insertion_sort(l):
	for i in range(1, len(l)):
		j = i - 1
		key = l[i]
		while l[j] > key and j >= 0:
			l[j + 1] = l[j]
			j -= 1
		l[j + 1] = key
m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(' '.join(map(str, ar)))