-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection_sort.py
More file actions
33 lines (27 loc) · 1016 Bytes
/
selection_sort.py
File metadata and controls
33 lines (27 loc) · 1016 Bytes
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
def selection_sort(arr):
for ind,i in enumerate(arr):
smallest_ind = find_smallest(arr,ind)
arr[ind],arr[smallest_ind]= arr[smallest_ind],arr[ind]
return arr
def find_smallest(arr,start):
smallest = (-1,float('inf'))
for i in range(start,len(arr)):
if arr[i]<smallest[1]:
smallest = (i,arr[i])
return smallest[0]
arr = [8,3,9,1,4]
#selection_sort(arr)
def recursive_find_smallest(arr,current,smallest,smallest_ind):
if current == len(arr)-1:
return smallest_ind
if arr[current] < smallest:
return recursive_find_smallest(arr,current+1,arr[current],current)
return recursive_find_smallest(arr, current+1,smallest,smallest_ind)
def recursive_selection_sort(ind,arr):
if ind == len(arr)-1:
return arr
smallest_ind = recursive_find_smallest(arr,ind,float('inf'),-1)
arr[ind],arr[smallest_ind]= arr[smallest_ind],arr[ind]
return recursive_selection_sort(ind+1,arr)
recursive_selection_sort(0,arr)
print(arr)