JB Header
Radix Sort Algorithm-Whiteboard Video Tutorial with Java Code Walkthrough
IntroductionThis is a YouTube video tutorial showing the working of Radix Sort Algorithm with whiteboard animation. It also does a detailed code walk-through of Radix Sort implementation in Java. The code shown in the video is present below on this page. YouTube Video explaining Radix Sort Algorithm's working using whiteboard animation
Java Code for Radix Sort Algorithm shown in the YouTube video
Radix Sort Java Code shown in the YouTube video
package com.javabrahman.algorithms.sorts;
import java.util.ArrayList;
import java.util.Arrays;
public class RadixSort {
  public static void main(String args[]) {
    int inputArray[] = {100, 54, 355, 102, 43, 10, 287, 005};
    final int RADIX = 10;

    ArrayList<Integer> bucketsArray[] = new ArrayList[RADIX];
    for (int count = 0; count < bucketsArray.length; count++) {
      bucketsArray[count] = new ArrayList<>();
    }

    boolean maxDigitsLengthReached = false;
    int temp = -1, placeValue = 1;
    while (!maxDigitsLengthReached) {
      maxDigitsLengthReached = true;
      for (Integer element : inputArray) {
        temp = element / placeValue;
        bucketsArray[temp % RADIX].add(element);
        if (maxDigitsLengthReached && temp > 0) {
          maxDigitsLengthReached = false;
        }
      }
      int a = 0;
      for (int b = 0; b < RADIX; b++) {
        for (Integer i : bucketsArray[b]) {
          inputArray[a++] = i;
        }
        bucketsArray[b].clear();
      }
      placeValue = placeValue * RADIX;
    }
    Arrays.stream(inputArray).forEach(System.out::println);
  }
}
 OUTPUT of the above code
5
10
43
54
100
102
287
355
Explanation of the code

Watch the Youtube video embedded above for Java Code walk-through and explanation.