Pages

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

No comments:

Post a Comment