Skip to content

Instantly share code, notes, and snippets.

@adsl99801
Last active March 23, 2018 04:06
Show Gist options
  • Save adsl99801/378e408dc53bbf04142edde06a62c0b5 to your computer and use it in GitHub Desktop.
Save adsl99801/378e408dc53bbf04142edde06a62c0b5 to your computer and use it in GitHub Desktop.
go selection sort
package main
import (
"fmt"
)
func findSmallest(arr []int) (int, int) {
if len(arr) < 0 {
panic("len < 0")
}
rIndex, rNum := 0, arr[0]
for index, item := range arr {
if item < rNum {
rIndex = index
rNum = item
}
}
return rIndex, rNum
}
func main() {
unsort := []int{16, 5, 11, 8, 1, 2, 20}
for i := range unsort {
rIndex, rNum := findSmallest(unsort[i:])
fmt.Printf("%v,%d\n", unsort[i:], rNum)
unsort[i], unsort[rIndex+i] = unsort[rIndex+i], unsort[i]
fmt.Printf("after:%v\n", unsort)
}
fmt.Printf("%v\n", unsort)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment