~telemachus/algorithms

algorithms/selectionsort.go -rw-r--r-- 339 bytes
c4396785Peter Aronoff Update for golangci-lint 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package algorithms

func SelectionSort(xs []int) {
	for i := 0; i < len(xs)-1; i++ {
		minIndex := findMinIndexFrom(xs, i)
		xs[i], xs[minIndex] = xs[minIndex], xs[i]
	}
}

func findMinIndexFrom(xs []int, n int) int {
	minIndex := n
	for i := n + 1; i < len(xs); i++ {
		if xs[i] < xs[minIndex] {
			minIndex = i
		}
	}
	return minIndex
}