Skip to content

Commit 8749401

Browse files
committed
Added the default method to sort a list to the sort interface
Changes in InsertionSort and CountingSort
1 parent 5560be8 commit 8749401

File tree

4 files changed

+92
-90
lines changed

4 files changed

+92
-90
lines changed

Sorts/InsertionSort.java

Lines changed: 0 additions & 63 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1-
import java.util.ArrayList;
2-
import java.util.List;
3-
import java.util.Map;
4-
import java.util.TreeMap;
1+
package sort;
2+
3+
import java.util.*;
54
import java.util.stream.IntStream;
65
import java.util.stream.Stream;
76

87
import static java.util.stream.Collectors.toList;
98
import static java.util.stream.Collectors.toMap;
9+
import static sort.SortUtils.print;
1010

1111
/**
1212
*
1313
* @author Youssef Ali (https://github.com/youssefAli11997)
1414
* @author Podshivalov Nikita (https://github.com/nikitap492)
1515
*
1616
*/
17-
class CountingSort {
17+
class CountingSort implements SortAlgorithm {
18+
19+
20+
21+
@Override
22+
public <T extends Comparable<T>> T[] sort(T[] unsorted) {
23+
return sort(Arrays.asList(unsorted)).toArray(unsorted);
24+
}
1825

1926
/**
2027
* This method implements the Generic Counting Sort
@@ -24,7 +31,8 @@ class CountingSort {
2431
* Sorts the list in increasing order
2532
* The method uses list elements as keys in the frequency map
2633
**/
27-
public static <T extends Comparable<T>> List<T> countingSort(List<T> list) {
34+
@Override
35+
public <T extends Comparable<T>> List<T> sort(List<T> list) {
2836

2937
Map<T, Integer> frequency = new TreeMap<>();
3038
// The final output array
@@ -46,12 +54,12 @@ public static <T extends Comparable<T>> List<T> countingSort(List<T> list) {
4654

4755
/**
4856
* Stream Counting Sort
49-
* The same as method {@link CountingSort#countingSort(List)} but this method uses stream API
57+
* The same as method {@link CountingSort#sort(List)} } but this method uses stream API
5058
*
5159
* @param list The list to be sorted
5260
*
5361
**/
54-
public static <T extends Comparable<T>> List<T> streamCountingSort(List<T> list) {
62+
private static <T extends Comparable<T>> List<T> streamSort(List<T> list) {
5563
return list.stream()
5664
.collect(toMap(k -> k, v -> 1, (v1, v2) -> v1 + v2, TreeMap::new))
5765
.entrySet()
@@ -64,43 +72,31 @@ public static <T extends Comparable<T>> List<T> streamCountingSort(List<T> list)
6472
public static void main(String[] args) {
6573
// Integer Input
6674
List<Integer> unsortedInts = Stream.of(4, 23, 6, 78, 1, 54, 23, 1, 9, 231, 9, 12).collect(toList());
75+
CountingSort countingSort = new CountingSort();
6776

6877
System.out.println("Before Sorting:");
69-
printList(unsortedInts);
78+
print(unsortedInts);
7079

7180
// Output => 1 1 4 6 9 9 12 23 23 54 78 231
7281
System.out.println("After Sorting:");
73-
printList(countingSort(unsortedInts));
82+
print(countingSort.sort(unsortedInts));
7483
System.out.println("After Sorting By Streams:");
75-
printList(streamCountingSort(unsortedInts));
84+
print(streamSort(unsortedInts));
7685

7786
System.out.println("\n------------------------------\n");
7887

7988
// String Input
8089
List<String> unsortedStrings = Stream.of("c", "a", "e", "b","d", "a", "f", "g", "c").collect(toList());
8190

8291
System.out.println("Before Sorting:");
83-
printList(unsortedStrings);
92+
print(unsortedStrings);
8493

8594
//Output => a a b c c d e f g
8695
System.out.println("After Sorting:");
87-
printList(countingSort(unsortedStrings));
96+
print(countingSort.sort(unsortedStrings));
8897

8998
System.out.println("After Sorting By Streams:");
90-
printList(streamCountingSort(unsortedStrings));
91-
92-
}
99+
print(streamSort(unsortedStrings));
93100

94-
/**
95-
* Just print list
96-
* @param toPrint - a list which should be printed
97-
*/
98-
private static void printList(List<?> toPrint){
99-
toPrint.stream()
100-
.map(Object::toString)
101-
.map(str -> str + " ")
102-
.forEach(System.out::print);
103-
104-
System.out.println();
105101
}
106102
}

Sorts/src/sort/InsertionSort.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package sort;
2+
3+
import static sort.SortUtils.less;
4+
import static sort.SortUtils.print;
5+
6+
/**
7+
*
8+
* @author Varun Upadhyay (https://github.com/varunu28)
9+
* @author Podshivalov Nikita (https://github.com/nikitap492)
10+
*
11+
*/
12+
13+
class InsertionSort implements SortAlgorithm {
14+
15+
/**
16+
* This method implements the Generic Insertion Sort
17+
* Sorts the array in increasing order
18+
*
19+
* @param array The array to be sorted
20+
*
21+
**/
22+
23+
@Override
24+
public <T extends Comparable<T>> T[] sort(T[] array) {
25+
for (int j = 1; j < array.length; j++) {
26+
27+
// Picking up the key(Card)
28+
T key = array[j];
29+
int i = j - 1;
30+
31+
while (i >= 0 && less(key, array[i])) {
32+
array[i + 1] = array[i];
33+
i--;
34+
}
35+
// Placing the key (Card) at its correct position in the sorted subarray
36+
array[i + 1] = key;
37+
}
38+
return array;
39+
}
40+
41+
// Driver Program
42+
public static void main(String[] args) {
43+
// Integer Input
44+
Integer[] integers = {4, 23, 6, 78, 1, 54, 231, 9, 12};
45+
46+
InsertionSort sort = new InsertionSort();
47+
48+
sort.sort(integers);
49+
50+
// Output => 1 4 6 9 12 23 54 78 231
51+
print(integers);
52+
53+
// String Input
54+
String[] strings = {"c", "a", "e", "b","d"};
55+
56+
sort.sort(strings);
57+
58+
//Output => a b c d e
59+
print(strings);
60+
}
61+
}

Sorts/src/sort/SortAlgorithm.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package sort;
22

3+
import java.util.Arrays;
4+
import java.util.List;
5+
36
/**
47
* The common interface of most algorithms
58
*
@@ -10,4 +13,9 @@ public interface SortAlgorithm {
1013

1114
<T extends Comparable<T>> T[] sort(T[] unsorted);
1215

16+
@SuppressWarnings("unchecked")
17+
default <T extends Comparable<T>> List<T> sort(List<T> unsorted){
18+
return Arrays.asList(sort(unsorted.toArray((T[]) new Comparable[unsorted.size()])));
19+
}
20+
1321
}

0 commit comments

Comments
 (0)