-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort.java
More file actions
28 lines (25 loc) · 765 Bytes
/
Copy pathSort.java
File metadata and controls
28 lines (25 loc) · 765 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
public class Sort {
public static void sort(int[] a) {
quickSort(a, 0, a.length - 1);
}
static void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static int partition(int[] a, int start, int end) {
int value = a[end];
int i = start - 1;
for (int j = start; j <= end - 1; ++j)
if (a[j] < value)
swap(a, ++i, j);
swap(a, i + 1, end);
return i + 1;
}
static void quickSort(int[] a, int start, int end) {
if (start >= end) return;
int middle = partition(a, start, end);
quickSort(a, start, middle-1);
quickSort(a, middle+1, end);
}
}