|
| 1 | +# Straight Radix Sort |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +## Introduction |
| 6 | +Straight Radix Sort (SRS) is a **stable** sorting algorithm that processes each digit or each bit of the binary representation |
| 7 | +of an integer, starting from the least significant digit / bit and working its |
| 8 | +way to the most significant bit / digit. It leverages a non-comparative |
| 9 | +approach, using a **stable** auxiliary sorting algorithm at each stage to reorder the elements by each bit / digit. |
| 10 | + |
| 11 | +## Explanation |
| 12 | +### 1. Initialisation |
| 13 | +The ```Radixsort``` function begins by determining the maximum number of digits, in this case the maximum number of digits |
| 14 | +in the data. This number defines how many iterations the algorithm will go through, |
| 15 | +starting from the least significant bit (LSB-right most) moving toward the most significant bit (MSB-left most). |
| 16 | +### 2. Iteration through Bits |
| 17 | +* Instead of iterating bit by bit, we have decided to process two consecutive bits at a time and sort based on possible |
| 18 | +combinations, i.e., (00, 01, 10, 11), starting from the LSB, performing ```Countingsort``` at each step to ensure the elements |
| 19 | +are sorted based on that bit combination. |
| 20 | +* Since bits are processed as a combination of 2, the number of iterations will be reduced. If the maximum number has ```d``` |
| 21 | +the algorithm will only need to perform ```d/2``` iterations (rounding up if the number of bits is odd). |
| 22 | + |
| 23 | +### 3. Counting Sort |
| 24 | +* Counting occurrences of 00, 01, 10 and 11 |
| 25 | + * For each number in the array, the two-bit value at the current position is extracted. |
| 26 | + * The algorithm **counts** how many times each two-bit combination occurs. |
| 27 | + These counts are stored in an auxiliary array ```C```, which has four slots to count the occurrences of each combination. |
| 28 | +* Cumulative Sum |
| 29 | + * After counting the occurrences of the two-bit combinations, the algorithm computes the cumulative sums of these counts |
| 30 | + in ```C```. This allows the determination of the correct position of each element in the sorted array. |
| 31 | + * The cumulative sum ensures that elements with the same two-bit value are placed next to each other |
| 32 | + in the final sorted array while preserving their original order, i.e., the **stability** |
| 33 | +* Populating the Temporary Array ```B```: |
| 34 | + * ```C[digit]``` determines how many elements have this specific combination, allowing the algorithm to decide where |
| 35 | + to place the current element. |
| 36 | + * After placing an element in ```B```, the algorithm decrements the count in ```C[digit]``` by 1, so the next element |
| 37 | + with the same two-bit combination will be placed in the next available slot. |
| 38 | + |
| 39 | +### 4. Completion |
| 40 | +* Once the sorting based on the current two-bit combination is complete, the temporary array ```B```, which now contains |
| 41 | +the sorted element for the current two-bit combination is copied back to the original array ```A```. |
| 42 | +* The process is repeated for the next two-bit combination until all bits are processed and the array is fully sorted. |
| 43 | + |
| 44 | +## Complexity |
| 45 | +### Time Complexity |
| 46 | +* Each iteration requires ```Countingsort```, which requires ```O(n)```, where n is the number of elements. |
| 47 | +* Since our design processes two bits at a time, the number of iterations is halved, hence, if the maximum number in the |
| 48 | +array has ```d``` bits, the number of iterations is ```O(d/2)``` |
| 49 | +* As a result, the overall time complexity is: ```O(n * d/2)``` -> ```O(n * d)``` |
| 50 | + |
| 51 | +### Space Complexity |
| 52 | +* Original Array ```A```: ```O(n)``` |
| 53 | +* Counting Array ```C```: ```O(1)```, constant space of 4 in this case. |
| 54 | +* Temporary Array ```B```: ```O(n)```, |
| 55 | +* As a result, the overall time complexity is: ```O(n)``` |
| 56 | + |
| 57 | + |
0 commit comments