Last active
September 18, 2022 18:14
-
-
Save maxclav/4ac57a7b49e35044cca0b241896c6ab9 to your computer and use it in GitHub Desktop.
Simple and custom insertion 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 | |
// 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