From 874940160ac02528b6a8d4a54aa79106759db633 Mon Sep 17 00:00:00 2001 From: nik Date: Mon, 9 Apr 2018 14:04:46 +0300 Subject: [PATCH] Added the default method to sort a list to the sort interface Changes in InsertionSort and CountingSort --- Sorts/InsertionSort.java | 63 -------------------------- Sorts/{ => src/sort}/CountingSort.java | 50 ++++++++++---------- Sorts/src/sort/InsertionSort.java | 61 +++++++++++++++++++++++++ Sorts/src/sort/SortAlgorithm.java | 8 ++++ 4 files changed, 92 insertions(+), 90 deletions(-) delete mode 100644 Sorts/InsertionSort.java rename Sorts/{ => src/sort}/CountingSort.java (70%) create mode 100644 Sorts/src/sort/InsertionSort.java diff --git a/Sorts/InsertionSort.java b/Sorts/InsertionSort.java deleted file mode 100644 index c77b895f..00000000 --- a/Sorts/InsertionSort.java +++ /dev/null @@ -1,63 +0,0 @@ -/** - * - * @author Varun Upadhyay (https://github.com/varunu28) - * - */ - -class InsertionSort { - - /** - * This method implements the Generic Insertion 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 - **/ - - public static > void IS(T array[], int last) { - T key; - for (int j=1;j=0 && key.compareTo(array[i]) < 0) { - array[i+1] = array[i]; - i--; - } - // Placing the key (Card) at its correct position in the sorted subarray - array[i+1] = key; - } - } - - // Driver Program - public static void main(String[] args) { - // Integer Input - int[] arr1 = {4,23,6,78,1,54,231,9,12}; - int last = arr1.length; - Integer[] array = new Integer[arr1.length]; - for (int i=0;i 1 4 6 9 12 23 54 78 231 - for (int i=0;i a b c d e - for(int i=0; i> T[] sort(T[] unsorted) { + return sort(Arrays.asList(unsorted)).toArray(unsorted); + } /** * This method implements the Generic Counting Sort @@ -24,7 +31,8 @@ class CountingSort { * Sorts the list in increasing order * The method uses list elements as keys in the frequency map **/ - public static > List countingSort(List list) { + @Override + public > List sort(List list) { Map frequency = new TreeMap<>(); // The final output array @@ -46,12 +54,12 @@ class CountingSort { /** * Stream Counting Sort - * The same as method {@link CountingSort#countingSort(List)} but this method uses stream API + * The same as method {@link CountingSort#sort(List)} } but this method uses stream API * * @param list The list to be sorted * **/ - public static > List streamCountingSort(List list) { + private static > List streamSort(List list) { return list.stream() .collect(toMap(k -> k, v -> 1, (v1, v2) -> v1 + v2, TreeMap::new)) .entrySet() @@ -64,15 +72,16 @@ class CountingSort { public static void main(String[] args) { // Integer Input List 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:"); - printList(unsortedInts); + print(unsortedInts); // Output => 1 1 4 6 9 9 12 23 23 54 78 231 System.out.println("After Sorting:"); - printList(countingSort(unsortedInts)); + print(countingSort.sort(unsortedInts)); System.out.println("After Sorting By Streams:"); - printList(streamCountingSort(unsortedInts)); + print(streamSort(unsortedInts)); System.out.println("\n------------------------------\n"); @@ -80,27 +89,14 @@ class CountingSort { List unsortedStrings = Stream.of("c", "a", "e", "b","d", "a", "f", "g", "c").collect(toList()); System.out.println("Before Sorting:"); - printList(unsortedStrings); + print(unsortedStrings); //Output => a a b c c d e f g System.out.println("After Sorting:"); - printList(countingSort(unsortedStrings)); + print(countingSort.sort(unsortedStrings)); System.out.println("After Sorting By Streams:"); - printList(streamCountingSort(unsortedStrings)); + print(streamSort(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(); - } } diff --git a/Sorts/src/sort/InsertionSort.java b/Sorts/src/sort/InsertionSort.java new file mode 100644 index 00000000..59361430 --- /dev/null +++ b/Sorts/src/sort/InsertionSort.java @@ -0,0 +1,61 @@ +package sort; + +import static sort.SortUtils.less; +import static sort.SortUtils.print; + +/** + * + * @author Varun Upadhyay (https://github.com/varunu28) + * @author Podshivalov Nikita (https://github.com/nikitap492) + * + */ + +class InsertionSort implements SortAlgorithm { + + /** + * This method implements the Generic Insertion Sort + * Sorts the array in increasing order + * + * @param array The array to be sorted + * + **/ + + @Override + public > T[] sort(T[] array) { + for (int j = 1; j < array.length; j++) { + + // Picking up the key(Card) + T key = array[j]; + int i = j - 1; + + while (i >= 0 && less(key, array[i])) { + array[i + 1] = array[i]; + i--; + } + // Placing the key (Card) at its correct position in the sorted subarray + array[i + 1] = key; + } + return array; + } + + // Driver Program + public static void main(String[] args) { + // Integer Input + Integer[] integers = {4, 23, 6, 78, 1, 54, 231, 9, 12}; + + InsertionSort sort = new InsertionSort(); + + sort.sort(integers); + + // Output => 1 4 6 9 12 23 54 78 231 + print(integers); + + // String Input + String[] strings = {"c", "a", "e", "b","d"}; + + sort.sort(strings); + + //Output => a b c d e + print(strings); + } +} diff --git a/Sorts/src/sort/SortAlgorithm.java b/Sorts/src/sort/SortAlgorithm.java index 5c88ba41..6725d187 100644 --- a/Sorts/src/sort/SortAlgorithm.java +++ b/Sorts/src/sort/SortAlgorithm.java @@ -1,5 +1,8 @@ package sort; +import java.util.Arrays; +import java.util.List; + /** * The common interface of most algorithms * @@ -10,4 +13,9 @@ public interface SortAlgorithm { > T[] sort(T[] unsorted); + @SuppressWarnings("unchecked") + default > List sort(List unsorted){ + return Arrays.asList(sort(unsorted.toArray((T[]) new Comparable[unsorted.size()]))); + } + }