Radix Sort in Java: Whiteboard Video Tutorial with Code Walkthrough
Introduction
This 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 animationJava 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);
}
}
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.
Sorting Algorithm Tutorials on JavaBrahman
Bubble SortClick to Read Bubble Sort Tutorial Insertion SortClick to Read Insertion Sort Tutorial Selection SortClick to Read Selection Sort Tutorial Merge SortClick to Read Merge Sort Tutorial Quick SortClick to Read Quick Sort Tutorial Heap SortClick to Read Heap Sort Tutorial Counting SortClick to Read Counting Sort Tutorial Shell SortClick to Read Shell Sort Tutorial Bucket SortClick to Read Bucket Sort Tutorial Radix SortClick to Read Radix Sort Tutorial
Bubble SortClick to Read Bubble Sort Tutorial Insertion SortClick to Read Insertion Sort Tutorial Selection SortClick to Read Selection Sort Tutorial Merge SortClick to Read Merge Sort Tutorial Quick SortClick to Read Quick Sort Tutorial Heap SortClick to Read Heap Sort Tutorial Counting SortClick to Read Counting Sort Tutorial Shell SortClick to Read Shell Sort Tutorial Bucket SortClick to Read Bucket Sort Tutorial Radix SortClick to Read Radix Sort Tutorial