def selection_sort(list) return list if list.size <= 1 # already sorted 0.upto(list.length-2) do |i| min = i # smallest value (i+1).upto(list.length-1) { |j| min = j if list[j] < list[min] } # find new smallest list[i], list[min] = list[min], list[i] if i != min #swap values end list end
You need to create an account or log in to post comments to this site.