Created
January 9, 2017 06:32
-
-
Save cipepser/a224fb335644d3ca8567ec6aff87acb7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
func Min(a []int) (idx, n int) { | |
n = a[0] | |
idx = 0 | |
for i, v := range a { | |
if n > v { | |
n = v | |
idx = i | |
} | |
} | |
return | |
} | |
func SelectionSort(a []int) []int { | |
for i, _ := range a { | |
idx, _ := Min(a[i:len(a)]) | |
a[i], a[i + idx] = a[i + idx], a[i] | |
} | |
return a | |
} | |
func main() { | |
a := []int{2, 4, 5, 1, 3} | |
fmt.Println(SelectionSort(a)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment