@@ -6,9 +6,15 @@ Counting sort is a non-comparison-based sorting algorithm and isn't bounded by t
6
6
of most sorting algorithms. <br >
7
7
It first obtains the frequency map of all elements (i.e. counting the occurrence of every element), then
8
8
computes the prefix sum for the map. This prefix map tells us which position an element should be inserted.
9
- It is updated after each insertion to reflection the new position to insert the next time the same element is
9
+ It is updated after each insertion to reflect the new position to insert the next time the same element is
10
10
encountered. <br >
11
11
12
+ ![ counting sort img] ( ../../../../../../docs/assets/images/CountingSort.png )
13
+
14
+ Image Source: https://www.oreilly.com/library/view/mastering-algorithms-with/1565924533/ch12s13.html
15
+
16
+ _ To align with the naming convention of our implementation, data => arr, counts => freq, temp => sorted._
17
+
12
18
### Implementation Invariant
13
19
14
20
** At the end of the ith iteration, the ith element (of the original array) from the back will be placed in
@@ -35,5 +41,7 @@ Counting sort is NOT AN IN-PLACE algorithm. For one, it requires additional spac
35
41
36
42
## Notes
37
43
38
- 1 . Counting sort (stable version) is often used as a sub-routine for radix sort.
39
- 2 . Supplementary: Here is a [ video] ( https://www.youtube.com/watch?v=OKd534EWcdk ) if you are still having troubles.
44
+ 1 . Our counting sort implementation works only on non-negative integers. However, to adapt it to work for arrays with
45
+ negative integers, we can add an offset of +m, where m is the smallest integer in the array.
46
+ 2 . Counting sort (stable version) is often used as a sub-routine for radix sort.
47
+ 3 . Supplementary: Here is a [ video] ( https://www.youtube.com/watch?v=OKd534EWcdk ) if you are still having troubles.
0 commit comments