Radix Sort Algorithm-Whiteboard Video Tutorial with Java 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 animation
Java Code for Radix Sort Algorithm shown in the YouTube video
[su_box title="Radix Sort Java Code shown in the YouTube video" style="soft" box_color="#fcba43" title_color="#00000" radius="4" Class="for-shortcodebox"][java]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);
}
}[/java][/su_box]
OUTPUT of the above code[su_note note_color="#1a1a1a" text_color="#DAD9D7"]5
10
43
54
100
102
287
355[/su_note]
Explanation of the code
→ Watch the Youtube video embedded above for Java Code walk-through and explanation.
[su_note note_color="#fbfbc4" text_color="#000000" radius="4"]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 Radix SortClick to Read Radix Sort Tutorial
[/su_note]