Last active
March 23, 2018 04:06
-
-
Save adsl99801/378e408dc53bbf04142edde06a62c0b5 to your computer and use it in GitHub Desktop.
go selection sort
This file contains hidden or 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 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