AcWing 785. 快速排序
原题链接
简单
作者:
恒心
,
2020-10-21 00:27:44
,
所有人可见
,
阅读 432
import java.io.*;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int[] nums = new int[n];
String[] strs = br.readLine().split(" ");
for(int i = 0; i < strs.length; ++i){
nums[i] = Integer.parseInt(strs[i]);
}
quick_sort(nums, 0, n - 1);
for(int i = 0; i < n; ++i){
System.out.print(nums[i] + " ");
}
br.close();
}
public static void quick_sort(int[] nums, int start, int end){
if(start >= end) return;
int i = start - 1, j = end + 1;
int x = nums[start + (end - start) / 2];
while(i < j){
while(nums[++i] < x);
while(nums[--j] > x);
if(i < j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
quick_sort(nums, start, j);
quick_sort(nums, j + 1, end);
}
}