-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
48 lines (33 loc) · 1.05 KB
/
QuickSort.java
File metadata and controls
48 lines (33 loc) · 1.05 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
package algoritmos;
import java.util.Arrays;
public class QuickSort {
public void sort(int[] array, int low, int high){
if(low < high){
int indexPivot = partition(array, low, high);
sort(array, low, indexPivot-1);
sort(array, indexPivot+1, high);
}
}
private int partition(int[] array, int low, int high) {
int pivot = array[high];
int i = low - 1;
for(int j = low; j < high; j++){
if(array[j] < pivot){
i++;
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
int temp = array[i+1];
array[i+1] = array[high];
array[high] = temp;
return i+1;
}
public static void main(String[] args) {
QuickSort sort = new QuickSort();
int[] array = new int[]{1,3,4,5,2,4,4,32,32,3,2,32,42,43,43,2,4,23,2,43,243,42,1};
sort.sort(array, 0, array.length - 1);
System.out.println(Arrays.toString(array));
}
}