Created
March 17, 2013 14:42
-
-
Save ackintosh/5181829 to your computer and use it in GitHub Desktop.
Selection sort in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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