Skip to content

Instantly share code, notes, and snippets.

@didil
Created January 26, 2023 14:32
Show Gist options
  • Save didil/416b6980073abd4e48737b7b823bcef8 to your computer and use it in GitHub Desktop.
Save didil/416b6980073abd4e48737b7b823bcef8 to your computer and use it in GitHub Desktop.
go iteration sort generics ordered
func InsertSort[T constraints.Ordered](items []T) {
for i := 1; i < len(items); i++ {
// loop through subslices
for j := i; j > 0; j-- {
if items[j] < items[j-1] {
// swap elements if not ordered
items[j], items[j-1] = items[j-1], items[j]
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment