Skip to content

Commit 170f7d9

Browse files
Adding selection sort
1 parent 417b631 commit 170f7d9

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

Go/sorting/selection.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package sorting
2+
3+
func selectionSort(arr []int, n int) {
4+
var minIndex int
5+
for iteration := 0; iteration < n; iteration++ {
6+
minIndex = iteration
7+
for pointer := iteration + 1; pointer < n; pointer++ {
8+
if arr[pointer] < arr[minIndex] {
9+
minIndex = pointer
10+
}
11+
}
12+
arr[iteration], arr[minIndex] = arr[minIndex], arr[iteration]
13+
}
14+
}

0 commit comments

Comments
 (0)