Created
January 26, 2023 14:32
-
-
Save didil/416b6980073abd4e48737b7b823bcef8 to your computer and use it in GitHub Desktop.
go iteration sort generics ordered
This file contains hidden or 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
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