|
| 1 | +# Most Significant Digit / Bit Radix Exchange Sort |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +## Introduction to Most Significant Digit / Bit Radix Exchange Sort (MSDRexSort) |
| 6 | +The Most Significant Digit/Bit (MSD/MSB) variant of Radix Sort is a recursive algorithm that sorts an array (of integers) |
| 7 | +by focusing on individual bits, starting from the most significant (left most) bit and working down to the least significant |
| 8 | +one as indicated by the binary representation and mask. <br> |
| 9 | + |
| 10 | +By sorting based on the most significant bits first, it ensures that larger or more important bits are considered before |
| 11 | +smaller ones. |
| 12 | + |
| 13 | +## Processes of MSDRexSort |
| 14 | + |
| 15 | +### 1. Initial Setup: |
| 16 | +* The ```Rexsort``` function sets up the sorting process by determining the largest bit position in the array element, |
| 17 | +referred to as the ```mask```. This ```mask``` serves as a reference for which bit is being compared in the following |
| 18 | +steps. |
| 19 | +* The recursive sorting function ```RexsortRecursive``` is then called to sort the entire array starting from the most significant |
| 20 | +bit. |
| 21 | +### 2. Recursive Sorting: |
| 22 | +* The ```RexsortRecursive``` function works on sub-arrays, defined by a left and right index/guard, and recursively divides |
| 23 | +the array based on the current bit position determined by the ```mask```. |
| 24 | +* If the segment contains more than one element, ```i < j``` and the mask is greater than 0, the function proceeds to partition |
| 25 | +the array. |
| 26 | + |
| 27 | +### 3. Partitioning Based on Current Bit |
| 28 | +* The guards ```i``` and ```j```, are set at the left and right of the array segment. |
| 29 | +* These guards move towards each other, adjusting positions based on the bit value at the current ```mask``` position |
| 30 | + i.e., |
| 31 | + * Increment ```i``` until an element with a 1 in the current ```mask``` position is found. |
| 32 | + * Decrement ```j``` until an element with a 0 in the current ```mask``` position is found. |
| 33 | + |
| 34 | +* If the two guards have not crossed, perform a swap. to ensure that 0s and placed on the left and 1s on the right. |
| 35 | + |
| 36 | +### 4. Completion |
| 37 | +Once the array is done with sorting its left and right sub arrays, the ```mask``` is reduced by 1 to shift the focus to |
| 38 | +the next bit. The recursion continues until the entire array is sorted based on all bits, from the most significant bit to |
| 39 | +the least significant bit. |
| 40 | + |
| 41 | +## Complexity |
| 42 | +### Time Complexity |
| 43 | +* ```n``` is the number elements in the array |
| 44 | +* ```log(largest bit)``` is the number of bits required to represent the largest number in the array |
| 45 | +* Hence, overall time complexity is ```O(n * log(largest bit)``` |
| 46 | +### Space Complexity |
| 47 | +```O(log(largest bit))``` |
0 commit comments