Skip to content

Instantly share code, notes, and snippets.

@bitops
Created July 28, 2016 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitops/dc53ed599a472d3dcb1c991627c4a437 to your computer and use it in GitHub Desktop.
Save bitops/dc53ed599a472d3dcb1c991627c4a437 to your computer and use it in GitHub Desktop.
selection sort :)
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