-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickselect.java
More file actions
51 lines (42 loc) · 1.51 KB
/
Quickselect.java
File metadata and controls
51 lines (42 loc) · 1.51 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
50
51
import java.util.Random;
public class Quickselect {
public static ServerStatus quickselect(ServerStatus[] arr, int k) {
if (k < 0 || k >= arr.length) {
throw new IllegalArgumentException("k est hors des limites du tableau");
}
return quickselect(arr, 0, arr.length - 1, k);
}
private static ServerStatus quickselect(ServerStatus[] arr, int left, int right, int k) {
if (left == right) {
return arr[left];
}
Random random = new Random();
int pivotIndex = left + random.nextInt(right - left + 1);
pivotIndex = partition(arr, left, right, pivotIndex);
if (k == pivotIndex) {
return arr[k];
} else if (k < pivotIndex) {
return quickselect(arr, left, pivotIndex - 1, k);
} else {
return quickselect(arr, pivotIndex + 1, right, k);
}
}
private static int partition(ServerStatus[] arr, int left, int right, int pivotIndex) {
long pivotValue = arr[pivotIndex].freeSpace;
swap(arr, pivotIndex, right);
int storeIndex = left;
for (int i = left; i < right; i++) {
if (arr[i].freeSpace < pivotValue) {
swap(arr, storeIndex, i);
storeIndex++;
}
}
swap(arr, right, storeIndex);
return storeIndex;
}
private static void swap(ServerStatus[] arr, int i, int j) {
ServerStatus temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}