Go/sorts/insertion_sort.go

22 строки
313 B
Go
Исходник Постоянная ссылка Обычный вид История

2016-12-05 18:59:57 +03:00
package main
import "fmt"
func main() {
arr := [9]int{2, 1, 4, 3, 5, 9, 7, 6, 8}
2016-12-05 18:59:57 +03:00
for out := 1; out < len(arr); out++ {
temp := arr[out]
2016-12-05 18:59:57 +03:00
in := out
for ; in > 0 && arr[in-1] >= temp; in-- {
2016-12-05 18:59:57 +03:00
arr[in] = arr[in-1]
}
arr[in] = temp
}
for _, sortedvals := range arr {
2016-12-05 18:59:57 +03:00
fmt.Println(sortedvals)
}
}