Created
July 28, 2016 18:48
-
-
Save bitops/dc53ed599a472d3dcb1c991627c4a437 to your computer and use it in GitHub Desktop.
selection sort :)
This file contains 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, start_idx=0) | |
smallest_value, swap_idx = nil, nil | |
for idx in start_idx..(array.size - 1) | |
val = array[idx] | |
if smallest_value.nil? || smallest_value > val | |
smallest_value = val | |
swap_idx = idx | |
end | |
end | |
if swap_idx.nil? | |
array | |
else | |
array[start_idx], array[swap_idx] = array[swap_idx], array[start_idx] | |
selection_sort(array, start_idx + 1) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment