-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsertion Sort.py
54 lines (43 loc) · 1.1 KB
/
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
54
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'insertionSort1' function below.
#
# The function accepts following parameters:
# 1. INTEGER n
# 2. INTEGER_ARRAY arr
#
def insertionSort1(n, arr):
temp=arr[-1]
for i in range(2,n+1):
if temp < arr[-i]:
if i==n and arr[-n]>temp:
arr[-i+1]=arr[-i]
for i in arr:
print(i,end=" ")
print()
arr[-i]=temp
for i in arr:
print(i,end=" ")
continue
arr[-i+1]=arr[-i]
for i in arr:
print(i,end=" ")
print()
else :
if arr[0]>temp:
arr[0]=temp
else :
arr[-i+1]=temp
for i in arr:
print(i,end=" ")
print()
break
if __name__ == '__main__':
n = int(input().strip())
arr = list(map(int, input().rstrip().split()))
insertionSort1(n, arr)