-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathins_sorting.py
More file actions
56 lines (46 loc) · 1.46 KB
/
ins_sorting.py
File metadata and controls
56 lines (46 loc) · 1.46 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def insert_list(lst, ele, index, i=0):
'''
objective: to insert an element into a sorted list using recursion
parameters: -> lst: original list in which element has to be inserted
-> ele: element to insert into list
-> i=0: default parameter index
return value: none
'''
if i > index:
return lst
if i == index:
lst.insert(index, ele)
if i == len(lst):
lst.append(ele)
else:
if lst[i] > ele:
lst.insert(i,ele)
else:
i = i+1
insert_list(lst, ele, index, i)
return lst
def sortList(lst, sorted_index=0):
'''
flag = 0
for i in range(0, sorted_index):
if lst[i] > lst[participant]:
flag = 1
break
if flag == 1:
lst = insert_list(lst[:sorted_index], lst[participant]) + lst[sorted_index+1:]
sorted_index += 1
participant += 1
i += 1
if participant < len(lst):
return sortList(lst, sorted_index, participant)
return lst
'''
sorted_index = sorted_index + 1
if sorted_index == len(lst):
return lst
temp = lst[sorted_index]
del lst[sorted_index]
return sortList(insert_list(lst, temp, sorted_index), sorted_index)
lst = [4,2,10,3,5,0,11,9,0,100]
print("\nOriginal list: ", lst)
print("\nSorted list: ", sortList(lst))