forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStoogeSort.java
More file actions
34 lines (30 loc) · 939 Bytes
/
StoogeSort.java
File metadata and controls
34 lines (30 loc) · 939 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
28
29
30
31
32
33
34
package com.thealgorithms.sorts;
/**
* @author Amir Hassan (https://github.com/ahsNT)
* @see SortAlgorithm
*/
public class StoogeSort implements SortAlgorithm {
@Override
public <T extends Comparable<T>> T[] sort(T[] array) {
if (array.length == 0) {
return array;
}
sort(array, 0, array.length);
return array;
}
public <T extends Comparable<T>> T[] sort(final T[] array, final int start, final int end) {
if (SortUtils.less(array[end - 1], array[start])) {
final T temp = array[start];
array[start] = array[end - 1];
array[end - 1] = temp;
}
final int length = end - start;
if (length > 2) {
int third = length / 3;
sort(array, start, end - third);
sort(array, start + third, end);
sort(array, start, end - third);
}
return array;
}
}