-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsel_sort.py
More file actions
60 lines (45 loc) · 1.6 KB
/
sel_sort.py
File metadata and controls
60 lines (45 loc) · 1.6 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
57
58
def minOfList(lst, start, end):
'''
objective: to find index of minimum element in the list passed to this function
parameters: -> lst: list whose minimum has to be found
-> start: starting index from where min has to be calculated
-> end: last index upto which min has to be calculated
return value: index of smallest element of the list
'''
'approach: updating min_element using comparision in recursion and returning its index after traversing completely'
'''
if i == 0:
i = start
j = start
if i == (end+1):
return j
else:
if lst[i] < lst[j]:
min_element = lst[i]
j = i
i = i+1
return minOfList(lst,start, end, i, j)
'''
if start == end:
return end
elif lst[end] > lst[start]:
return minOfList(lst, start, end-1)
else:
return minOfList(lst, start+1, end)
def SelectionSort(lst, index=0):
'''
objective: to implement selection sort using recursive function minOfList()
parameters: -> lst: list which has to be sorted
-> index: DEFAULT PARAMETER used to traverse the list
return value: none
'''
'approach: finding the minimum element and swapping one by one'
if index == len(lst):
return
min_index = minOfList(lst, index, len(lst)-1)
lst[index],lst[min_index] = lst[min_index], lst[index]
return SelectionSort(lst, index+1)
lst = [50,10,30,0,40,100,90,0]
print("\nInitial list: ", lst)
SelectionSort(lst)
print("\nSorted list: ", lst)