programming-examples/ruby/Algorithms/selection_sort.rb
2019-11-15 12:59:38 +01:00

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