-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.Java
More file actions
53 lines (44 loc) · 955 Bytes
/
quickSort.Java
File metadata and controls
53 lines (44 loc) · 955 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.util.Arrays;
import java.util.Collections;
class quickSort {
static int partition(Integer[] a, int low, int high) {
int i = low;
int j = low;
while (i < high) {
if (a[i] < a[high]) {
var m = a[i];
a[i] = a[j];
a[j] = m;
j++;
}
i++;
}
var t = a[j];
a[j] = a[high];
a[high] = t;
return j;
}
static void devide(Integer[] a, int low, int high) {
if (low >= high) return;
int mid = partition(a,low,high);
devide(a, low, mid-1);
devide(a, mid+1, high);
}
public static void main(String[] args) {
var n = 7;
var a = new Integer[n];
for (var i = 0; i < n; i++) {
a[i] = i;
}
Collections.shuffle(Arrays.asList(a));
for (var i : a) {
System.out.printf("%3d", i);
}
System.out.println();
devide(a, 0, n-1);
for (var i : a) {
System.out.printf("%3d", i);
}
System.out.println();
}
}