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
27 lines (24 loc) · 726 Bytes
/
SlowSort.java
File metadata and controls
27 lines (24 loc) · 726 Bytes
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
package com.thealgorithms.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;
}
final int m = (i + j) >>> 1;
sort(array, i, m);
sort(array, m + 1, j);
if (SortUtils.less(array[j], array[m])) {
SortUtils.swap(array, j, m);
}
sort(array, i, j - 1);
}
}