Selection Sort in Ruby

Ruby code to Implement Selection Sort. This Algorithm is very simple. Find the smallest element in the array and swap it. That's all you will get a sorted array in the end.

Source code :


array = Array[1,45,10,35,100,13,147,500,80]

size = array.size

min = 0

for i in 0..size
    for j in (i+1)..size
        if array[j-1] < array[i-1]
            min = array[j-1]
            array[j-1] = array[i-1]
            array[i-1] = min
        end
    end
end

puts array


Output :

1
10
13
35
45
80
100
147
500

Output :


                

Comments :