programming-examples/ruby/Algorithms/selection_sort.rb

19 lines
405 B
Ruby
Raw Normal View History

2019-11-15 12:59:38 +01:00
# 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