
function buckertSort(array){
    var max = Math.max.apply(0, array);
    var m = getLoopTimes(max), n = array.length;
    for(var i = 1; i <= m; i++){
        buckertSortImpl(array, n, i);
    }
    return array;
}
// 234 
function getDecimal(num, i){//0,1,2... 0表示个位数
    return (num+"").reverse()[i];
}
function buckertSortImpl(array, len, d){
    var buckets = [];
    for(let i = 0; i < 10; i++){
        buckets[i] = []; //初始化10个桶
    }
    for(let i = 0; i < len; i++){
        let el = array[i];
        let index = getDecimal(el, d);
        buckets[index].push(el);
    }
    var k = 0;
    for(let i = 0; i < 10; i++){
        let bucket = buckets[i];
        for(let j = 0; j < bucket.length; j++){
            array[k++] = bucket[j];
        }
    }
}

//获取数字的位数
function getLoopTimes(num){
    var count = 1;
    var temp = num / 10;
    while(temp != 0) {
        count++;
        temp = temp / 10;
    }
    return count;
}