Created
February 27, 2016 00:26
-
-
Save mindplace/6865165f55d5ce0e2f7c to your computer and use it in GitHub Desktop.
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
def selection_sort(array) | |
array.each_index do |i| | |
break if i == array.length - 1 | |
current = array[i] | |
smaller = array[i] | |
smaller_index = i | |
array.each_with_index do |item, j| | |
next if j <= i | |
if item < smaller | |
smaller = item | |
smaller_index = j | |
end | |
end | |
if smaller < current | |
array[smaller_index], array[i] = array[i], array[smaller_index] | |
end | |
end | |
p array | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment