Pages

Sunday, December 26, 2010

Selection Algorithm Using Java

/**
 * Class to provide selection algorithm implementation
 * 
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 27, 2010
 * @version : 1.0
 */

class SelectionAlgorithm{

    /*
     * data array of integer
     */
    private int[] data;

    /*
     * len size of the data
     */
    private int len;


    /**
     * A constructor service
     * @param data array of integer
     */
    public SelectionAlgorithm(int[] data){
        this.data = data;
        this.len = this.data.length;
    }

    /**
     * method to get an index of min value of the data
     * @return minIndex int index of min value
     */
    public int getMinIndex(){
        int minIndex = 0;
        for(int i=1;i<this.len;i++){
            if(this.data[i]<this.data[minIndex]){
                minIndex = i;
            }
        }
        return minIndex;
    }

    /**
     * method to get an index of max value of the data
     * @return maxIndex int an index of max value
     */
    public int getMaxIndex(){
        int maxIndex = 0;
        for(int i=1;i<this.len;i++){
            if(this.data[i]>this.data[maxIndex]){
                maxIndex = i;
            }
        }
        return maxIndex;
    }

    /**
     * method to get min value
     * @return minValue int min value of the data
     */
    public int getMinValue(){
        int minValue = this.data[0];
        for(int i=1;i<this.len;i++){
            if(this.data[i]<minValue){
                minValue = this.data[i];
            }
        }
        return minValue;
    }

    /**
     * method to get max value
     * @return maxValue int max value of the data
     */
    public int getMaxValue(){
        int maxValue = this.data[0];
        for(int i=1;i<this.len;i++){
            if(this.data[i]>maxValue){
                maxValue = this.data[i];
            }
        }
        return maxValue;
    }

}
          

No comments:

Post a Comment