Implementing Radix Sort Algorithm in C++

Radix sort is a non-comparison integer sorting algorithm that uses the least significant digit first (LSD) approach, sorting numbers digit by digit (units, tens, etc.) without comparing element sizes. Its core idea is to process each digit using a stable counting sort, ensuring that the result of lower-digit sorting remains ordered during higher-digit sorting. Implementation steps: 1. Identify the maximum number in the array to determine the highest number of digits to process; 2. From the lowest digit to the highest, process each digit using counting sort: count the frequency of the current digit, compute positions, place elements stably from back to front, and finally copy back to the original array. In the C++ code, the `countingSort` helper function implements digit-wise sorting (counting frequencies, calculating positions, and stable placement), while the `radixSort` main function loops through each digit. The time complexity is O(d×(n+k)) (where d is the maximum number of digits, n is the array length, and k=10), making it suitable for scenarios with a large range of integers. The core lies in leveraging the stability of counting sort to ensure that the results of lower-digit sorting are not disrupted during higher-digit sorting. Test results show that the sorted array is ordered, verifying the algorithm's effectiveness.

Read More
Implementing the Counting Sort Algorithm in C++

**Counting Sort** is a non-comparison sorting algorithm. Its core idea is to construct a sorted array by counting the occurrences of elements, making it suitable for scenarios where the range of integers is not large (e.g., student scores, ages). **Basic Idea**: Taking the array `[4, 2, 2, 8, 3, 3, 1]` as an example, the steps are: 1. Determine the maximum value (8) and create a count array `count` to statistics the occurrences of each element (e.g., `count[2] = 2`); 2. Insert elements into the result array in the order of the count array to obtain the sorted result `[1, 2, 2, 3, 3, 4, 8]`. **Implementation Key Points**: In C++ code, first find the maximum value, count the occurrences, construct the result array, and copy it back to the original array. Key steps include initializing the count array, counting occurrences, and filling the result array according to the counts. **Complexity**: Time complexity is O(n + k) (where n is the array length and k is the data range), and space complexity is O(k). **Applicable Scenarios**: Non-negative integers with a small range, requiring efficient sorting; negative numbers can be handled by offset conversion (e.g., adding the minimum value). Counting Sort achieves linear-time sorting through the "counting-construction" logic and is ideal for processing small-range integers.

Read More
Implementing the Radix Sort Algorithm with Python

Radix sort is a non-comparative integer sorting algorithm. Its core idea is to distribute elements into buckets and collect them by each digit (from the least significant to the most significant). The basic steps are as follows: first, determine the number of digits of the maximum number in the array. Then, from the least significant digit to the most significant digit, perform "bucket distribution" (10 buckets for digits 0-9) and "collection" operations for each digit. Elements with the same current digit are placed into the same bucket, and the array is updated by collecting them in bucket order until all digits are processed. In Python, this is implemented by looping through the digits, calculating the current digit to distribute into buckets, and then collecting. The time complexity is O(d×(n+k)) (where d is the number of digits of the maximum number, n is the array length, and k=10), and the space complexity is O(n+k). It is suitable for integer arrays with few digits. When handling negative numbers, they can first be converted to positive numbers for sorting and then their signs can be restored.

Read More
Implementing Radix Sort Algorithm in Java

Radix sort is a non-comparison integer sorting algorithm that processes digits from the least significant to the most significant. It distributes each number into "buckets" based on the current digit, then collects them back into the original array in bucket order, repeating until all digits are processed. It is suitable for integers with few digits and has high efficiency. The basic idea is "distribute-collect-repeat": distribute numbers into corresponding buckets by the current digit (units, tens, etc.), collect them back into the array in bucket order, and repeat for all digits. Taking the array [5, 3, 8, 12, 23, 100] as an example, it is sorted after three rounds of processing: units, tens, and hundreds. In Java code, the maximum number determines the highest digit, and `(num / radix) % 10` is used to get the current digit. ArrayLists are used as buckets to implement distribution and collection. The time complexity is O(d(n+k)) (where d is the number of digits of the maximum number and k=10), and the space complexity is O(n+k). This algorithm is stable and suitable for integer sorting. Negative numbers can be separated into positive and negative groups, sorted separately, and then merged.

Read More