-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfindMinElIndex.py
32 lines (27 loc) · 1.02 KB
/
findMinElIndex.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
#function to find the index of the minimum element in list in the range of given indexes
def findMinElIndex(list,minElIndex=0,position=0):
'''
Objective: To find the index of minimum element
Input Parameters:
list: list in which index of minimum element is to be found
minElIndex: Index of minimum element of list
position: To traverse each element of list
Return Value: minimum element of the list
'''
#Approach: Traverse each element of list recursively
lastIndex=len(list)-1
if (position < lastIndex):
if (list[minElIndex] > list[position+1]):
minElIndex = position+1
return findMinElIndex(list,minElIndex,position+1)
else:
return minElIndex
#Test Case 1: Checking upper bound
list1=[34,56,29,16,7]
print(findMinElIndex(list1))
#Test Case 2: Checking lower bound
list1=[2,34,56,29,16,7]
print(findMinElIndex(list1))
#Test Case 3: Checking intermediate element
list1=[34,56,3,29,16,7]
print(findMinElIndex(list1))