-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecursivlySorting.py
59 lines (54 loc) · 1.72 KB
/
recursivlySorting.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
55
56
57
58
59
def maxElementIndex(list,length,index):
'''
Objective : To return a index of maximum value of list
Input Parameters :
length -> containing the length of list
index-> store a index of maximum value of list
Return: index of maximum value of list
'''
#Approach : Recursive
if(length>0):
if(list[index]<list[length-1]):
index=length-1
return maxElementIndex(list,length-1,index)
else:
return maxElementIndex(list,length-1,index)
else:
return index
def sorting(list,length):
'''
Objective : To sorting a list by recursion
Input Parameters :
maximum -> Store a index of maximum value in a list
index-> Temprory store a last index
temp -> temprory variable that use for swaping
Return:Sorted list
'''
#Approach Recrsivly Swap the maximum value with the last index of list
temp=0;
if(length>0):
index=length-1
maximum=maxElementIndex(list,length,index)
if(list[length-1]<list[maximum]):
temp=list[maximum]
list[maximum]=list[length-1]
list[length-1]=temp
return sorting(list,length-1)
else:
return sorting(list,length-1)
else:
return list
def main():
'''
Objective : Sorting by recursion
Input Parameters :
list-> containig list of element
length-> length of list
Result: Sorted list
'''
# Approach : Call the function sorting()
list=[12,4,241,48,54,544,78,55,20]
length=len(list)
print(sorting(list,length))
if __name__ == '__main__':
main()