-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathselectionsort.py
More file actions
27 lines (19 loc) · 831 Bytes
/
selectionsort.py
File metadata and controls
27 lines (19 loc) · 831 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
'''
Selection Sort
Selection sort is a sorting algorithm that selects the smallest element from an unsorted list in each iteration and places that element at the beginning of the unsorted list.
Time Complexity - O(n^2)
'''
def selectionSort(list, length):
for step in range(length):
min_idx = step
for i in range(step + 1, length):
# to sort in descending order, change > to < in this line
# select the minimum element in each loop
if list[i] < list[min_idx]:
min_idx = i
# put min at the correct position
(list[step], list[min_idx]) = (list[min_idx], list[step])
array = [-2, 45, 0, 11, -9]
length = len(array)
selectionSort(array, length)
print(array) #[-9, -2, 0, 11, 45]