-
Notifications
You must be signed in to change notification settings - Fork 65
/
selection.py
31 lines (24 loc) · 850 Bytes
/
selection.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
""" Implementation of Selection Sort algorithm
"""
def selection(array):
""" Selection sort algorithm sorts an array by repeatedly finding the
minimum element (considering ascending order) from unsorted part and
putting it at the beginning.
:param array: list of elements that needs to be sorted
:type array: list
"""
length = len(array)
for i in range(length - 1):
min_element_index = i
for j in range(i+1, length):
if array[j] < array[min_element_index]:
min_element_index = j
array[i], array[min_element_index] = array[min_element_index], array[i]
def main():
""" operational function """
arr = [34, 56, 23, 67, 3, 68]
print(f"unsorted array: {arr}")
selection(arr)
print(f" sorted array: {arr}")
if __name__ == "__main__":
main()