-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectionSort.py
45 lines (38 loc) · 1.27 KB
/
SelectionSort.py
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
"""
Selection Sort
Time Complexity: O(n^2)
"""
def FindMin(arr):
"""finds minimum element from the list"""
min = 100000 # let list contain +ve numbers only: so minimum number is -1
for i in range(len(arr)):
if arr[i] < min:
min = arr[i]
return min
def SelectionSort(arr):
size = len(arr)
for i in range(size-1): # decreased 1 iteration: no need to iterate to the last element
minIndex = i # pointer initialised to ith index for selection sort
for j in range(minIndex+1, size):
if arr[j] < arr[minIndex]: # if current index < minimum index of the array
minIndex = j # minimum index updated
# swapping element
if i != minIndex:
arr[i], arr[minIndex] = arr[minIndex], arr[i]
if __name__ == '__main__':
elements = [100, 19, 28, 14, 6, 1, 99]
print(FindMin(elements)) # prints minimum element from the list
SelectionSort(elements)
print(elements)
# running test cases
tests = [
[89, 78, 61, 23, 21, 53, 12, 1, 2, 6, 3, 17, 9],
[],
[1, 2, 3, 4, 5],
[350, 3, 1, 99, 12, 78, 12, 1200],
[9]
]
# print("Tests case testing...")
for elements in tests:
SelectionSort(elements)
print(elements)