|
1 | 1 | package com.thealgorithms.sorts;
|
2 | 2 |
|
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 |
| - |
7 | 3 | import java.util.ArrayList;
|
8 | 4 | import java.util.Arrays;
|
9 | 5 | import java.util.List;
|
10 | 6 | import java.util.Map;
|
11 | 7 | import java.util.TreeMap;
|
12 |
| -import java.util.stream.IntStream; |
13 |
| -import java.util.stream.Stream; |
14 | 8 |
|
15 | 9 | /**
|
| 10 | + * CountingSort is a generic implementation of the counting sort algorithm. |
| 11 | + * This implementation sorts elements that implement the Comparable interface. |
| 12 | + * |
16 | 13 | * @author Youssef Ali (https://github.com/youssefAli11997)
|
17 | 14 | * @author Podshivalov Nikita (https://github.com/nikitap492)
|
18 | 15 | */
|
19 | 16 | class CountingSort implements SortAlgorithm {
|
20 |
| - |
21 | 17 | @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); |
24 | 20 | }
|
25 | 21 |
|
26 | 22 | /**
|
27 |
| - * This method implements the Generic Counting Sort |
| 23 | + * Sorts the provided list using the counting sort algorithm. |
28 | 24 | *
|
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. |
33 | 28 | */
|
34 | 29 | @Override
|
35 | 30 | 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<>(); |
39 | 33 |
|
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 | + } |
42 | 38 |
|
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()); |
47 | 44 | }
|
48 | 45 | }
|
49 | 46 |
|
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; |
92 | 48 | }
|
93 | 49 | }
|
0 commit comments