Что у меня есть: алгоритм в псевдокоде из книги "Computer Science Distilled" (стр. 27)
function selection_sort(list)
for current <- 1 ... list.length - 1
smallest <- current
for i <- current + 1 ... list.length
if list[i] < list[smallest]
smallest <- i
list.swap_items(current, smallest)
Я пытаюсь понять это, поэтому я написал это в Go:
func main() {
list := []int{5, 2, 7, 9}
for current := 1; current < len(list)-1; current++ {
smallest := current
for i := current + 1; i < len(list); i++ {
if list[i] < list[smallest] {
smallest = i
}
}
current_tmp := list[current]
smallest_tmp := list[smallest]
list[current], list[smallest] = smallest_tmp, current_tmp
}
fmt.Printf("%v\n", list)
}
Детская площадка
И вывод [5 2 7 9]
. Я что-то упустил?