-
Notifications
You must be signed in to change notification settings - Fork 20k
refactor: cleanup CountingSort
#5275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 2 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
5a3a8fd
refactor: cleanup counting sort
d0fb1ba
checkstyle: fix import, adding links
d070a54
fix: extracting logic to separate methods to improve readability
6e0e044
fix: change name from histogram to frequency
0e43795
fix: improve naming
2ea5f55
fix: code improvements
bfffea4
Merge branch 'master' into refactor/counting_sort
alxkm 82aa523
fix: renaming previous counting sort to pseudo counting sort as it is…
2393b57
fix: changing counting sort to keep it with standard implementation
1855072
checkstyle: fix formatting
ada56e0
checkstyle: fix import ordering
863b913
Merge branch 'master' into refactor/counting_sort
alxkm 9ff217e
Merge branch 'master' into refactor/counting_sort
alxkm 0be0b6c
Merge branch 'master' into refactor/counting_sort
alxkm 4d7e8b5
Merge branch 'master' into refactor/counting_sort
alxkm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,49 @@ | ||
package com.thealgorithms.sorts; | ||
|
||
import static com.thealgorithms.sorts.SortUtils.print; | ||
import static java.util.stream.Collectors.toList; | ||
import static java.util.stream.Collectors.toMap; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
import java.util.stream.IntStream; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* CountingSort is a generic implementation of the counting sort algorithm. | ||
* This implementation sorts elements that implement the Comparable interface. | ||
* | ||
* @author Youssef Ali (https://github.com/youssefAli11997) | ||
* @author Podshivalov Nikita (https://github.com/nikitap492) | ||
*/ | ||
class CountingSort implements SortAlgorithm { | ||
|
||
@Override | ||
public <T extends Comparable<T>> T[] sort(T[] unsorted) { | ||
return sort(Arrays.asList(unsorted)).toArray(unsorted); | ||
public <T extends Comparable<T>> T[] sort(T[] array) { | ||
return sort(Arrays.asList(array)).toArray(array); | ||
} | ||
|
||
/** | ||
* This method implements the Generic Counting Sort | ||
* Sorts the provided list using the counting sort algorithm. | ||
* | ||
* @param list The list to be sorted | ||
* <p> | ||
* Sorts the list in increasing order The method uses list elements as keys | ||
* in the frequency map | ||
* @param list The list to be sorted. | ||
* @param <T> The type of elements in the list, must be Comparable. | ||
* @return A sorted list in increasing order. | ||
*/ | ||
@Override | ||
public <T extends Comparable<T>> List<T> sort(List<T> list) { | ||
Map<T, Integer> frequency = new TreeMap<>(); | ||
// The final output array | ||
List<T> sortedArray = new ArrayList<>(list.size()); | ||
// TreeMap to maintain order of elements naturally. | ||
Map<T, Integer> frequencyMap = new TreeMap<>(); | ||
|
||
// Counting the frequency of @param array elements | ||
list.forEach(v -> frequency.put(v, frequency.getOrDefault(v, 0) + 1)); | ||
// Count the frequency of each element in the list. | ||
for (T element : list) { | ||
frequencyMap.put(element, frequencyMap.getOrDefault(element, 0) + 1); | ||
} | ||
|
||
// Filling the sortedArray | ||
for (Map.Entry<T, Integer> element : frequency.entrySet()) { | ||
for (int j = 0; j < element.getValue(); j++) { | ||
sortedArray.add(element.getKey()); | ||
// Create the sorted list based on the frequency map. | ||
List<T> sortedList = new ArrayList<>(list.size()); | ||
for (Map.Entry<T, Integer> entry : frequencyMap.entrySet()) { | ||
alxkm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for (int i = 0; i < entry.getValue(); i++) { | ||
sortedList.add(entry.getKey()); | ||
} | ||
alxkm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return sortedArray; | ||
} | ||
|
||
/** | ||
* Stream Counting Sort The same as method {@link CountingSort#sort(List)} } | ||
* but this method uses stream API | ||
* | ||
* @param list The list to be sorted | ||
*/ | ||
private static <T extends Comparable<T>> List<T> streamSort(List<T> list) { | ||
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()); | ||
} | ||
|
||
// Driver Program | ||
public static void main(String[] args) { | ||
// Integer Input | ||
List<Integer> unsortedInts = Stream.of(4, 23, 6, 78, 1, 54, 23, 1, 9, 231, 9, 12).collect(toList()); | ||
CountingSort countingSort = new CountingSort(); | ||
|
||
System.out.println("Before Sorting:"); | ||
print(unsortedInts); | ||
|
||
// Output => 1 1 4 6 9 9 12 23 23 54 78 231 | ||
System.out.println("After Sorting:"); | ||
print(countingSort.sort(unsortedInts)); | ||
System.out.println("After Sorting By Streams:"); | ||
print(streamSort(unsortedInts)); | ||
|
||
System.out.println("\n------------------------------\n"); | ||
|
||
// String Input | ||
List<String> unsortedStrings = Stream.of("c", "a", "e", "b", "d", "a", "f", "g", "c").collect(toList()); | ||
|
||
System.out.println("Before Sorting:"); | ||
print(unsortedStrings); | ||
|
||
// Output => a a b c c d e f g | ||
System.out.println("After Sorting:"); | ||
print(countingSort.sort(unsortedStrings)); | ||
|
||
System.out.println("After Sorting By Streams:"); | ||
print(streamSort(unsortedStrings)); | ||
return sortedList; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/thealgorithms/sorts/CountingSortUsingStream.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.thealgorithms.sorts; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
import static java.util.stream.Collectors.toMap; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.TreeMap; | ||
import java.util.stream.IntStream; | ||
|
||
public class CountingSortUsingStream implements SortAlgorithm { | ||
@Override | ||
public <T extends Comparable<T>> T[] sort(T[] array) { | ||
return streamSort(Arrays.asList(array)).toArray(array); | ||
} | ||
|
||
/** | ||
* Sorts the provided list using the counting sort algorithm with the Stream API. | ||
* | ||
* @param list The list to be sorted. | ||
* @param <T> The type of elements in the list, must be Comparable. | ||
* @return A sorted list in increasing order. | ||
*/ | ||
private static <T extends Comparable<T>> List<T> streamSort(List<T> list) { | ||
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()); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.thealgorithms.sorts; | ||
|
||
public class CountingSortTest extends SortingAlgorithmTest { | ||
@Override | ||
SortAlgorithm getSortAlgorithm() { | ||
return new CountingSort(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/test/java/com/thealgorithms/sorts/CountingSortUsingStreamTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.thealgorithms.sorts; | ||
|
||
public class CountingSortUsingStreamTest extends SortingAlgorithmTest { | ||
@Override | ||
SortAlgorithm getSortAlgorithm() { | ||
return new CountingSortUsingStream(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.