2
0
mirror of https://github.com/0xAX/go-algorithms synced 2024-11-09 19:11:08 +00:00
go-algorithms/sorting/insertion_sort.go

31 lines
498 B
Go
Raw Normal View History

2017-05-31 18:02:12 +00:00
package main
/*
* Insertion sort - https://en.wikipedia.org/wiki/Insertion_sort
*/
import "fmt"
import "github.com/0xAX/go-algorithms"
func main() {
arr := utils.RandArray(10)
fmt.Println("Initial array is:", arr)
fmt.Println("")
2018-01-06 11:29:55 +00:00
if len(arr) <= 1 {
2017-05-31 18:02:12 +00:00
fmt.Println("Sorted array is: ", arr)
return
}
2018-01-06 11:29:55 +00:00
var i, j int
for i = 1; i < len(arr); i++ {
2018-01-24 12:43:58 +00:00
for j = 0; j < i; j++ {
if arr[j] > arr[i] {
arr[i], arr[j] = arr[j], arr[i]
}
2017-05-31 18:02:12 +00:00
}
}
fmt.Println("Sorted array is: ", arr)
}