mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-23 12:35:55 +08:00
docs: update the whole repository
* fix some bugs * delete duplicate files * format code
This commit is contained in:
@ -9,15 +9,11 @@ import static java.util.stream.Collectors.toMap;
|
||||
import static Sorts.SortUtils.print;
|
||||
|
||||
/**
|
||||
*
|
||||
* @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);
|
||||
@ -27,9 +23,9 @@ class CountingSort implements SortAlgorithm {
|
||||
* This method implements the Generic Counting Sort
|
||||
*
|
||||
* @param list The list to be sorted
|
||||
*
|
||||
* Sorts the list in increasing order
|
||||
* The method uses list elements as keys in the frequency map
|
||||
* <p>
|
||||
* Sorts the list in increasing order
|
||||
* The method uses list elements as keys in the frequency map
|
||||
**/
|
||||
@Override
|
||||
public <T extends Comparable<T>> List<T> sort(List<T> list) {
|
||||
@ -42,8 +38,8 @@ class CountingSort implements SortAlgorithm {
|
||||
list.forEach(v -> frequency.put(v, frequency.getOrDefault(v, 0) + 1));
|
||||
|
||||
// Filling the sortedArray
|
||||
for(Map.Entry<T, Integer> element : frequency.entrySet()) {
|
||||
for(int j=0; j<element.getValue(); j++){
|
||||
for (Map.Entry<T, Integer> element : frequency.entrySet()) {
|
||||
for (int j = 0; j < element.getValue(); j++) {
|
||||
sortedArray.add(element.getKey());
|
||||
}
|
||||
}
|
||||
@ -57,10 +53,9 @@ class CountingSort implements SortAlgorithm {
|
||||
* 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()
|
||||
return list.stream()
|
||||
.collect(toMap(k -> k, v -> 1, (v1, v2) -> v1 + v2, TreeMap::new))
|
||||
.entrySet()
|
||||
.stream()
|
||||
@ -86,7 +81,7 @@ class CountingSort implements SortAlgorithm {
|
||||
System.out.println("\n------------------------------\n");
|
||||
|
||||
// String Input
|
||||
List<String> unsortedStrings = Stream.of("c", "a", "e", "b","d", "a", "f", "g", "c").collect(toList());
|
||||
List<String> unsortedStrings = Stream.of("c", "a", "e", "b", "d", "a", "f", "g", "c").collect(toList());
|
||||
|
||||
System.out.println("Before Sorting:");
|
||||
print(unsortedStrings);
|
||||
|
Reference in New Issue
Block a user