2018-11-14 01:15:47 +08:00
|
|
|
package Sorts;
|
2018-04-09 19:04:46 +08:00
|
|
|
|
|
|
|
import java.util.*;
|
2018-04-06 16:34:52 +08:00
|
|
|
import java.util.stream.IntStream;
|
|
|
|
import java.util.stream.Stream;
|
|
|
|
|
|
|
|
import static java.util.stream.Collectors.toList;
|
|
|
|
import static java.util.stream.Collectors.toMap;
|
2018-11-14 01:15:47 +08:00
|
|
|
import static Sorts.SortUtils.print;
|
2017-10-14 18:52:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author Youssef Ali (https://github.com/youssefAli11997)
|
2018-04-06 16:34:52 +08:00
|
|
|
* @author Podshivalov Nikita (https://github.com/nikitap492)
|
2017-10-14 18:52:55 +08:00
|
|
|
*
|
|
|
|
*/
|
2018-04-09 19:04:46 +08:00
|
|
|
class CountingSort implements SortAlgorithm {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public <T extends Comparable<T>> T[] sort(T[] unsorted) {
|
|
|
|
return sort(Arrays.asList(unsorted)).toArray(unsorted);
|
|
|
|
}
|
2017-10-14 18:52:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This method implements the Generic Counting Sort
|
|
|
|
*
|
2018-04-06 16:34:52 +08:00
|
|
|
* @param list The list to be sorted
|
|
|
|
*
|
|
|
|
* Sorts the list in increasing order
|
|
|
|
* The method uses list elements as keys in the frequency map
|
2017-10-14 18:52:55 +08:00
|
|
|
**/
|
2018-04-09 19:04:46 +08:00
|
|
|
@Override
|
|
|
|
public <T extends Comparable<T>> List<T> sort(List<T> list) {
|
2018-04-06 16:34:52 +08:00
|
|
|
|
|
|
|
Map<T, Integer> frequency = new TreeMap<>();
|
|
|
|
// The final output array
|
|
|
|
List<T> sortedArray = new ArrayList<>(list.size());
|
|
|
|
|
|
|
|
// Counting the frequency of @param array elements
|
|
|
|
list.forEach(v -> frequency.put(v, frequency.getOrDefault(v, 0) + 1));
|
|
|
|
|
|
|
|
// Filling the sortedArray
|
|
|
|
for(Map.Entry<T, Integer> element : frequency.entrySet()) {
|
2018-04-09 14:57:03 +08:00
|
|
|
for(int j=0; j<element.getValue(); j++){
|
2017-10-14 18:52:55 +08:00
|
|
|
sortedArray.add(element.getKey());
|
2018-04-09 14:57:03 +08:00
|
|
|
}
|
2017-10-14 18:52:55 +08:00
|
|
|
}
|
2018-04-06 16:34:52 +08:00
|
|
|
|
|
|
|
return sortedArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stream Counting Sort
|
2018-04-09 19:04:46 +08:00
|
|
|
* The same as method {@link CountingSort#sort(List)} } but this method uses stream API
|
2018-04-06 16:34:52 +08:00
|
|
|
*
|
|
|
|
* @param list The list to be sorted
|
|
|
|
*
|
|
|
|
**/
|
2018-04-09 19:04:46 +08:00
|
|
|
private static <T extends Comparable<T>> List<T> streamSort(List<T> list) {
|
2018-04-06 16:34:52 +08:00
|
|
|
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());
|
2017-10-14 18:52:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Driver Program
|
|
|
|
public static void main(String[] args) {
|
|
|
|
// Integer Input
|
2018-04-06 16:34:52 +08:00
|
|
|
List<Integer> unsortedInts = Stream.of(4, 23, 6, 78, 1, 54, 23, 1, 9, 231, 9, 12).collect(toList());
|
2018-04-09 19:04:46 +08:00
|
|
|
CountingSort countingSort = new CountingSort();
|
2017-10-14 18:52:55 +08:00
|
|
|
|
2018-04-06 16:34:52 +08:00
|
|
|
System.out.println("Before Sorting:");
|
2018-04-09 19:04:46 +08:00
|
|
|
print(unsortedInts);
|
2017-10-14 18:52:55 +08:00
|
|
|
|
2018-04-06 16:34:52 +08:00
|
|
|
// Output => 1 1 4 6 9 9 12 23 23 54 78 231
|
2017-10-14 18:52:55 +08:00
|
|
|
System.out.println("After Sorting:");
|
2018-04-09 19:04:46 +08:00
|
|
|
print(countingSort.sort(unsortedInts));
|
2018-04-06 16:34:52 +08:00
|
|
|
System.out.println("After Sorting By Streams:");
|
2018-04-09 19:04:46 +08:00
|
|
|
print(streamSort(unsortedInts));
|
2018-04-06 16:34:52 +08:00
|
|
|
|
|
|
|
System.out.println("\n------------------------------\n");
|
|
|
|
|
2017-10-14 18:52:55 +08:00
|
|
|
// String Input
|
2018-04-06 16:34:52 +08:00
|
|
|
List<String> unsortedStrings = Stream.of("c", "a", "e", "b","d", "a", "f", "g", "c").collect(toList());
|
2017-10-14 18:52:55 +08:00
|
|
|
|
2018-04-06 16:34:52 +08:00
|
|
|
System.out.println("Before Sorting:");
|
2018-04-09 19:04:46 +08:00
|
|
|
print(unsortedStrings);
|
2017-10-14 18:52:55 +08:00
|
|
|
|
2018-04-06 16:34:52 +08:00
|
|
|
//Output => a a b c c d e f g
|
2017-10-14 18:52:55 +08:00
|
|
|
System.out.println("After Sorting:");
|
2018-04-09 19:04:46 +08:00
|
|
|
print(countingSort.sort(unsortedStrings));
|
2018-04-06 16:34:52 +08:00
|
|
|
|
|
|
|
System.out.println("After Sorting By Streams:");
|
2018-04-09 19:04:46 +08:00
|
|
|
print(streamSort(unsortedStrings));
|
2018-04-06 16:34:52 +08:00
|
|
|
|
2017-10-14 18:52:55 +08:00
|
|
|
}
|
|
|
|
}
|