Skip to content

Commit 3c40937

Browse files
committed
Created general interface for most algorithms
Created utils methods Refactored ShellSort
1 parent 205aec0 commit 3c40937

File tree

3 files changed

+94
-39
lines changed

3 files changed

+94
-39
lines changed

Sorts/ShellSort.java

+21-39
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,50 @@
11
package Sorts;
22

3+
import static Sorts.SortUtils.*;
4+
5+
36
/**
47
* @author dpunosevac
8+
* @author Podshivalov Nikita (https://github.com/nikitap492)
9+
*
10+
* @see SortAlgorithm
11+
*
512
*/
6-
public class ShellSort {
13+
public class ShellSort implements SortAlgorithm {
714

815
/**
916
* This method implements Generic Shell Sort.
1017
* @param array The array to be sorted
1118
*/
12-
public static void shellSort(Comparable[] array) {
19+
@Override
20+
public <T extends Comparable<T>> T[] sort(T[] array) {
1321
int N = array.length;
1422
int h = 1;
1523

1624
while (h < N/3) {
17-
h = 3 * h + 1;
25+
h = 3 * h + 1;
1826
}
1927

2028
while (h >= 1) {
21-
for (int i = h; i < N; i++) {
22-
for (int j = i; j >= h && less(array[j], array[j-h]); j -= h) {
23-
exch(array, j, j - h);
29+
for (int i = h; i < N; i++) {
30+
for (int j = i; j >= h && less(array[j], array[j-h]); j -= h) {
31+
swap(array, j, j - h);
32+
}
2433
}
25-
}
2634

27-
h /= 3;
35+
h /= 3;
2836
}
29-
}
3037

31-
/**
32-
* Helper method for exchanging places in array
33-
* @param array The array which elements we want to swap
34-
* @param i index of the first element
35-
* @param j index of the second element
36-
*/
37-
private static void exch(Comparable[] array, int i, int j) {
38-
Comparable swap = array[i];
39-
array[i] = array[j];
40-
array[j] = swap;
41-
}
42-
43-
/**
44-
* This method checks if first element is less then the other element
45-
* @param v first element
46-
* @param w second element
47-
* @return true if the first element is less then the second element
48-
*/
49-
private static boolean less(Comparable v, Comparable w) {
50-
return v.compareTo(w) < 0;
38+
return array;
5139
}
5240

5341
public static void main(String[] args) {
54-
// Integer Input
55-
int[] arr1 = {4,23,6,78,1,54,231,9,12};
56-
Integer[] array = new Integer[arr1.length];
42+
Integer[] toSort = {4, 23, 6, 78, 1, 54, 231, 9, 12};
5743

58-
for (int i=0;i<arr1.length;i++) {
59-
array[i] = arr1[i];
60-
}
44+
ShellSort sort = new ShellSort();
45+
Integer[] sorted = sort.sort(toSort);
6146

62-
shellSort(array);
47+
print(sorted);
6348

64-
for (int i = 0; i < array.length; i++) {
65-
System.out.println(array[i]);
66-
}
6749
}
6850
}

Sorts/SortAlgorithm.java

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package Sorts;
2+
3+
/**
4+
* The common interface of most algorithms
5+
*
6+
* @author Podshivalov Nikita (https://github.com/nikitap492)
7+
*
8+
**/
9+
public interface SortAlgorithm {
10+
11+
<T extends Comparable<T>> T[] sort(T[] unsorted);
12+
13+
}

Sorts/SortUtils.java

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package Sorts;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
/**
7+
* The class contains util methods
8+
*
9+
* @author Podshivalov Nikita (https://github.com/nikitap492)
10+
*
11+
**/
12+
final class SortUtils {
13+
14+
15+
/**
16+
* Helper method for swapping places in array
17+
* @param array The array which elements we want to swap
18+
* @param idx index of the first element
19+
* @param idy index of the second element
20+
*/
21+
static <T> void swap(T[] array, int idx, int idy){
22+
T swap = array[idx];
23+
array[idx] = array[idy];
24+
array[idy] = swap;
25+
}
26+
27+
28+
/**
29+
* This method checks if first element is less then the other element
30+
* @param v first element
31+
* @param w second element
32+
* @return true if the first element is less then the second element
33+
*/
34+
static <T extends Comparable<T>> boolean less(T v, T w) {
35+
return v.compareTo(w) < 0;
36+
}
37+
38+
39+
/**
40+
* Just print list
41+
* @param toPrint - a list which should be printed
42+
*/
43+
static void print(List<?> toPrint){
44+
toPrint.stream()
45+
.map(Object::toString)
46+
.map(str -> str + " ")
47+
.forEach(System.out::print);
48+
49+
System.out.println();
50+
}
51+
52+
53+
/**
54+
* Prints an array
55+
* @param toPrint - the array which should be printed
56+
*/
57+
static void print(Object[] toPrint){
58+
print(Arrays.asList(toPrint));
59+
}
60+
}

0 commit comments

Comments
 (0)