-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_index.py
32 lines (23 loc) · 1.07 KB
/
find_index.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
def min_index(mylist,min_el,last_index,position=0):
'''
Objective: Find the index of minimum element
Input parameter:
mylist: The list in which minimum element is to be found
position: To Traverse the list
min_el: Assumed as minimum element of the list
last_index: last index of the list is stored
Return value: minimum element of the list
'''
#Approach: First element is assumed as minimum of the list and then compare with rest of the elements
#if any of the element is smaller then minimum element then it becomes new minimum element
if (position < last_index):
if (mylist[min_el] > mylist[position+1]):
min_el = position+1
return min_index(mylist,min_el,last_index,position+1)
else: return min_el
mylist=[27,16,-5,29,11,5]
min_el_index = 0
last_index = len(mylist)-1
#print the index of minimum element
print('Index of minimum element')
print(min_index(mylist,min_el_index,last_index))