Pages

Thursday, December 23, 2010

JS - FindMinMax Selection Algorithm

/**
 * FindMinMax
 * @author : irfanudin ridho
 * @email  : irfan.ub@gmail.com
 * @date   : December 24, 2010
 * @version: 1.0
 *
 * This class provide selection algorithm
 *
 * @data number, string data provided for sorting.
 */

function FindMinMax(data){
    this.data = data;
    this.min = this.data[0];
    this.max = this.data[0];
}

/**
 * @return min value of the data
 */FindMinMax.prototype.getMin = function(){
    for(i=1;i<this.data.length;i++){
        if(this.data[i]<this.min){
            this.min = this.data[i];
         }
    }
    return this.min;
}

/**
 * @return max value of the data
 */
FindMinMax.prototype.getMax = function(){
    for(i=1;i<this.data.length;i++){
        if(this.data[i]>this.max){
             this.max = this.data[i];
        }
    }
    return this.max;
}

/**
 * @return mean value of the data
 */FindMinMax.prototype.getMean = function(){
    var sum = 0;
    for(i=0;i<this.data.length;i++){
        sum = sum + this.data[i];
    }
    return (sum/this.data.length);
}

        

No comments:

Post a Comment