From f564fa4ea14e8e21d146523cedb1a3a52edb0068 Mon Sep 17 00:00:00 2001 From: nik Date: Fri, 6 Apr 2018 11:34:52 +0300 Subject: [PATCH 1/2] Added the counting sort method which uses stream API Changed the original sorting method: creating and throwing an exception it costs then using getOrDefault for a map --- Sorts/CountingSort.java | 130 ++++++++++++++++++++++------------------ 1 file changed, 73 insertions(+), 57 deletions(-) diff --git a/Sorts/CountingSort.java b/Sorts/CountingSort.java index e6828265a..b0b5e7aa5 100644 --- a/Sorts/CountingSort.java +++ b/Sorts/CountingSort.java @@ -1,10 +1,17 @@ import java.util.ArrayList; +import java.util.List; import java.util.Map; import java.util.TreeMap; +import java.util.stream.IntStream; +import java.util.stream.Stream; + +import static java.util.stream.Collectors.toList; +import static java.util.stream.Collectors.toMap; /** * * @author Youssef Ali (https://github.com/youssefAli11997) + * @author Podshivalov Nikita (https://github.com/nikitap492) * */ @@ -13,78 +20,87 @@ class CountingSort { /** * This method implements the Generic Counting Sort * - * @param array The array to be sorted - * @param last The count of total number of elements in array - * Sorts the array in increasing order - * It uses array elements as keys in the frequency map + * @param list The list to be sorted + * + * Sorts the list in increasing order + * The method uses list elements as keys in the frequency map **/ - - public static > void CS(T[] array, int last) { - - Map frequency = new TreeMap(); - // The final output array - ArrayList sortedArray = new ArrayList(); - - // Counting the frequency of @param array elements - for(T t : array) { - try{ - frequency.put(t, frequency.get(t)+1); - }catch(Exception e){ // new entry - frequency.put(t, 1); - } - } - - // Filling the sortedArray - for(Map.Entry element : frequency.entrySet()) { + public static > List CS(List list) { + + Map frequency = new TreeMap<>(); + // The final output array + List 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 element : frequency.entrySet()) { for(int j=0; j> List streamCS(List 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 - Integer[] arr1 = {4,23,6,78,1,54,231,9,12}; - int last = arr1.length; - + List unsortedInts = Stream.of(4, 23, 6, 78, 1, 54, 23, 1, 9, 231, 9, 12).collect(toList()); + System.out.println("Before Sorting:"); - for (int i=0;i 1 4 6 9 12 23 54 78 231 + // Output => 1 1 4 6 9 9 12 23 23 54 78 231 System.out.println("After Sorting:"); - for (int i=0;i unsortedStrings = Stream.of("c", "a", "e", "b","d", "a", "f", "g", "c").collect(toList()); + System.out.println("Before Sorting:"); - for (int i=0;i a b c d e + //Output => a a b c c d e f g System.out.println("After Sorting:"); - for(int i=0; i toPrint){ + toPrint.stream() + .map(Object::toString) + .map(str -> str + " ") + .forEach(System.out::print); + + System.out.println(); } } From 205aec0c4f7640fece153e85347d912e53ea3305 Mon Sep 17 00:00:00 2001 From: nik Date: Mon, 9 Apr 2018 09:57:03 +0300 Subject: [PATCH 2/2] Changed the names of the counting sort methods Added curly braces for the loop --- Sorts/CountingSort.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Sorts/CountingSort.java b/Sorts/CountingSort.java index b0b5e7aa5..e93afb0c2 100644 --- a/Sorts/CountingSort.java +++ b/Sorts/CountingSort.java @@ -14,7 +14,6 @@ import static java.util.stream.Collectors.toMap; * @author Podshivalov Nikita (https://github.com/nikitap492) * */ - class CountingSort { /** @@ -25,7 +24,7 @@ class CountingSort { * Sorts the list in increasing order * The method uses list elements as keys in the frequency map **/ - public static > List CS(List list) { + public static > List countingSort(List list) { Map frequency = new TreeMap<>(); // The final output array @@ -36,8 +35,9 @@ class CountingSort { // Filling the sortedArray for(Map.Entry element : frequency.entrySet()) { - for(int j=0; j> List streamCS(List list) { + public static > List streamCountingSort(List list) { return list.stream() .collect(toMap(k -> k, v -> 1, (v1, v2) -> v1 + v2, TreeMap::new)) .entrySet() @@ -70,9 +70,9 @@ class CountingSort { // Output => 1 1 4 6 9 9 12 23 23 54 78 231 System.out.println("After Sorting:"); - printList(CS(unsortedInts)); + printList(countingSort(unsortedInts)); System.out.println("After Sorting By Streams:"); - printList(streamCS(unsortedInts)); + printList(streamCountingSort(unsortedInts)); System.out.println("\n------------------------------\n"); @@ -84,10 +84,10 @@ class CountingSort { //Output => a a b c c d e f g System.out.println("After Sorting:"); - printList(CS(unsortedStrings)); + printList(countingSort(unsortedStrings)); System.out.println("After Sorting By Streams:"); - printList(streamCS(unsortedStrings)); + printList(streamCountingSort(unsortedStrings)); }