Skip to content

Instantly share code, notes, and snippets.

@Nitesh-Mishra
Created September 11, 2017 19:16
Show Gist options
  • Select an option

  • Save Nitesh-Mishra/f77bf9fec98f20cb3d9bb0923435f6d3 to your computer and use it in GitHub Desktop.

Select an option

Save Nitesh-Mishra/f77bf9fec98f20cb3d9bb0923435f6d3 to your computer and use it in GitHub Desktop.
Selection Sort program in ruby
# selection_sort.rb
#
# $ ruby selection_sort.rb
# [1, 2, 3, 4, 5, 6, 7, 8]
def selection_sort(array)
n = array.length-1
for i in 0..n-1
min = i
j = i + 1
for j in j..n
if array[min] > array[j]
min = j
end
end
if i != min
array[i],array[min] = array[min],array[i]
end
end
end
array = [6,5,3,1,8,7,2,4]
selection_sort array
print array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment