Skip to content

Commit 5a3a8fd

Browse files
author
Alex Klymenko
committed
refactor: cleanup counting sort
1 parent 26b4b82 commit 5a3a8fd

File tree

5 files changed

+65
-65
lines changed

5 files changed

+65
-65
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@
496496
* [CocktailShakerSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/CocktailShakerSort.java)
497497
* [CombSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/CombSort.java)
498498
* [CountingSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/CountingSort.java)
499+
* [CountingSortUsingStream](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/CountingSortUsingStream.java)
499500
* [CycleSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/CycleSort.java)
500501
* [DNFSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/DNFSort.java)
501502
* [DualPivotQuickSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/DualPivotQuickSort.java)
Lines changed: 21 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,49 @@
11
package com.thealgorithms.sorts;
22

3-
import static com.thealgorithms.sorts.SortUtils.print;
4-
import static java.util.stream.Collectors.toList;
5-
import static java.util.stream.Collectors.toMap;
6-
73
import java.util.ArrayList;
84
import java.util.Arrays;
95
import java.util.List;
106
import java.util.Map;
117
import java.util.TreeMap;
12-
import java.util.stream.IntStream;
13-
import java.util.stream.Stream;
148

159
/**
10+
* CountingSort is a generic implementation of the counting sort algorithm.
11+
* This implementation sorts elements that implement the Comparable interface.
12+
*
1613
* @author Youssef Ali (https://github.com/youssefAli11997)
1714
* @author Podshivalov Nikita (https://github.com/nikitap492)
1815
*/
1916
class CountingSort implements SortAlgorithm {
20-
2117
@Override
22-
public <T extends Comparable<T>> T[] sort(T[] unsorted) {
23-
return sort(Arrays.asList(unsorted)).toArray(unsorted);
18+
public <T extends Comparable<T>> T[] sort(T[] array) {
19+
return sort(Arrays.asList(array)).toArray(array);
2420
}
2521

2622
/**
27-
* This method implements the Generic Counting Sort
23+
* Sorts the provided list using the counting sort algorithm.
2824
*
29-
* @param list The list to be sorted
30-
* <p>
31-
* Sorts the list in increasing order The method uses list elements as keys
32-
* in the frequency map
25+
* @param list The list to be sorted.
26+
* @param <T> The type of elements in the list, must be Comparable.
27+
* @return A sorted list in increasing order.
3328
*/
3429
@Override
3530
public <T extends Comparable<T>> List<T> sort(List<T> list) {
36-
Map<T, Integer> frequency = new TreeMap<>();
37-
// The final output array
38-
List<T> sortedArray = new ArrayList<>(list.size());
31+
// TreeMap to maintain order of elements naturally.
32+
Map<T, Integer> frequencyMap = new TreeMap<>();
3933

40-
// Counting the frequency of @param array elements
41-
list.forEach(v -> frequency.put(v, frequency.getOrDefault(v, 0) + 1));
34+
// Count the frequency of each element in the list.
35+
for (T element : list) {
36+
frequencyMap.put(element, frequencyMap.getOrDefault(element, 0) + 1);
37+
}
4238

43-
// Filling the sortedArray
44-
for (Map.Entry<T, Integer> element : frequency.entrySet()) {
45-
for (int j = 0; j < element.getValue(); j++) {
46-
sortedArray.add(element.getKey());
39+
// Create the sorted list based on the frequency map.
40+
List<T> sortedList = new ArrayList<>(list.size());
41+
for (Map.Entry<T, Integer> entry : frequencyMap.entrySet()) {
42+
for (int i = 0; i < entry.getValue(); i++) {
43+
sortedList.add(entry.getKey());
4744
}
4845
}
4946

50-
return sortedArray;
51-
}
52-
53-
/**
54-
* Stream Counting Sort The same as method {@link CountingSort#sort(List)} }
55-
* but this method uses stream API
56-
*
57-
* @param list The list to be sorted
58-
*/
59-
private static <T extends Comparable<T>> List<T> streamSort(List<T> list) {
60-
return list.stream().collect(toMap(k -> k, v -> 1, (v1, v2) -> v1 + v2, TreeMap::new)).entrySet().stream().flatMap(entry -> IntStream.rangeClosed(1, entry.getValue()).mapToObj(t -> entry.getKey())).collect(toList());
61-
}
62-
63-
// Driver Program
64-
public static void main(String[] args) {
65-
// Integer Input
66-
List<Integer> unsortedInts = Stream.of(4, 23, 6, 78, 1, 54, 23, 1, 9, 231, 9, 12).collect(toList());
67-
CountingSort countingSort = new CountingSort();
68-
69-
System.out.println("Before Sorting:");
70-
print(unsortedInts);
71-
72-
// Output => 1 1 4 6 9 9 12 23 23 54 78 231
73-
System.out.println("After Sorting:");
74-
print(countingSort.sort(unsortedInts));
75-
System.out.println("After Sorting By Streams:");
76-
print(streamSort(unsortedInts));
77-
78-
System.out.println("\n------------------------------\n");
79-
80-
// String Input
81-
List<String> unsortedStrings = Stream.of("c", "a", "e", "b", "d", "a", "f", "g", "c").collect(toList());
82-
83-
System.out.println("Before Sorting:");
84-
print(unsortedStrings);
85-
86-
// Output => a a b c c d e f g
87-
System.out.println("After Sorting:");
88-
print(countingSort.sort(unsortedStrings));
89-
90-
System.out.println("After Sorting By Streams:");
91-
print(streamSort(unsortedStrings));
47+
return sortedList;
9248
}
9349
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.thealgorithms.sorts;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.TreeMap;
6+
import java.util.stream.IntStream;
7+
8+
import static java.util.stream.Collectors.toList;
9+
import static java.util.stream.Collectors.toMap;
10+
11+
public class CountingSortUsingStream implements SortAlgorithm {
12+
@Override
13+
public <T extends Comparable<T>> T[] sort(T[] array) {
14+
return streamSort(Arrays.asList(array)).toArray(array);
15+
}
16+
17+
/**
18+
* Sorts the provided list using the counting sort algorithm with the Stream API.
19+
*
20+
* @param list The list to be sorted.
21+
* @param <T> The type of elements in the list, must be Comparable.
22+
* @return A sorted list in increasing order.
23+
*/
24+
private static <T extends Comparable<T>> List<T> streamSort(List<T> list) {
25+
return list.stream().collect(toMap(k -> k, v -> 1, Integer::sum, TreeMap::new)).entrySet().stream().flatMap(entry -> IntStream.rangeClosed(1, entry.getValue()).mapToObj(t -> entry.getKey())).collect(toList());
26+
}
27+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.thealgorithms.sorts;
2+
3+
public class CountingSortTest extends SortingAlgorithmTest {
4+
@Override
5+
SortAlgorithm getSortAlgorithm() {
6+
return new CountingSort();
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.thealgorithms.sorts;
2+
3+
public class CountingSortUsingStreamTest extends SortingAlgorithmTest {
4+
@Override
5+
SortAlgorithm getSortAlgorithm() {
6+
return new CountingSortUsingStream();
7+
}
8+
}

0 commit comments

Comments
 (0)