Skip to content

Commit d3f6b76

Browse files
authored
Merge pull request #78 from kaitinghh/branch-UpdateCountingSort
Branch update counting sort
2 parents 0cff81c + 5d293e4 commit d3f6b76

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ Gradle is used for development.
7979
* [Radix Sort](src/main/java/algorithms/sorting/radixSort) (found in tutorial)
8080
4. Trees
8181
* [Binary search tree](src/main/java/dataStructures/binarySearchTree)
82-
* AVL-tree
82+
* [AVL-tree](src/main/java/dataStructures/avlTree)
8383
* Orthogonal Range Searching
8484
* [Trie](src/main/java/dataStructures/trie)
8585
* [B-Tree](src/main/java/dataStructures/bTree)
22.6 KB
Loading

src/main/java/algorithms/sorting/countingSort/README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@ Counting sort is a non-comparison-based sorting algorithm and isn't bounded by t
66
of most sorting algorithms. <br>
77
It first obtains the frequency map of all elements (i.e. counting the occurrence of every element), then
88
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
1010
encountered. <br>
1111

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+
1218
### Implementation Invariant
1319

1420
**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
3541

3642
## Notes
3743

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.

src/main/java/algorithms/sorting/radixSort/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ In practice, numbers are often interpreted in their binary representation, with
3131
bit chunk of a specified size (usually 8 bits/1 byte, though this number could vary for optimization).
3232

3333
For our implementation, we utilize the binary representation of elements, partitioning them into 8-bit segments.
34-
Given that an integer is typically represented in 32 bits, this results in four segments per integer.
34+
Given that an integer is typically represented in 32 bits, this results in four segments.
3535
By applying the sorting subroutine to each segment across all integers, we can efficiently sort the array.
36-
This method requires sorting the array four times in total, once for each 8-bit segment,
36+
This method requires sorting the array four times in total, once for each 8-bit segment.
3737

3838
### Implementation Invariant
3939
At the end of the *ith* iteration, the elements are sorted based on their numeric value up till the *ith* segment.

0 commit comments

Comments
 (0)