|
1 |
| -# Most Significant Digit / Bit Radix Exchange Sort |
| 1 | +# Most Significant Digit Radix Sort |
2 | 2 |
|
3 | 3 | ---
|
4 | 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> |
| 5 | +## Introduction |
| 6 | +Unlike most sorting algorithms, which are based on *comparison* of whole |
| 7 | +keys, radix sorting is based on examining individual "digits" in the |
| 8 | +keys. |
| 9 | +Most Significant Digit (MSD) Radix Sort (also known as Radix Exchange Sort) |
| 10 | +is a recursive algorithm that sorts an array of keys (here we use integers) |
| 11 | +by focusing on individual bits, starting from the most significant (left |
| 12 | +most) bit of each key and working down to the least significant bit. |
9 | 13 |
|
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 | +## The algorithm |
14 | 15 |
|
15 | 16 | ### 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. |
| 17 | +* The ```Rexsort``` function sets up the sorting process by determining the |
| 18 | +most significant bit position in the array elements. |
| 19 | +* A *mask* is created that has a 1 in this bit position and 0 elsewhere. The |
| 20 | +mask is used to extract bits from keys during sorting. |
| 21 | +* The recursive sorting function ```RexsortRecursive``` is then called to |
| 22 | +sort the entire array starting with this mask. The mask is shifted to the |
| 23 | +right for recursive calls. |
21 | 24 | ### 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. |
| 25 | +* The ```RexsortRecursive``` function works on sub-arrays, defined by a left |
| 26 | +and right index, and additionally has the current mask as a parameter. |
| 27 | +* If the sub-array contains no more than one element or the mask is 0, the |
| 28 | +function simply returns. |
| 29 | +* Otherwise, the array is first *partitioned* into two sub-arrays using the |
| 30 | +mask bit. The |
| 31 | +(smaller) keys with a 0 mask bit are put at the left and (larger) keys with a 1 |
| 32 | +mask bit are put at the right (similar to partition in quicksort). |
| 33 | +* Finally, the two sub-arrays are recursively sorted, using the appropriate |
| 34 | +indices for the sub-arrays boundaries and and the mask shifted one bit |
| 35 | +position to the right. |
26 | 36 |
|
27 | 37 | ### 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. |
| 38 | +* The indices ```i``` and ```j```, are set at the left and right of the array segment. |
| 39 | +* These indices move towards each other, adjusting positions based on the |
| 40 | +mask bit of each key: |
| 41 | + * Increment ```i``` until an element with a 1 as the mask bit is reached |
| 42 | + * Decrement ```j``` until an element with a 0 as the mask bit is reached |
35 | 43 |
|
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. |
| 44 | +* If the two guards have not crossed, perform a swap to ensure that 0s are placed on the left and 1s on the right. |
40 | 45 |
|
41 | 46 | ## Complexity
|
42 | 47 | ### 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)``` |
| 48 | +* Partitioning takes```O(n)```, where ```n``` is the number elements in the |
| 49 | +array. |
| 50 | +* If we *assume the number of bits is limited* the overall time complexity is: ```O(n)``` |
| 51 | +* Note that the best comparison-based sorting algorithms have ```O(n log n)``` |
| 52 | + complexity, but the number of bits is typically related to ```log n``` |
| 53 | + so radix sorts are not necessarily better. |
| 54 | + |
46 | 55 | ### Space Complexity
|
47 |
| -```O(log(largest bit))``` |
| 56 | +Extra space is required for the stack but, unlike quicksort, the stack depth |
| 57 | +is limited by the number of bits, so it is a constant (if the number of bits |
| 58 | +is limited). |
0 commit comments