Skip to content

Commit 09ee9dc

Browse files
committed
Radix sort background etc
1 parent dfa606a commit 09ee9dc

File tree

3 files changed

+90
-40
lines changed

3 files changed

+90
-40
lines changed

src/algorithms/explanations/StraightRadixSortExp.md

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,59 @@
33
---
44

55
## 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
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. For integers (used here) this could be decimal digits (base 10), bits (base 2) or and other
9+
base. Using bytes (base 256) has efficiency advantages but here we use base 4
10+
(digits 0-3) for illustration.
11+
Straight Radix Sort (also known as Least Significant Digit Radix Sort)
12+
is a *stable* sorting algorithm that processes each digit of the keys
13+
starting from the least significant digit and working its
14+
way to the most significant digit. It uses an
15+
auxiliary sorting algorithm (Counting Sort) at each stage to reorder the
16+
elements by each digit. Importantly, Counting Sort is *stable*, so when sorting
17+
on one digit, if two values are equal, the ordering on previously sorted
18+
digits is retained.
19+
20+
## The algorithm
1221
### 1. Initialisation
1322
The ```Radixsort``` function begins by determining the maximum number of digits, in this case the maximum number of digits
1423
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-
24+
starting from the least significant (right-most) digit moving toward the most
25+
significant (left-most). Here we use base 4 digits (0-3 or two bits)
26+
and show values in binary.
27+
28+
### 2. Outer Loop
29+
For each digit (scanning right to left), *Counting Sort* is performed on that
30+
digit. This leave the array sorted.
31+
2332
### 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.
33+
* Counting occurrences of each digit (00, 01, 10 and 11 in binary)
34+
* For each key in the array, the digit at the current position is extracted.
35+
* The algorithm counts how many times each digit occurs.
36+
* These counts are stored in an auxiliary array ```Count```, which has a slot for each of the four digit values.
2837
* 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.
38+
* After counting the occurrences of the digits, the algorithm computes the cumulative sums of these counts
39+
in ```Count```. This allows the determination of the correct position of each element in the sorted array.
40+
* Keys with digit value 0 will be put in positions 0 to ```Count[0]-1```; those with value 1 in ```Count[0]``` to ```Count[1]-1```, etc.
41+
* Copying to temporary Array ```B```:
42+
* Array ```A``` is scanned right to left, copying keys to ```B```.
43+
* The digit is extracted, ```Count[digit]``` is decremented and then
44+
determines which element of ```B``` the key is copied to.
45+
* The temporary array ```B```, which now contains
46+
the keys sorted on the current digit, is copied back to the original array ```A```.
4347

4448
## Complexity
4549
### 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+
* Each iteration uses ```Countingsort```, which requires ```O(n)```, where n is the number of elements.
51+
* If we *assume the number of digits is limited* the overall time complexity is: ```O(n)```
52+
* Note that the best comparison-based sorting algorithms have ```O(n log n)```
53+
complexity, but the number of digits is typically related to ```log n```
54+
so radix sorts are not necessarily better.
5055

5156
### 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)```
57+
* Due to the temporary array ```B``` we need ```O(n)``` extra space.
58+
* The ```Count``` array is considered constant space (though potentially a
59+
very large radix could be used).
5660

5761

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<style>
2+
a:link {
3+
color: #1e28f0;
4+
}
5+
a:visited{
6+
color: #3c1478;
7+
}
8+
a:hover{
9+
color: #1e288c;
10+
}
11+
</style>
12+
13+
## Extra Info
14+
15+
-----
16+
17+
Geeks for Geeks Link: [**(MSD) Radix Sort**][G4GLink] (also known as Radix Exchange
18+
Sort)
19+
20+
21+
[G4GLink]: https://www.geeksforgeeks.org/msd-most-significant-digit-radix-sort/
22+
23+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<style>
2+
a:link {
3+
color: #1e28f0;
4+
}
5+
a:visited{
6+
color: #3c1478;
7+
}
8+
a:hover{
9+
color: #1e288c;
10+
}
11+
</style>
12+
13+
## Extra Info
14+
15+
-----
16+
17+
Geeks for Geeks Links: [**(Straight/LSD) Radix Sort**][G4GLink] and
18+
[**Counting Sort**][G4GLink2] (also known as Distribution counting)
19+
20+
21+
[G4GLink]: https://www.geeksforgeeks.org/radix-sort/
22+
[G4GLink2]: https://www.geeksforgeeks.org/counting-sort/
23+

0 commit comments

Comments
 (0)