Skip to content

Instantly share code, notes, and snippets.

@maxclav
Last active September 18, 2022 18:14
Show Gist options
  • Save maxclav/4ac57a7b49e35044cca0b241896c6ab9 to your computer and use it in GitHub Desktop.
Save maxclav/4ac57a7b49e35044cca0b241896c6ab9 to your computer and use it in GitHub Desktop.
Simple and custom insertion 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
// InsertionSort sorts the slice `vals` with the
// insertion sort algorithm.
// Time: O(n^2)
// Space: O(1)
func InsertionSort(vals []int) {
for i := 1; i < len(vals); i++ {
for j := i; j > 0 && vals[j-1] > vals[j]; j-- {
vals[j-1], vals[j] = vals[j], vals[j-1]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment