Skip to content

Instantly share code, notes, and snippets.

@maxclav
Last active September 18, 2022 18:14
Show Gist options
  • Save maxclav/c12075f3f063f55f3f7bbd63fb1435f4 to your computer and use it in GitHub Desktop.
Save maxclav/c12075f3f063f55f3f7bbd63fb1435f4 to your computer and use it in GitHub Desktop.
Simple and custom selection sort in Go (GoLang).
package slice
// For learning purpose only.
// Please, use the sort package of Go's Standard library: https://pkg.go.dev/sort
// SelectionSort sorts the slice `vals` with the
// selection sort algorithm.
// Time: O(n^2)
// Space: O(1)
func SelectionSort(vals []int) {
for i := 0; i < len(vals)-1; i++ {
currentMin := i
for j := i + 1; j < len(vals); j++ {
if vals[j] < vals[currentMin] {
currentMin = j
}
}
if currentMin != i {
vals[currentMin], vals[i] = vals[i], vals[currentMin]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment