-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path10.SelectionSort.py
More file actions
51 lines (37 loc) · 1.8 KB
/
10.SelectionSort.py
File metadata and controls
51 lines (37 loc) · 1.8 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
def findMinimumIndex(lst, lower, i = 0, i_min = 0 ):
'''
objective: to find the index of minimum element of the given list
parameters: lst -> the list in which the the index of the minimum element is to be found according to lower and upper bound
lower -> stores the index of the lower bound
i -> i is an iterative value that is used to traverse the list from lower bound to end of the list
i_min -> stores the index of the minimum element of the sublist
return value: the index of the minimum element of the sublist
'''
# Approach : comparing the element at index i against the element at index i_min. If lst[i] < lst[i_min] is encountered, i_min is updated with value of i.
if (i + lower) == len(lst):
return (i_min + lower)
if lst[i + lower] < lst[i_min + lower] :
i_min = i
i = i + 1
return findMinimumIndex(lst, lower, i, i_min)
#-------------------------------------------------------------------------------------------------------------------------------------------------------------
def selectionSort( lst , i = 0):
'''
objective : to sort a given list
parameters : lst -> unsorted list that is to be sorted
i -> parameter holding the lower bound index
'''
#approach : finding the minimum element out of the unsorted part of list and push it on sorted part of list
if( i == len(lst)):
return
else:
mins = findMinimumIndex( lst, i )
temp = lst[mins]
lst[mins] = lst[i]
lst[i] = temp
return selectionSort(lst, i + 1)
#----------------------------------------------------
test = [30,20,10,5,13,86]
print('\n\nUnsorted List : ', test)
selectionSort(test)
print('\n\nSorted List : ', test)