-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathQuickSort.Java
More file actions
38 lines (27 loc) · 756 Bytes
/
QuickSort.Java
File metadata and controls
38 lines (27 loc) · 756 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
35
36
37
38
public class QuickSort {
public void quickSort(int[] array, int leftIndex, int rightIndex) {
// RightIndex = array.length - 1
if (leftIndex < rightIndex) {
int indice_do_pivot = particiona(array, leftIndex, rightIndex);
quickSort(array, leftIndex, indice_do_pivot - 1);
quickSort(array, indice_do_pivot + 1, rightIndex);
}
}
public int particiona(int[] array, int leftIndex, int rightIndex) {
int pivot = array[leftIndex];
int i = leftIndex;
for (int j = leftIndex + 1; j <= rightIndex; j++) {
if (array[j] < pivot) {
i++;
swap(array, i, j);
}
}
swap(array, leftIndex, i);
return i;
}
public void swap(int[] array, int i, int j) {
int aux = array[i];
array[i] = array[j];
array[j] = aux;
}
}