Last active
July 13, 2019 11:48
-
-
Save pdkproitf/41baced722f695688a921ba40e944826 to your computer and use it in GitHub Desktop.
Implement Selection Sort in ruby
This file contains hidden or 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
# 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