Skip to content

Commit 1525ae6

Browse files
initial commit
1 parent e976b69 commit 1525ae6

2 files changed

Lines changed: 189 additions & 0 deletions

File tree

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
2+
Given the array:
3+
```
4+
[38, 27, 43, 3, 9, 82, 10]
5+
```
6+
7+
## Step-by-Step Simulation on Ones Place Value
8+
9+
### Counting Occurrences by Ones Place
10+
11+
1. **Count Array Initialization:**
12+
```
13+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
14+
```
15+
16+
2. **Number: 38, Ones Digit: 8 -> Count[8]++**
17+
```
18+
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
19+
```
20+
21+
3. **Number: 27, Ones Digit: 7 -> Count[7]++**
22+
```
23+
[0, 0, 0, 0, 0, 0, 0, 1, 1, 0]
24+
```
25+
26+
4. **Number: 43, Ones Digit: 3 -> Count[3]++**
27+
```
28+
[0, 0, 0, 1, 0, 0, 0, 1, 1, 0]
29+
```
30+
31+
5. **Number: 3, Ones Digit: 3 -> Count[3]++**
32+
```
33+
[0, 0, 0, 2, 0, 0, 0, 1, 1, 0]
34+
```
35+
36+
6. **Number: 9, Ones Digit: 9 -> Count[9]++**
37+
```
38+
[0, 0, 0, 2, 0, 0, 0, 1, 1, 1]
39+
```
40+
41+
7. **Number: 82, Ones Digit: 2 -> Count[2]++**
42+
```
43+
[0, 0, 1, 2, 0, 0, 0, 1, 1, 1]
44+
```
45+
46+
8. **Number: 10, Ones Digit: 0 -> Count[0]++**
47+
```
48+
[1, 0, 1, 2, 0, 0, 0, 1, 1, 1]
49+
```
50+
51+
### Transforming to Cumulative Count Array
52+
53+
1. **Count[1] = Count[1] + Count[0] = 0 + 1 = 1**
54+
```
55+
[1, 1, 1, 2, 0, 0, 0, 1, 1, 1]
56+
```
57+
58+
2. **Count[2] = Count[2] + Count[1] = 1 + 1 = 2**
59+
```
60+
[1, 1, 2, 2, 0, 0, 0, 1, 1, 1]
61+
```
62+
63+
3. **Count[3] = Count[3] + Count[2] = 2 + 2 = 4**
64+
```
65+
[1, 1, 2, 4, 0, 0, 0, 1, 1, 1]
66+
```
67+
68+
4. **Count[4] = Count[4] + Count[3] = 0 + 4 = 4**
69+
```
70+
[1, 1, 2, 4, 4, 0, 0, 1, 1, 1]
71+
```
72+
73+
5. **Count[5] = Count[5] + Count[4] = 0 + 4 = 4**
74+
```
75+
[1, 1, 2, 4, 4, 4, 0, 1, 1, 1]
76+
```
77+
78+
6. **Count[6] = Count[6] + Count[5] = 0 + 4 = 4**
79+
```
80+
[1, 1, 2, 4, 4, 4, 4, 1, 1, 1]
81+
```
82+
83+
7. **Count[7] = Count[7] + Count[6] = 1 + 4 = 5**
84+
```
85+
[1, 1, 2, 4, 4, 4, 4, 5, 1, 1]
86+
```
87+
88+
8. **Count[8] = Count[8] + Count[7] = 1 + 5 = 6**
89+
```
90+
[1, 1, 2, 4, 4, 4, 4, 5, 6, 1]
91+
```
92+
93+
9. **Count[9] = Count[9] + Count[8] = 1 + 6 = 7**
94+
```
95+
[1, 1, 2, 4, 4, 4, 4, 5, 6, 7]
96+
```
97+
98+
### Building the Output Array
99+
100+
1. **Output Array:**
101+
```
102+
[0, 0, 0, 0, 0, 0, 0]
103+
```
104+
105+
2. **Place Number: 10, Ones Digit: 0**
106+
- Place 10 at index `count[0] - 1 = 0`
107+
- Decrement `count[0]`
108+
```
109+
Output Array: [10, 0, 0, 0, 0, 0, 0]
110+
Count Array: [0, 1, 2, 4, 4, 4, 4, 5, 6, 7]
111+
```
112+
113+
3. **Place Number: 82, Ones Digit: 2**
114+
- Place 82 at index `count[2] - 1 = 1`
115+
- Decrement `count[2]`
116+
```
117+
Output Array: [10, 82, 0, 0, 0, 0, 0]
118+
Count Array: [0, 1, 1, 4, 4, 4, 4, 5, 6, 7]
119+
```
120+
121+
4. **Place Number: 9, Ones Digit: 9**
122+
- Place 9 at index `count[9] - 1 = 6`
123+
- Decrement `count[9]`
124+
```
125+
Output Array: [10, 82, 0, 0, 0, 0, 9]
126+
Count Array: [0, 1, 1, 4, 4, 4, 4, 5, 6, 6]
127+
```
128+
129+
5. **Place Number: 3, Ones Digit: 3**
130+
- Place 3 at index `count[3] - 1 = 3`
131+
- Decrement `count[3]`
132+
```
133+
Output Array: [10, 82, 0, 3, 0, 0, 9]
134+
Count Array: [0, 1, 1, 3, 4, 4, 4, 5, 6, 6]
135+
```
136+
137+
6. **Place Number: 43, Ones Digit: 3**
138+
- Place 43 at index `count[3] - 1 = 2`
139+
- Decrement `count[3]`
140+
```
141+
Output Array: [10, 82, 43, 3, 0, 0, 9]
142+
Count Array: [0, 1, 1, 2, 4, 4, 4, 5, 6, 6]
143+
```
144+
145+
7. **Place Number: 27, Ones Digit: 7**
146+
- Place 27 at index `count[7] - 1 = 4`
147+
- Decrement `count[7]`
148+
```
149+
Output Array: [10, 82, 43, 3, 27, 0, 9]
150+
Count Array: [0, 1, 1, 2, 4, 4, 4, 4, 6, 6]
151+
```
152+
153+
8. **Place Number: 38, Ones Digit: 8**
154+
- Place 38 at index `count[8] - 1 = 5`
155+
- Decrement `count[8]`
156+
```
157+
Output Array: [10, 82, 43, 3, 27, 38, 9]
158+
Count Array: [0, 1, 1, 2, 4, 4, 4, 4, 5, 6]
159+
```
160+
161+
### Sorted by Ones Place
162+
163+
The array sorted by the ones place is:
164+
165+
```
166+
[10, 82, 43, 3, 27, 38, 9]
167+
```
168+
169+
### Recap of Radix Sort Steps
170+
171+
1. **Sort by Least Significant Digit (Ones Place):**
172+
```
173+
[10, 82, 43, 3, 27, 38, 9]
174+
```
175+
176+
2. **Sort by Next Significant Digit (Tens Place):**
177+
```
178+
[3, 9, 10, 27, 38, 43, 82]
179+
```
180+
181+
### Overview of Each Step and Keyword
182+
183+
- **Digit Position:** Each iteration focuses on sorting elements based on a specific digit position, starting from the least significant to the most significant.
184+
- **
185+
186+
**Counting Sort:** Counting sort is applied at each digit level, ensuring the algorithm is stable and correctly sorts elements at each step.
187+
- **Cumulative Count:** The cumulative count array helps place elements in the correct position in the output array.
188+
- **Stability:** Ensures that elements with the same digit values appear in the same order as they were in the input array.
189+

‎Algorithm/Sorting algorithm/radix-sort/main.go‎

Whitespace-only changes.

0 commit comments

Comments
 (0)