3. Algorithms - Selection Sort
Selection sort sorts an array by finding the lowest value element and appending it to an empty array. The algorithm will shrink the unsorted array and repeat the process recursively until all unsorted elements are used up.
package main
func selectionSort(list *[]int) {
sorted := make([]int, 0, len(*list))
unSorted := *list
for i := 0; i < len(*list); i++ {
min := unSorted[0]
index := 0
for i, e := range unSorted {
if e <= min {
min = e
index = i
}
}
sorted = append(sorted, (unSorted)[index])
if index == 0 {
unSorted = unSorted[1:]
} else if index == (len(*list) - 1) {
unSorted = unSorted[:index]
} else {
unSorted = append(unSorted[0:index], unSorted[index+1:]...)
}
}
*list = sorted
}
/*func main() {
x := []int{4, 6, 3, 2, 9, 7, 3, 5}
fmt.Println(x)
selectionSort(&x)
fmt.Println(x)
}*/
Comments
Post a Comment