-
Notifications
You must be signed in to change notification settings - Fork 13
/
Code Insertion Sort.py
53 lines (36 loc) · 930 Bytes
/
Code Insertion Sort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Provided with a random integer array/list(ARR) of size N,
# you have been required to sort this array using 'Insertion Sort'.
from sys import stdin
def insertionSort(arr, n):
#Your code goes here
i = 1
while i < n:
temp = arr[i]
j = i - 1
while j >= 0:
if arr[j] > temp:
arr[j + 1] = arr[j]
else:
break
j -= 1
arr[j + 1] = temp
i += 1
#Taking Input Using Fast I/O
def takeInput():
n = int(stdin.readline().strip())
if n == 0:
return list(), 0
arr = list(map(int, stdin.readline().strip().split(" ")))
return arr, n
#to print the array/list
def printList(arr, n):
for i in range(n):
print(arr[i], end=" ")
print()
#main
t = int(stdin.readline().strip())
while t > 0:
arr, n = takeInput()
insertionSort(arr, n)
printList(arr, n)
t -= 1