Skip to content

Instantly share code, notes, and snippets.

@trkrameshkumar
Created May 1, 2017 05: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 trkrameshkumar/df53d0ea42b28e8d3a09e8d8167ce560 to your computer and use it in GitHub Desktop.
Save trkrameshkumar/df53d0ea42b28e8d3a09e8d8167ce560 to your computer and use it in GitHub Desktop.
Selection sort in ruby
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