class Algorithmable::Sort::Selection

Public Class Methods

sort(collection) click to toggle source

Does sorting in O(n2) time (quadratic time)

# File lib/algorithmable/sort/selection.rb, line 7
def self.sort(collection)
  return collection if collection.empty? || 1 >= collection.length
  length = collection.length - 1

  0.upto(length).each do |i|
    min = i
    (1 + i).upto(length).each do |j|
      min = j if collection[j] < collection[min]
    end
    exchange(i, min, collection)
  end

  collection
end