forked from bfaure/Python_Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
30 lines (26 loc) · 757 Bytes
/
Copy pathmain.py
File metadata and controls
30 lines (26 loc) · 757 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
def create_array(length=10,maxint=50):
from random import randint
return [randint(0,maxint) for _ in range(length)]
'''
def selection_sort(arr):
sort_idx=0 # end of sorted portion of array
while sort_idx<len(arr):
min_idx=None # index of smallest item found
for i,elem in enumerate(arr[sort_idx:]):
if min_idx==None or elem<arr[min_idx]:
min_idx=i+sort_idx
arr[sort_idx],arr[min_idx]=arr[min_idx],arr[sort_idx]
sort_idx+=1
return arr
'''
def selection_sort(arr):
sort_idx=0 # end of sorted portion of array
while sort_idx<len(arr):
min_idx=arr[sort_idx:].index(min(arr[sort_idx:]))+sort_idx
arr[sort_idx],arr[min_idx]=arr[min_idx],arr[sort_idx]
sort_idx+=1
return arr
a=create_array()
print a
a=selection_sort(a)
print a