Skip to content

Instantly share code, notes, and snippets.

@ackintosh
Created March 17, 2013 14:42
Show Gist options
  • Select an option

  • Save ackintosh/5181829 to your computer and use it in GitHub Desktop.

Select an option

Save ackintosh/5181829 to your computer and use it in GitHub Desktop.
Selection sort in Ruby
class Array
def selection_sort
tmp = self.dup
res = []
res.push tmp.delete_min until tmp.empty?
res
end
def delete_min
min_index = find_index { |v| v == self.min }
delete_at(min_index)
end
end
p [3,1,5,6,10,9].selection_sort
# [1, 3, 5, 6, 9, 10]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment