SIGN IN SIGN UP
doocs / leetcode UNCLAIMED

🔥LeetCode solutions in any programming language | 多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解

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);
}
}