forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlowSort.java
More file actions
49 lines (41 loc) · 1.33 KB
/
SlowSort.java
File metadata and controls
49 lines (41 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package Sorts;
/**
* @author Amir Hassan (https://github.com/ahsNT)
* @see SortAlgorithm
*/
public class SlowSort implements SortAlgorithm {
@Override
public <T extends Comparable<T>> T[] sort(T[] unsortedArray) {
sort(unsortedArray, 0, unsortedArray.length - 1);
return unsortedArray;
}
private <T extends Comparable<T>> void sort(T[] array, int i, int j) {
if (SortUtils.greaterOrEqual(i, j)) {
return;
}
int m = (i + j) / 2;
sort(array, i, m);
sort(array, m + 1, j);
if (SortUtils.less(array[j], array[m])) {
T temp = array[j];
array[j] = array[m];
array[m] = temp;
}
sort(array, i, j - 1);
}
public static void main(String[] args) {
SlowSort slowSort = new SlowSort();
Integer[] integerArray = {8, 84, 53, 953, 64, 2, 202, 98};
// Print integerArray unsorted
SortUtils.print(integerArray);
slowSort.sort(integerArray);
// Print integerArray sorted
SortUtils.print(integerArray);
String[] stringArray = {"g", "d", "a", "b", "f", "c", "e"};
// Print stringArray unsorted
SortUtils.print(stringArray);
slowSort.sort(stringArray);
// Print stringArray sorted
SortUtils.print(stringArray);
}
}