Source Code :
package main
import "fmt"
func selectionSort(arr []int) {
for i := 0; i < len(arr)-1; i++ {
minIndex := i
for j := i + 1; j < len(arr); j++ {
if arr[minIndex] > arr[j] {
minIndex = j
}
}
tmp := arr[i]
arr[i] = arr[minIndex]
arr[minIndex] = tmp
}
}
func main() {
arr := []int{11, 8, 35, 98, 3, 44, 24, 14, 10, 1}
fmt.Print("elements: ", arr, "\n", "In ascending order: [ ")
selectionSort(arr)
for i := 0; i < len(arr); i++ {
fmt.Print(arr[i], ", ")
}
fmt.Print("]\n")
}
Compile & Run :
Here's how to compile source code manually:
$ go build selectionsort.go
$ ./selectionsort
Run without compile:
$ go run selectionsort.go
Output Of Program :
Result program selection sort in Go |