2
0
mirror of https://github.com/0xAX/go-algorithms synced 2024-11-13 13:10:30 +00:00
go-algorithms/sorting/selection_sort.go
2014-06-24 21:10:09 +06:00

34 lines
592 B
Go

package main
/*
* Selection sort - http://en.wikipedia.org/wiki/Selection_sort
*/
import "fmt"
import "github.com/0xAX/go-algorithms"
func main() {
arr := utils.RandArray(10)
fmt.Println("Initial array is:", arr)
fmt.Println("")
var min int = 0
var tmp int = 0
for i := 0; i < len(arr); i++ {
min = i
for j := i + 1; j < len(arr); j++ {
if arr[j] < arr[min] {
min = j
}
}
tmp = arr[i]
arr[i] = arr[min]
arr[min] = tmp
}
fmt.Println("Sorted array: ", arr)
}