Added the default method to sort a list to the sort interface

Changes in InsertionSort and CountingSort
This commit is contained in:
nik
2018-04-09 14:04:46 +03:00
parent 5560be8262
commit 874940160a
4 changed files with 92 additions and 90 deletions

View File

@ -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 <T extends Comparable<T>> void IS(T array[], int last) {
T key;
for (int j=1;j<last;j++) {
// Picking up the key(Card)
key = array[j];
int i = j-1;
while (i>=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<arr1.length;i++) {
array[i] = arr1[i];
}
IS(array, last);
// Output => 1 4 6 9 12 23 54 78 231
for (int i=0;i<array.length;i++) {
System.out.print(array[i] + " ");
}
System.out.println();
// String Input
String[] array1 = {"c", "a", "e", "b","d"};
last = array1.length;
IS(array1, last);
//Output => a b c d e
for(int i=0; i<last; i++)
{
System.out.print(array1[i]+"\t");
}
}
}

View File

@ -1,12 +1,12 @@
import java.util.ArrayList; package sort;
import java.util.List;
import java.util.Map; import java.util.*;
import java.util.TreeMap;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import java.util.stream.Stream; import java.util.stream.Stream;
import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap; import static java.util.stream.Collectors.toMap;
import static sort.SortUtils.print;
/** /**
* *
@ -14,7 +14,14 @@ import static java.util.stream.Collectors.toMap;
* @author Podshivalov Nikita (https://github.com/nikitap492) * @author Podshivalov Nikita (https://github.com/nikitap492)
* *
*/ */
class CountingSort { class CountingSort implements SortAlgorithm {
@Override
public <T extends Comparable<T>> T[] sort(T[] unsorted) {
return sort(Arrays.asList(unsorted)).toArray(unsorted);
}
/** /**
* This method implements the Generic Counting Sort * This method implements the Generic Counting Sort
@ -24,7 +31,8 @@ class CountingSort {
* Sorts the list in increasing order * Sorts the list in increasing order
* The method uses list 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) { @Override
public <T extends Comparable<T>> List<T> sort(List<T> list) {
Map<T, Integer> frequency = new TreeMap<>(); Map<T, Integer> frequency = new TreeMap<>();
// The final output array // The final output array
@ -46,12 +54,12 @@ class CountingSort {
/** /**
* Stream Counting Sort * 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 * @param list The list to be sorted
* *
**/ **/
public static <T extends Comparable<T>> List<T> streamCountingSort(List<T> list) { 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)) .collect(toMap(k -> k, v -> 1, (v1, v2) -> v1 + v2, TreeMap::new))
.entrySet() .entrySet()
@ -64,15 +72,16 @@ class CountingSort {
public static void main(String[] args) { public static void main(String[] args) {
// Integer Input // Integer Input
List<Integer> unsortedInts = Stream.of(4, 23, 6, 78, 1, 54, 23, 1, 9, 231, 9, 12).collect(toList()); List<Integer> 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:"); System.out.println("Before Sorting:");
printList(unsortedInts); print(unsortedInts);
// Output => 1 1 4 6 9 9 12 23 23 54 78 231 // Output => 1 1 4 6 9 9 12 23 23 54 78 231
System.out.println("After Sorting:"); System.out.println("After Sorting:");
printList(countingSort(unsortedInts)); print(countingSort.sort(unsortedInts));
System.out.println("After Sorting By Streams:"); System.out.println("After Sorting By Streams:");
printList(streamCountingSort(unsortedInts)); print(streamSort(unsortedInts));
System.out.println("\n------------------------------\n"); System.out.println("\n------------------------------\n");
@ -80,27 +89,14 @@ class CountingSort {
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:"); System.out.println("Before Sorting:");
printList(unsortedStrings); print(unsortedStrings);
//Output => a a b c c d e f g //Output => a a b c c d e f g
System.out.println("After Sorting:"); System.out.println("After Sorting:");
printList(countingSort(unsortedStrings)); print(countingSort.sort(unsortedStrings));
System.out.println("After Sorting By Streams:"); 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();
}
} }

View File

@ -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 extends Comparable<T>> 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);
}
}

View File

@ -1,5 +1,8 @@
package sort; package sort;
import java.util.Arrays;
import java.util.List;
/** /**
* The common interface of most algorithms * The common interface of most algorithms
* *
@ -10,4 +13,9 @@ public interface SortAlgorithm {
<T extends Comparable<T>> T[] sort(T[] unsorted); <T extends Comparable<T>> T[] sort(T[] unsorted);
@SuppressWarnings("unchecked")
default <T extends Comparable<T>> List<T> sort(List<T> unsorted){
return Arrays.asList(sort(unsorted.toArray((T[]) new Comparable[unsorted.size()])));
}
} }