-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertion_sort.py
More file actions
37 lines (33 loc) · 1.01 KB
/
Copy pathinsertion_sort.py
File metadata and controls
37 lines (33 loc) · 1.01 KB
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
# declaring empty list
list = []
take_input = True
# taking input for the list
while take_input:
try:
element = int(input("Please enter a number for the list :"))
list.append(element)
except ValueError:
print("Please provide a valid number")
condition = False
while not condition:
yes_no = input("Do you want to provide another value?(y/n)")
if yes_no == 'y':
take_input = True
condition = True
elif yes_no == 'n':
take_input = False
condition = True
else:
print("Incorrect input! Please enter (y/n)")
print("List you provided is :", list)
for j in range (1, len(list)):
# determine the key
key = list[j]
# set i to find preceeding element
i = j - 1
while i >= 0 and list[i] > key:
# replace the suceeding element by preceeding if it is greater than key
list[i + 1] = list[i]
i = i - 1
list[i + 1] = key
print("List Sorted in ascending order is :", list)