Skip to content

Instantly share code, notes, and snippets.

@RavenZZ
Forked from RichardKnop/insertion_sort.go
Created July 12, 2017 03:29
Show Gist options
  • Save RavenZZ/da584a6e155b7549d9d3e9fbd8824c55 to your computer and use it in GitHub Desktop.
Save RavenZZ/da584a6e155b7549d9d3e9fbd8824c55 to your computer and use it in GitHub Desktop.
Insertion Sort
package main
import (
"fmt"
)
func main() {
items := []int{4, 202, 3, 9, 6, 5, 1, 43, 506, 2, 0, 8, 7, 100, 25, 4, 5, 97, 1000, 27}
insertionSort(items)
fmt.Println(items)
}
func insertionSort(items []int) {
var n = len(items)
for i := 1; i < n; i++ {
j := i
for j > 0 {
if items[j-1] > items[j] {
items[j-1], items[j] = items[j], items[j-1]
}
j = j - 1
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment