
Radix Sort is a non-comparison-based sorting algorithm that works by sorting digits in a multi-digit number starting from the least significant digit (LSD) to the most significant digit (MSD). It is efficient for sorting integers or strings when the range of digits or characters is small.
Final Sorted Array: [2, 24, 45, 66, 75, 90, 170, 802]
import java.util.Arrays;
public class RadixSort {
// Method to get the maximum value in the array
private static int getMax(int[] arr) {
int max = arr[0];
for (int num : arr) {
if (num > max) {
max = num;
}
}
return max;
}
// Counting sort based on the digit represented by exp
private static void countingSort(int[] arr, int exp) {
int n = arr.length;
int[] output = new int[n];
int[] count = new int[10]; // Digits range from 0-9
// Count occurrences of digits
for (int num : arr) {
count[(num / exp) % 10]++;
}
// Compute cumulative count
for (int i = 1; i < 10; i++) {
count[i] += count[i - 1];
}
// Place elements into the output array in sorted order
for (int i = n - 1; i >= 0; i--) {
int digit = (arr[i] / exp) % 10;
output[count[digit] - 1] = arr[i];
count[digit]--;
}
// Copy sorted values back to the original array
System.arraycopy(output, 0, arr, 0, n);
}
// Main Radix Sort method
public static void radixSort(int[] arr) {
int max = getMax(arr);
// Perform counting sort for each digit (exp: 1, 10, 100, ...)
for (int exp = 1; max / exp > 0; exp *= 10) {
countingSort(arr, exp);
}
}
public static void main(String[] args) {
int[] arr = {170, 45, 75, 90, 802, 24, 2, 66};
System.out.println("Original Array: " + Arrays.toString(arr));
radixSort(arr);
System.out.println("Sorted Array: " + Arrays.toString(arr));
}
}
def counting_sort(arr, exp):
n = len(arr)
output = [0] * n
count = [0] * 10 # Digits range from 0-9
# Count occurrences of digits
for num in arr:
index = (num // exp) % 10
count[index] += 1
# Compute cumulative count
for i in range(1, 10):
count[i] += count[i - 1]
# Place elements into the output array in sorted order
for i in range(n - 1, -1, -1):
index = (arr[i] // exp) % 10
output[count[index] - 1] = arr[i]
count[index] -= 1
# Copy sorted values back to the original array
for i in range(n):
arr[i] = output[i]
def radix_sort(arr):
# Find the maximum value in the array
max_val = max(arr)
# Perform counting sort for each digit (exp: 1, 10, 100, ...)
exp = 1
while max_val // exp > 0:
counting_sort(arr, exp)
exp *= 10
# Example usage
if __name__ == "__main__":
arr = [170, 45, 75, 90, 802, 24, 2, 66]
print("Original Array:", arr)
radix_sort(arr)
print("Sorted Array:", arr)
Radix Sort is a powerful tool for specific scenarios, offering an efficient alternative to traditional comparison-based algorithms.
When analyzing a stock, one of the first financial indicators you’ll encounter is EPS, or Earnings Per Share. It’s one… Read More
When you look at a stock’s profile on a financial website, one of the first things you’ll see is its… Read More
In the world of open-source software, simplicity and flexibility are often just as important as legal protection. That’s why the… Read More
If you want your software to be open source, but still compatible with commercial use—and not as restrictive as the… Read More
When it comes to open-source software, developers and businesses alike need licenses that balance freedom, legal clarity, and long-term security.… Read More
If you’re working on open-source projects or choosing third-party libraries for your software, understanding software licenses is essential. Among the… Read More