Skip to content

Instantly share code, notes, and snippets.

@kathgironpe
Forked from brianstorti/selection_sort.rb
Created October 10, 2018 12:01
Show Gist options
  • Save kathgironpe/cee8a043a6d073d52001a7aeea345baa to your computer and use it in GitHub Desktop.
Save kathgironpe/cee8a043a6d073d52001a7aeea345baa to your computer and use it in GitHub Desktop.
Selection Sort in ruby
# Selection sort (very slow on large lists)
a = [9,8,6,1,2,5,4,3,9,50,12,11]
n = a.size - 1
n.times do |i|
index_min = i
(i + 1).upto(n) do |j|
index_min = j if a[j] < a[index_min]
end
# Yep, in ruby I can do that, no aux variable. w00t!
a[i], a[index_min] = a[index_min], a[i] if index_min != i
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment