Pages

Sunday, December 26, 2010

Selection Algorithm In Ruby

# Library for selection algorithm
#
# @author  : irfanudin ridho
# @email   : irfan.ub@gmail.com
# @date    : December 26, 2010
# @version : 1.0



# method to get the an index of the min value of the data
# @param data an array of the data
# @return minIndex int index of the min value
def getMinIndex(data)
    minIndex = 0
    len = data.length
    (1..len-1).each do |i|
        if(data[i]<data[minIndex])
            minIndex = i
        end
    end
    return minIndex
end


# method to get the an index of the max value of the data
# @param data an array of the data
# @return maxIndex int index of the max value
def getMaxIndex data
    maxIndex = 0
    len = data.length
    (1..len-1).each do |i|
        if(data[i]>data[maxIndex])
            maxIndex = i
        end
    end
    return maxIndex
end


# method to get min value of the data
# @param data an array of the data
# @return minValue min value of the data
def getMinValue data
    minValue = data[0]
    len = data.length
    (1..len-1).each do |i|
        if(data[i]<minValue)
            minValue = data[i]
        end
    end
    return minValue
end


# method to get max value of the data
# @param data an array of the data
# @return maxValue max value of the data
def getMaxValue data
    maxValue = data[0]
    len = data.length
    (1..len-1).each do |i|
        if(data[i]>maxValue)
            maxValue = data[i]
        end
    end
    return maxValue
end
           

No comments:

Post a Comment