- Notifications
You must be signed in to change notification settings - Fork 9.1k
/
Copy pathMain.java
37 lines (35 loc) · 984 Bytes
/
Main.java
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
importjava.util.Scanner;
publicclassMain {
publicstaticvoidmain(String[] args) {
Scannersc = newScanner(System.in);
intn = sc.nextInt();
int[] nums = newint[n];
for (inti = 0; i < n; ++i) {
nums[i] = sc.nextInt();
}
quickSort(nums, 0, n - 1);
for (inti = 0; i < n; ++i) {
System.out.print(nums[i] + " ");
}
}
publicstaticvoidquickSort(int[] nums, intleft, intright) {
if (left >= right) {
return;
}
inti = left - 1, j = right + 1;
intx = nums[(left + right) >> 1];
while (i < j) {
while (nums[++i] < x)
;
while (nums[--j] > x)
;
if (i < j) {
intt = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
}
quickSort(nums, left, j);
quickSort(nums, j + 1, right);
}
}