Home > other >  JS write MSD distribution method of sorting result is wrong
JS write MSD distribution method of sorting result is wrong

Time:09-19

Window. The onl oad=function () {
//because the array's largest only three elements, so the incoming 3
The console. The log (radixSortRise (,15,48,75,46,37,90,100 [99], 3100, 0100));
//the console. The log (radixSortDown (,15,48,75,46,37,90,100 [99], 3));
}

/* *
* JS implementation radix sort ascending
-* @ param arr to sort an array
* @ param maxDigit elements of the largest digit
* @ param mod and dev depends on how much is the largest number if it is one hundred
* mod incoming 1000 dev to 100
* */
The function radixSortRise (arr, maxDigit, mod, dev) {
//create the VAT
Var counter=[];

//the original array element subscript
Var pos.

//1. According to the elements of the largest digit LSD distribution method of the maximum number of allocated
//2. The largest number of all the elements for 3
//3. Because of all the elements in the current largest element in only three digits, so distribution at most three times
//4. Distribution after three sequenced
for (var i=0; i //the array arr for sorting each of integers, insert the corresponding container
for(var j=0; J & lt; Arr. Length; J++) {

//1. In order to obtain each element of each digit, starting from the bits, until to maximum digits
//2. Through numerical digits get elements to store small bucket subscript
Var bucket=parseInt ((arr [j] % mod)/dev);

//1. If the subscript corresponds to the small bucket keg there is no
Founded in the VAT//2. The subscript keg
If (counter/bucket==null) {
Counter [bucket]=[];
}
//the deposit element to specify the subscript small bucket
Counter/bucket. Push (arr) [j].
}

//every time before are collected, initialization of the original array array subscript
Pos=0;
//ascending collection
//start from vats of the first small bucket, in turn, each small barrel, and is due to return to the original elements in each small bucket array
for(var j=0; J & lt; Counter. Length; J++) {
//if the keg there
If (counter [j].=null) {
//remove the first element of the current small bucket
Var value=https://bbs.csdn.net/topics/counter [j]. The shift ();
//if this element is the starting from the first element in turn take out all the elements
while (value !=null) {
//put this element in the current element position, and after it finished, continue to get the next element position of subscript
Arr [pos++]=value;
//get the next element in the current small bucket, and after obtaining the barrels to delete this element from childhood will in the
Value=https://bbs.csdn.net/topics/counter [j]. The shift ();
}
}
}
}
//will be sorted array returned to the external
return arr;
}

CodePudding user response:

Just write a few days ago, radix sort, bucket sort have, oneself see https://github.com/zhengw060024/algorithmpratice/tree/master/algorithemJs/chapter8

CodePudding user response:

Code is ts, do you find a typescript compiler, or directly with js files can also

  • Related