Pages

Showing posts with label Algorithm. Show all posts
Showing posts with label Algorithm. Show all posts

Tuesday, January 25, 2011

Finding Minimum And Maximum Value

/**
 * Finding maximum value over an array of integer.
 * 
 * @param count int
 *     size of elements of data
 * @param data array
 *     data in an array format
 *
 * @return data[index] int
 *     max value of the data
 */
int findMax( int count, int data[]){
    int maxIndex = 0;
    int i;
    for( i=0; i<count; i++){
        if(data[i] > data[maxIndex])
            maxIndex = i;
    }

    return data[maxIndex];
}


/**
 * Finding minimum value over an array of integer.
 * 
 * @param count int
 *     size of elements of data
 * @param data array
 *     data in an array format
 *
 * @return data[index] int
 *     min value of the data
 */
int findMin(int count, int data[]){
    int minIndex = 0;
    int i;
    for( i=0; i<count; i++){
        if(data[i]<data[minIndex])
            minIndex = i;
    }

    return data[minIndex];
}


Thursday, January 6, 2011

Pagination Using Javascript

function Pagination(d){
 this.data = d;
 this.item = 10;
 this.numPage = Math.ceil(this.data.length/this.item);
}

Pagination.prototype.setFirstPage = function(){
 var arr = new Array();
 for(i=0;i<this.item;i++){
  arr[i] = this.data[i];
 }
 return arr;
}

Pagination.prototype.setLastPage = function(){
 var arr = new Array();
 var startLastIndex = (this.numPage-1) * this.item;
 for(i=0;i<(this.data.length-startLastIndex);i++){
  arr[i] = this.data[startLastIndex];
  startLastIndex++;
 }
 return arr;
}

Pagination.prototype.next = function(){
 
}

Pagination.prototype.previous = function(){

}

Pagination.prototype.displayAtPage = function(n){
var arr = new Array();
 n = n-1;
 var startIndex = n * this.item;
 for(i=0;i<(this.item);i++){
  arr[i] = this.data[startIndex];
  startIndex++;
 }
 return arr;
}

Thursday, December 30, 2010

Grouping Algorithm Using Javascript

/**
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 29,2010
 * @version : 1.0
 *
 * This function will provide distinct selection algorithm.
 */

/**
 * Constructor service
 * @param data array of data
 */
function GroupingAlgorithm(data){
    this.data = data;
}

/**
 * a mehtod that provide base service to slice the data.
 * var data = [1,2,2,3,1,2]
 *          = [2,2,3,2]
 *          = [3]
 *          = []
 * @param varData array of data
 * @return sliced array
 */
GroupingAlgorithm.prototype.first = function(varData){
    var value = varData[0];
    var newData = new Array();
    var k = 0;
    for(i=1;i<varData.length;i++){
        if(varData[i]!=value){
            newData[k]=varData[i];
            k++;
        }
    }
    return newData;
};


/**
 * This function will do like what happend at SELECT DISTINCT 
 * in MySQL.
 * @return array unsorted distinct value;
 */
GroupingAlgorithm.prototype.getDistinctValue = function(){
    var rest = true;
    var distinctValue = new Array();
    var d = 0;
    var copyData = this.data;
    while(rest){
        distinctValue[d]=copyData[0];
        copyData = this.first(copyData);
        d++;
        if(copyData.length==0){
            rest = false;
        }

    }
    return distinctValue;
};

/**
 * A method to know how many occurence for specified value
 *
 * @return array of data
 */
GroupingAlgorithm.prototype.getTotalNumber = function(){
    var distinctData = this.getDistinctValue();
    var nTimes = new Array();
  
    for(i=0;i<distinctData.length;i++){
        var n = 0;
        for(j=0;j<this.data.length;j++){
            if(distinctData[i]==this.data[j]){
                n++;
            }
        }
        nTimes[i]=n;
    }
    return nTimes;
};
        

Selection Algorithm Using Java

/**
 * Selection Algorithm
 * This function provide service for selection algorithm.
 *
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 29, 2010
 * @version : 1.0
 */

/**
 * Constructor service
 * @param data array of data
 */
function DataAlgorithm(data){
    this.data = data;
}

/**
 * function to get min value from the data
 * @return min value of the data
 */
DataAlgorithm.prototype.getMinValue = function(){
    return this.data[this.getMinValueIndex()];
};

/**
 * function to get max value from the data
 * @return max value of the data
 */
DataAlgorithm.prototype.getMaxValue = function(){
    return this.data[this.getMaxValueIndex()];
};

/**
 * function to get index of the min value
 * @return number index of the min value
 */
DataAlgorithm.prototype.getMinValueIndex = function(){
    var minIndex = this.data[0];
    for(i=0;i<this.data.length;i++){
        if(this.data[i+1]<this.data[minIndex]){
            minIndex = i+1;
        }
    }
    return minIndex;
};

/**
 * method to get index of the max value
 * @return number index of the max value
 */
DataAlgorithm.prototype.getMaxValueIndex = function(){
    var maxIndex = this.data[0];
    for(i=0;i<this.data.length;i++){
        if(this.data[i+1]>this.data[maxIndex]){
            maxIndex = i+1;
        }
    }
    return maxIndex;
};
      

Selection Algorithm In Python

# Selection Algorithm
#
# @author  : irfanudin ridho
# @email   : irfan.ub@gmail.com
# @date    : December 31, 2010
# @version : 1.0


class SA:

    def __init__(self,data):
        self.data = data
 
    def getMinIndex(self):
        minIndex = 0
        lenn = len(self.data)
        for i in range(lenn):
            if(self.data[i]<self.data[minIndex]):
                minIndex = i
        return minIndex
 
    def getMinValue(self):
        return self.data[self.getMinIndex()]

    def getMaxIndex(self):
        maxIndex = 0
        lenn = len(data)
        for i in range(lenn):
            if(self.data[i]>self.data[maxIndex]):
                maxIndex = i
        return maxIndex

    def getMaxValue(self):
        return self.data[self.getMaxIndex()]
 

Manipulating Array

/**
 * This class provide functionality to manipulate array of 2
 * dimension
 *
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 31, 2010
 * @version : 1.0
 */

/**
 * A constructor service.
 */
function Arr(){

}

/**
 * A method to create a two dimensional array based on two array.
 * The format of the array is [n][2]. The first element is placed in
 * [n][0], while the second element is placed in [n][1]
 *
 * @param f array the first array
 * @param s array the second array
 * @return two dimensional of array
 */
Arr.prototype.twoDim = function(f,s){
    if(f.length!=s.length)
        return 'must same in length';
    var twoDimm = new Array();
    for(i=0;i<f.length;i++){
        twoDimm[i] = new Array();
        twoDimm[i][0] = f[i];
        twoDimm[i][1] = s[i];
    }
    return twoDimm;
};

/**
 * A method to get the first value of the second index
 * operator [n][0]
 *
 * @param data two dimensional of array in format [n][2]
 * @return first one dimensional of array in format[n]
 */
Arr.prototype.getFirst = function(data){
    var first = new Array();
    for(i=0;i<data.length;i++){
        first[i] = data[i][0];
    }
    return first;
};

/**
 * A method to get the second value of the second index 
 * operator [n][1]
 *
 * @param data two dimensional of array in format [n][2]
 * @return second one dimensional of array in format[n]
 */
Arr.prototype.getSecond = function(data){
    var second = new Array();
    for(i=0;i<data.length;i++){
        second[i] = data[i][1];
    }
    return second;
};

Grouping Algorithm

/**
 * Grouping Algorithm
 * This algorithm will do select just a distinct value of the data.
 *
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 31, 2010
 * @version : 2.0
 */

/**
 * A constructor service.
 *
 * @param data array of data.
 */
function Grouping(data){
    this.data = data;
}

/**
 * A private method that provide slice functionality of the 
 * array of data
 *
 * @param data data to be sliced.
 * @return sliced of array
 */
Grouping.prototype.slice = function(data){
    var value = data[0];
    var newData = new Array();
    var k = 0;
    for(i=1;i<data.length;i++){
        if(data[i]!=value){
            newData[k]=data[i];
            k++;
        }
    }
    return newData;
};

/**
 * A method that gives what this class intended. 
 * This gives a distinct
 * value of array in the form of array.
 *
 * @return distinct value of array
 */
Grouping.prototype.create = function(){
    var dataValue = new Array();
    var k = 0;
    var loop = true;
    var data = this.data;
    while(loop){
        dataValue[k] = data[0];
        data = this.slice(data);
        k++;
        if(data.length==0)
            loop = false;
        }
        return dataValue;
};


/**
 * This method provide serive on how many a value counting in the data.
 *
 * @return array counting value.
 */
Grouping.prototype.valueCounting = function(){
    var groupedData = this.create();
    var countValue = new Array();
    var data = this.data;
    for(i=0;i<groupedData.length;i++){
        var k = 1;
        for(j=0;j<data.length;j++){
            if(groupedData[i]==data[j]){
                countValue[i]=k;
                k++;
            }
        }
    }
    return countValue;
};


/**
 * A method to map distinct value and how many occurence of 
 * the value
 * in the data
 *
 * @return two dimensional array in the form [n][2]
 */
Grouping.prototype.mapping = function(){
    var data = this.data;
    var dimData = new Array();
    var value = this.create();
    var countValue = this.valueCounting();
    var len = value.length;
    for(i=0;i<len;i++){
        dimData[i] = new Array();
        dimData[i][0] = value[i];
        dimData[i][1] = countValue[i];
    }
    return dimData;
};

Tuesday, December 28, 2010

Rotating Algorithm Using JS

/**
 * This function will do rotate element of the data.
 * If the step of rotate, n, is equal length of data-1, the data
 * will be reversed.
 * 
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 29, 2010
 * @version : 1.0
 */

/**
 * @param data array of data
 * @param n number step to rotate
 * @return datas new rotated data in n step
 */
function Rotate(data,n){
    var datas = new Array();
    for(i=n;i<data.length;i++){
       datas[i] = data[i-n];
    }
    for(i=0;i<n;i++){
        datas[i]= data[data.length-1-i];
    }
    return datas;
}

Sunday, December 26, 2010

Selection Algorithm Using C++

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

class SelectionAlgorithm{
    private:
        int* data;
        int len;
    public:
        SelectionAlgorithm(int[],int);
        int getMinIndex();
        int getMaxIndex();
        int getMinValue();
        int getMaxValue();
};

SelectionAlgorithm::SelectionAlgorithm(int d[],int l){
    data = d;
    len = l;
}

int SelectionAlgorithm::getMinIndex(){
    int minIndex = 0;
    for(int i=1;i<len;i++){
        if(data[i]<data[minIndex]){
            minIndex = i;
        }
    }
    return minIndex;
}

int SelectionAlgorithm::getMaxIndex(){
    int maxIndex = 0;
    for(int i=1;i<len;i++){
        if(data[i]>data[maxIndex]){
            maxIndex = i;
        }
    }
    return maxIndex;
}

int SelectionAlgorithm::getMinValue(){
    int minValue = data[0];
    for(int i=1;i<len;i++){
        if(data[i]<minValue){
            minValue = data[i];
        }
    }
    return minValue;
}

int SelectionAlgorithm::getMaxValue(){
    int maxValue = data[0];
    for(int i=1;i<len;i++){
        if(data[i]>maxValue){
            maxValue = data[i];
        }
    }
    return maxValue;
}

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;
    }

}
       

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;
    }

}
          

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
           

Selection Algorithm In Python

# Class for implementation of Selection Algorithm
#
# @author  : irfanudin ridho
# @email   : irfan.ub@gmail.com
# @date    : December 26, 2010]
# @version : 1.0

class SelectionAlgorithm:
    def __init__(self,data):
        self.data = data
        self.len = len(data)

    def getMinIndex(self):
        minIndex = 0
        for i in range(1,self.len):
            if(self.data[i]<self.data[minIndex]):
                minIndex = i
        return minIndex

    def getMaxIndex(self):
        maxIndex = 0
        for i in range(1,self.len):
            if(self.data[i]>self.data[maxIndex]):
                maxIndex = i
        return maxIndex

    def getMinValue(self):
        minValue = self.data[0];
        for i in range(1,self.len):
            if(self.data[i]<minValue):
                minValue = self.data[i];
        return minValue;

    def getMaxValue(self):
        maxValue = self.data[0];
        for i in range(1,self.len):
            if(self.data[i]>maxValue):
                maxValue = self.data[i];
        return maxValue;

Selection Algorithm In C

/**
 * This function is an implementation of the selection algorithm.
 * 
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 26, 2010
 * @version : 1.0
 */


/**
 * This function will do give an index of the minimal value.
 * @param data array of integer of the data
 * @param len int length of the data
 * @return minIndex int an index of the min value
 */
int getMinIndex(int data[], int len){
    int minIndex = 0;
    int i;
    for(i=1;i<len;i++){
        if(data[i]<data[minIndex]){
           minIndex = i;
        }
    }
    return minIndex;
}




/**
 * This function will do give an index of the mmaximal value.
 * @param data an array of integer of the data
 * @param len int length of the data
 * @return maxIndex int an index of the min value
 */
int getMaxIndex(int data[], int len){
    int maxIndex = 0;
    int i;
    for(i=1;i<len;i++){
        if(data[i]>data[maxIndex]){
            maxIndex = i;
        }
    }
    return maxIndex;
}


/**
 * This function will do give the max value
 *
 * @param data an array of integer
 * @param len int length of the data
 * @return minValue int min value of the data
 */
int getMinValue(int data[], int len){
    int minValue = data[0];
    int i;
    for(i=1;i<len;i++){
        if(data[i]<minValue){
            minValue = data[i];
        }
    }
    return minValue;
}



/**
 * This function will do give the max value of the data
 *
 * @param data an array of integer
 * @param len length of the data array
 * @return maxValue int max value of the data
 */
int getMaxValue(int data[], int len){
    int maxValue = data[0];
    int i;
    for(i=1;i<len;i++){
        if(data[i]>maxValue){
            maxValue = data[i];
        }
    }
    return maxValue;
}

          
     

Saturday, December 25, 2010

Selection Sort Algorithm

/**
 * This framework provide a sorting service based on 
 * selection sort using Javascript.
 *
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 25, 2010
 * @version : 2.0
 */


/**
 * Constructor service
 * @param data Array of data
 */
function SelectionSort(data){
    this.data = data;
    this.len = this.data.length;
}


/**
 * method that provide ascending sorting services.
 * @return ascending order of array
 */
SelectionSort.prototype.getAsc = function(){
    for(i=0;i<this.len;i++){
        var minIndex = i;
        var tempValue = this.data[minIndex];
        for(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 that give a service to give descending order
 * @return descending order of an array
 */
SelectionSort.prototype.getDesc = function(){
    for(i=0;i<this.len;i++){
        var maxIndex = i;
        var tempValue = this.data[maxIndex];
        for(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;
}
       

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;
}
        

Friday, December 24, 2010

Linear Search Algorithm

/**
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 24, 2010
 * @version : 1.0
 *
 * This function provide search algorithm and it give the index
 * number of the desired values
 *
 * @param data Arrays of the data
 * @param value the desired value
 * @return i index of the value
 */

function linearSearch(data, value){
    for(i=0;i<data.length;i++){
        if(data[i]==value){
            return i;
        }
    }
    return "No found match!";
}
      

JS - Selection Sort

/**
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 24, 2010
 * @version : 1.0
 */
/*
 * This function will do ascending sorting
 * 
 * @param data Array
 * @return Array ascending sorted array
 */

function ascSort(data){
    for(i=0;i<data.length;i++){
        var minPos = i;  // index of min value
        for(j=1;j<data.length-1;j++){
            if(data[j+1]<data[minPos]){
                minPos = j+1;
            }
        }
        var temp = data[i];
        data[i] = data[minPos];
        data[minPos] = temp;
    }
}



/*
 * This function will do descending sorting
 * 
 * @param data Array
 * @return Array descending sorted array
 */

function descSort(data){
    for(i=0;i<data.length;i++){
        var maxPos = i;  // index of max value
        for(j=1;j<data.length-1;j++){
            if(data[j+1]>data[maxPos]){
                maxPos = j+1;
            }
        }
        var temp = data[i];
        data[i] = data[maxPos];
        data[maxPos] = temp;
    }
}