Merge pull request #409 from nikitap492/master

Thanks for contribution
This commit is contained in:
Christian Bender 2018-04-09 15:20:02 +02:00 committed by GitHub
commit 18808175da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,90 +1,106 @@
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.TreeMap; 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 Youssef Ali (https://github.com/youssefAli11997)
* @author Podshivalov Nikita (https://github.com/nikitap492)
* *
*/ */
class CountingSort { class CountingSort {
/** /**
* This method implements the Generic Counting Sort * This method implements the Generic Counting Sort
* *
* @param array The array to be sorted * @param list The list to be sorted
* @param last The count of total number of elements in array *
* Sorts the array in increasing order * Sorts the list in increasing order
* It uses array elements as keys in the frequency map * The method uses list elements as keys in the frequency map
**/ **/
public static <T extends Comparable<T>> List<T> countingSort(List<T> list) {
public static <T extends Comparable<T>> void CS(T[] array, int last) { Map<T, Integer> frequency = new TreeMap<>();
// The final output array
List<T> sortedArray = new ArrayList<>(list.size());
Map<T, Integer> frequency = new TreeMap<T, Integer>(); // Counting the frequency of @param array elements
// The final output array list.forEach(v -> frequency.put(v, frequency.getOrDefault(v, 0) + 1));
ArrayList<T> sortedArray = new ArrayList<T>();
// Counting the frequency of @param array elements // Filling the sortedArray
for(T t : array) { for(Map.Entry<T, Integer> element : frequency.entrySet()) {
try{ for(int j=0; j<element.getValue(); j++){
frequency.put(t, frequency.get(t)+1);
}catch(Exception e){ // new entry
frequency.put(t, 1);
}
}
// Filling the sortedArray
for(Map.Entry<T, Integer> element : frequency.entrySet()) {
for(int j=0; j<element.getValue(); j++)
sortedArray.add(element.getKey()); sortedArray.add(element.getKey());
}
} }
for(int i=0; i<array.length; i++){ return sortedArray;
array[i] = sortedArray.get(i); }
}
/**
* Stream Counting Sort
* The same as method {@link CountingSort#countingSort(List)} but this method uses stream API
*
* @param list The list to be sorted
*
**/
public static <T extends Comparable<T>> List<T> streamCountingSort(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 // Driver Program
public static void main(String[] args) { public static void main(String[] args) {
// Integer Input // Integer Input
Integer[] arr1 = {4,23,6,78,1,54,231,9,12}; List<Integer> unsortedInts = Stream.of(4, 23, 6, 78, 1, 54, 23, 1, 9, 231, 9, 12).collect(toList());
int last = arr1.length;
System.out.println("Before Sorting:"); System.out.println("Before Sorting:");
for (int i=0;i<arr1.length;i++) { printList(unsortedInts);
System.out.print(arr1[i] + " ");
}
System.out.println();
CS(arr1, last); // Output => 1 1 4 6 9 9 12 23 23 54 78 231
// Output => 1 4 6 9 12 23 54 78 231
System.out.println("After Sorting:"); System.out.println("After Sorting:");
for (int i=0;i<arr1.length;i++) { printList(countingSort(unsortedInts));
System.out.print(arr1[i] + " "); System.out.println("After Sorting By Streams:");
} printList(streamCountingSort(unsortedInts));
System.out.println();
System.out.println("------------------------------"); System.out.println("\n------------------------------\n");
// String Input // String Input
String[] array1 = {"c", "a", "e", "b","d"}; List<String> unsortedStrings = Stream.of("c", "a", "e", "b","d", "a", "f", "g", "c").collect(toList());
last = array1.length;
System.out.println("Before Sorting:"); System.out.println("Before Sorting:");
for (int i=0;i<array1.length;i++) { printList(unsortedStrings);
System.out.print(array1[i] + " ");
}
System.out.println();
CS(array1, last); //Output => a a b c c d e f g
//Output => a b c d e
System.out.println("After Sorting:"); System.out.println("After Sorting:");
for(int i=0; i<last; i++) { printList(countingSort(unsortedStrings));
System.out.print(array1[i]+" ");
} System.out.println("After Sorting By Streams:");
printList(streamCountingSort(unsortedStrings));
} }
/**
* Just print list
* @param toPrint - a list which should be printed
*/
private static void printList(List<?> toPrint){
toPrint.stream()
.map(Object::toString)
.map(str -> str + " ")
.forEach(System.out::print);
System.out.println();
}
} }