forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; ++i) {
nums[i] = sc.nextInt();
}
quickSort(nums, 0, n - 1);
for (int i = 0; i < n; ++i) {
System.out.print(nums[i] + " ");
}
}
public static void quickSort(int[] nums, int left, int right) {
if (left >= right) {
return;
}
int i = left - 1, j = right + 1;
int x = nums[(left + right) >> 1];
while (i < j) {
while (nums[++i] < x)
;
while (nums[--j] > x)
;
if (i < j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
}
quickSort(nums, left, j);
quickSort(nums, j + 1, right);
}
}