Pages

Sunday, December 26, 2010

Sort Selection Algorithm Using Java

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

class Sorting{


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

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

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

    /*
     * method to get ascending series of the data
     * @return array in ascending order
     */
    public int[] getAsc(){       
        for(int i=0;i<this.len;i++){
            int minIndex = i;
            int tempValue = this.data[minIndex];
            for(int j=i;j<this.len-1;j++){
                if(this.data[j+1]<this.data[minIndex]){
                    minIndex = j+1;
                }
            }
            this.data[i] = this.data[minIndex];
            this.data[minIndex] = tempValue;
        }
        return this.data;
    }

    /*
     * method to get data in descending order
     * @return array in descending order
     */
    public int[] getDesc(){
        for(int i=0;i<this.len;i++){
            int maxIndex = i;
            int tempValue = this.data[maxIndex];
            for(int j=i;j<this.len-1;j++){
                if(this.data[j+1]>this.data[maxIndex]){
                    maxIndex = j+1;
                }
            }
            this.data[i] = this.data[maxIndex];
            this.data[maxIndex] = tempValue;
        }
        return this.data;
    }

}
       

No comments:

Post a Comment