
function radixSort(array){
    var max = Math.max.apply(0, array);
    var m = getLoopTimes(max), len = array.length;
    var buckets = [];
    for(let i = 0; i < 10; i++){
        buckets[i] = []; //初始化10个桶
    }
    for(var d = 1; d <= m; d++){//个位，十位，百位，千位这样循环
        radixSortImpl(array, buckets, len, d);
    }
    return array;
}
// 取得数字num在某个位数d上的值
function getDigit(num, d){//0,1,2... 0表示个位数
    return (num+"").reverse()[d];
}
//或者这个 
function getDigit(num,i){
    return Math.floor((num / Math.pow(10, i)) % 10);
}
function radixSortImpl(array, buckets, len, d){
    for(let i = 0; i < len; i++){
        let el = array[i];
        let index = getDigit(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];
        }
        bucket.length = 0;
    }
}

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