function getDigit(num, i){returnMath.floor(Math.abs(num)/Math.pow(10,i))%10}
function digitCount(num){if(num ===0)return1returnMath.floor(Math.log10(Math.abs(num)))+1}
function mostDigitCount(nums){
let maxDigit =0for(let i =0;i< nums.length;i++){
maxDigit =Math.max(maxDigit,digitCount(nums[i]))}return maxDigit
}
function Radix(nums){
let maxDigitCount =mostDigitCount(nums)for(let k=0; k< maxDigitCount; k++){
let digitbucket =Array.from({length:10},()=>[])for(let i=0; i< nums.length; i++){
let digit =getDigit(nums[i], k)
digitbucket[digit].push(nums[i])}
nums =[].concat(...digitbucket)}return nums
}
console.log(Radix([23,123,23333,444444,55555555]))
function countingSort(arr, size, place){
let output =newArray(size +1).fill(0);
let max =Math.max(...arr);
let freq =newArray(max +1).fill(0);// Calculate count of elementsfor(let i =0; i < size; i++){const num =Math.floor(arr[i]/ place)%10;
freq[num]++;}// Calculate cummulative countfor(let i =1; i <10; i++){
freq[i]+= freq[i -1];}// Place the elements in sorted orderfor(let i = size -1; i >=0; i--){const num =Math.floor(arr[i]/ place)%10;
output[freq[num]-1]= arr[i];
freq[num]--;}//Copy the output arrayfor(let i =0; i < size; i++){
arr[i]= output[i];}}
function radixSort(arr, size = arr.length){//Get the max element
let max =Math.max(...arr);//Sort the array using counting sortfor(let i =1;parseInt(max / i)>0; i *=10){countingSort(arr, size, i);}}
// Radix sort Java implementationimportjava.io.*;importjava.util.*;classRadix{// A utility function to get maximum value in arr[]staticintgetMax(int arr[],int n){int mx = arr[0];for(int i =1; i < n; i++)if(arr[i]> mx)
mx = arr[i];return mx;}// A function to do counting sort of arr[] according to// the digit represented by exp.staticvoidcountSort(int arr[],int n,int exp){int output[]=newint[n];// output arrayint i;int count[]=newint[10];Arrays.fill(count,0);// Store count of occurrences in count[]for(i =0; i < n; i++)
count[(arr[i]/ exp)%10]++;// Change count[i] so that count[i] now contains// actual position of this digit in output[]for(i =1; i <10; i++)
count[i]+= count[i -1];// Build the output arrayfor(i = n -1; i >=0; i--){
output[count[(arr[i]/ exp)%10]-1]= arr[i];
count[(arr[i]/ exp)%10]--;}// Copy the output array to arr[], so that arr[] now// contains sorted numbers according to current digitfor(i =0; i < n; i++)
arr[i]= output[i];}// The main function to that sorts arr[] of size n using// Radix Sortstaticvoidradixsort(int arr[],int n){// Find the maximum number to know number of digitsint m =getMax(arr, n);// Do counting sort for every digit. Note that// instead of passing digit number, exp is passed.// exp is 10^i where i is current digit numberfor(int exp =1; m / exp >0; exp *=10)countSort(arr, n, exp);}// A utility function to print an arraystaticvoidprint(int arr[],int n){for(int i =0; i < n; i++)System.out.print(arr[i]+" ");}/*Driver Code*/publicstaticvoidmain(String[] args){int arr[]={170,45,75,90,802,24,2,66};int n = arr.length;// Function Callradixsort(arr, n);print(arr, n);}}/* This code is contributed by Devesh Agrawal */
1 #include <assert.h>2 #include <limits.h>3 #include <string.h>45 #include "radixsort.h"67/* in-place MSB radix sort for null-terminated strings */89/* helper routine for swapping */10staticvoid11swapStrings(constchar**a,constchar**b)12{13constchar*temp;1415 temp =*a;16*a =*b;17*b = temp;18}1920/* this is the internal routine that assumes all strings are equal for the
21 * first k characters */22staticvoid23radixSortInternal(int n,constchar**a,int k)24{25int i;26int count[UCHAR_MAX+1];/* number of strings with given character in position k */27int mode;/* most common position-k character */28constchar**bucket[UCHAR_MAX+1];/* position of character block in output */29constchar**top[UCHAR_MAX+1];/* first unused index in this character block */3031/* loop implements tail recursion on most common character */32while(n >1){3334/* count occurrences of each character */35memset(count,0,sizeof(int)*(UCHAR_MAX+1));3637for(i =0; i < n; i++){38 count[(unsigned char) a[i][k]]++;39}4041/* find the most common nonzero character */42/* we will handle this specially */43 mode =1;44for(i =2; i <UCHAR_MAX+1; i++){45if(count[i]> count[mode]){46 mode = i;47}48}4950if(count[mode]< n){5152/* generate bucket and top fields */53 bucket[0]= top[0]= a;54for(i =1; i <UCHAR_MAX+1; i++){55 top[i]= bucket[i]= bucket[i-1]+ count[i-1];56}5758/* reorder elements by k-th character */59/* this is similar to dutch flag algorithm */60/* we start at bottom character and swap values out until everything is in place */61/* invariant is that for all i, bucket[i] <= j < top[i] implies a[j][k] == i */62for(i =0; i <UCHAR_MAX+1; i++){63while(top[i]< bucket[i]+ count[i]){64if((unsigned char) top[i][0][k]== i){65/* leave it in place, advance bucket */66 top[i]++;67}else{68/* swap with top of appropriate block */69swapStrings(top[i], top[(unsigned char) top[i][0][k]]++);70}71}72}7374/* we have now reordered everything */75/* recurse on all but 0 and mode */76for(i =1; i <UCHAR_MAX+1; i++){77if(i != mode){78radixSortInternal(count[i], bucket[i], k+1);79}80}8182/* tail recurse on mode */83 n = count[mode];84 a = bucket[mode];85 k = k+1;8687}else{8889/* tail recurse on whole pile */90 k = k+1;91}92}93}9495void96radixSort(int n,constchar**a)97{98radixSortInternal(n, a,0);99}
Usedtosort integer numbers
sort by:
first sorting the first rightmost digit of every number
then move tothe next number tothe left and then sort it (if no such digit exists, then it is 0).
after we're totally done, the numbers will be sorted in order.for example
10,5,4,3,2
in thiscase:if we want tosort these in ascending order
the first sort will result in
10,2,3,4,510 will be the "smallest" number
however, the next sort withthe next digit will put things in proper order
In that case we are sorting
1-0,0-2,0-3,0-4,0-51 is (obviously) the largest of the digits.
So it is in the rightest.
The correct order is now 2,3,4,5,10