Skip to content

Commit 3034b38

Browse files
author
codebasics
committed
selection sort
1 parent eb5c053 commit 3034b38

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Diff for: algorithms/7_SelectionSort/selection_sort.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
def selection_sort(arr):
3+
size = len(arr)
4+
for i in range(size-1):
5+
min_index = i
6+
for j in range(min_index+1,size):
7+
if arr[j] < arr[min_index]:
8+
min_index = j
9+
if i != min_index:
10+
arr[i], arr[min_index] = arr[min_index], arr[i]
11+
12+
13+
if __name__ == '__main__':
14+
tests = [
15+
[89, 78, 61, 53, 23, 21, 17, 12, 9, 7, 6, 2, 1],
16+
[],
17+
[1,5,8,9],
18+
[234,3,1,56,34,12,9,12,1300],
19+
[78, 12, 15, 8, 61, 53, 23, 27],
20+
[5]
21+
]
22+
for elements in tests:
23+
selection_sort(elements)
24+
print(elements)

0 commit comments

Comments
 (0)