Created
May 1, 2017 05:26
-
-
Save trkrameshkumar/df53d0ea42b28e8d3a09e8d8167ce560 to your computer and use it in GitHub Desktop.
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
def selection_sort array | |
(array.length-1).times do |i| | |
minindex = i | |
minvalue = array[i] | |
(i).upto(array.length-1) do |j| | |
if array[j] < minvalue | |
minindex = j | |
minvalue = array[j] | |
end | |
if minvalue < array[i] | |
array[i] , array[minindex] = minvalue, array[i] | |
end | |
end | |
end | |
array | |
end | |
array = [10,5,8,93,6,2] | |
print selection_sort(array) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment