Skip to content

Instantly share code, notes, and snippets.

@pdkproitf
Last active July 13, 2019 11:48
Show Gist options
  • Save pdkproitf/41baced722f695688a921ba40e944826 to your computer and use it in GitHub Desktop.
Save pdkproitf/41baced722f695688a921ba40e944826 to your computer and use it in GitHub Desktop.
Implement Selection Sort in ruby
# Algorithms: https://www.geeksforgeeks.org/selection-sort/
def selection_sort(array)
(array.length - 1).times do |i|
min_index = i
for j in (i + 1)...array.length
min_index = j if array[min_index] > array[j]
end
array[min_index], array[i] = array[i], array[min_index] if min_index != i
end
array
end
puts '------------------------------SELECTION SORT O(n^2)--------------------------'
print selection_sort([3,7,8,4,10,16,2,1,12])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment