m******u 发帖数: 12400 | 1 def selection_sort(list)
i = 0
while i < list.size
smallest = list[i]
j = i + 1
while j < list.size
smallest = list[j] if list[j] < smallest
j = j + 1
end
b = list.index(smallest)
list[i], list[b] = list[b], list[i]
i = i + 1
end
list
end | n*******e 发帖数: 4894 | 2 估计问题出在 b = list.index(smallest)
试试记录smallest 的 index,然后用这个来swap
【在 m******u 的大作中提到】 : def selection_sort(list) : i = 0 : while i < list.size : smallest = list[i] : j = i + 1 : while j < list.size : smallest = list[j] if list[j] < smallest : j = j + 1 : end : b = list.index(smallest)
|
|