Last active
September 18, 2022 18:14
-
-
Save maxclav/c12075f3f063f55f3f7bbd63fb1435f4 to your computer and use it in GitHub Desktop.
Simple and custom selection sort in Go (GoLang).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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