You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

19 lines
405 B
Ruby

# The Selection sort uses ~N^2 compares and N exchanges to sort an array,
# see examples in test/selection_sort_test.rb
module Selection
def self.sort(array)
n = array.size - 1
n.times do |i|
min = i
(i + 1).upto(n) do |j|
min = j if array[j] < array[min]
end
if i != min
array[i], array[min] = array[min], array[i]
end
end
array
end
end