Pages

Saturday, December 25, 2010

Linear Search Algorithm

/**
 * Linear Search Algorithm
 * 
 * This class provide a framework on linear search algorithm
 * using Javascript.
 *
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 25, 2010
 * @version : 2.0
 * 
 */


/**
 * Constructor for this linear search algorithm
 * @param d array of data
 */ 
function LinearSearch(d){
    this.data = d;
}


/**
 * This function does to give a min value from the data
 * @return minValue min value from the data
 */
LinearSearch.prototype.getMinValue = function(){
    var minValue = this.data[0];
    for(i=0;i<this.data.length;i++){
        if(this.data[i+1]<minValue){
           minValue = this.data[i+1];
        }
    }
    return minValue;
}


/**
 * This function does to give a min index of the value from 
 * the data
 * @return minIndex an index of the min value from the data
 */
LinearSearch.prototype.getMinIndex = function(){
    var minIndex = 0;
    for(i=0;i<this.data.length;i++){
        if(this.data[i+1]<this.data[minIndex]){
            minIndex = i+1;
        }
    }
    return minIndex;
}


/**
 * This function does to give a max value from the data
 * @return maxValue max value from the data
 */
LinearSearch.prototype.getMaxValue = function(){
    var maxValue = this.data[0];
    for(i=0;i<this.data.length;i++){
        if(this.data[i+1]>maxValue){
             maxValue = this.data[i+1];
        }
    }
    return maxValue;
}


/**
 * This function does to give an index of the max value from
 * the data
 * @return maxIndex an index of the max value from the data
 */
LinearSearch.prototype.getMaxIndex = function(){
    var maxIndex = 0;
    for(i=0;i<this.data.length;i++){
        if(this.data[i+1]>this.data[maxIndex]){
             maxIndex = i+1;
        }
    }
    return maxIndex;
}
        

No comments:

Post a Comment