Skip to content

Instantly share code, notes, and snippets.

@mindplace
Created February 27, 2016 00:26
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 mindplace/6865165f55d5ce0e2f7c to your computer and use it in GitHub Desktop.
Save mindplace/6865165f55d5ce0e2f7c to your computer and use it in GitHub Desktop.
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