module Ruborithms::Algorithms::SelectionSort::ClassMethods

Public Instance Methods

selection_sort(object) click to toggle source
# File lib/ruborithms/algorithms/selection_sort.rb, line 11
def selection_sort(object)
  0.upto(object.count - 1) do |i|
    min_value_index = find_index_of_min_value(object, i)
    swap(object, i, min_value_index)
  end
  object
end

Private Instance Methods

find_index_of_min_value(object, i) click to toggle source
# File lib/ruborithms/algorithms/selection_sort.rb, line 21
def find_index_of_min_value(object, i)
  min_index = i
  min_value = object[i]
  i.upto(object.count - 1) do |j|
    if object[j] < min_value
      min_index = j
      min_value = object[j]
    end
  end
  min_index
end
swap(object, i, min_index) click to toggle source
# File lib/ruborithms/algorithms/selection_sort.rb, line 33
def swap(object, i, min_index)
  object[i], object[min_index] = object[min_index], object[i]
end