Skip to content

Commit a3916af

Browse files
committed
solved: practicing selection sort -@iamserda
1 parent c695f01 commit a3916af

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

neetcodeio/algostructybeginners/Lv3-Sorting/selection_sort.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
class Solution:
22

33
def selection_sort(self, arr: list) -> list:
4-
pass
4+
for i in range(len(arr)):
5+
# where to place the next smallest value
6+
min_index = i
7+
8+
for j in range(i, len(arr)):
9+
# find the index of the smallest value
10+
if arr[j] < arr[min_index]:
11+
min_index = j
12+
if min_index != i:
13+
# swap the value at i with the value at min_index
14+
arr[i], arr[min_index] = arr[min_index], arr[i]
515

616

717
sol = Solution()

0 commit comments

Comments
 (0)