Skip to content

Instantly share code, notes, and snippets.

@cipepser
Created January 9, 2017 06:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cipepser/fb4e64acc94d5b92a0227e898df99aa1 to your computer and use it in GitHub Desktop.
Save cipepser/fb4e64acc94d5b92a0227e898df99aa1 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
)
func InsertionSort(a []int) []int {
for i := 1; i < len(a); i++ {
for j := 0; j < i; j++ {
if a[i - j - 1] > a[i - j] {
a[i - j - 1], a[i - j] = a[i - j], a[i - j - 1]
} else {
break
}
}
}
return a
}
func main() {
a := []int{2, 4, 5, 1, 3}
fmt.Println(InsertionSort(a))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment