class SelectionSort

Public Class Methods

run(data) click to toggle source
# File lib/compare-sort.rb, line 125
def self.run(data)
        len = data.length

        # iterate through each element in the array
        for i in 0..len-2
                min_index = i
                # iterate through the rest of the array
                for j in i+1..len-1
                        # if min, save index
                        min_index = j if data[j] < data[min_index]
                end
                # put the min in it's correct spot
                data[i], data[min_index] = data[min_index], data[i]
        end

        return data
end