AcWing 785. 快速排序
原题链接
简单
作者:
guman
,
2020-09-22 00:49:11
,
所有人可见
,
阅读 413
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int count = sc.nextInt();
int[] nums = new int[count];
int i =0;
while(i<count){
nums[i] = sc.nextInt();
i++;
}
doQuickSort(nums,0,count-1);
for(int a:nums){
System.out.print(a + " ");
}
}
public static void doQuickSort(int[] nums,int s, int e){
if(s >= e){
return;
}
int k =nums[s];
int i=s-1,j=e+1;
while(i<j){
while(nums[++i] <k){
}
while(nums[--j] >k){
}
if(i<j){
swap(nums,i,j);
}
}
doQuickSort(nums, s,j);
doQuickSort(nums, j+1,e);
}
public static void swap(int[] nums,int i,int j){
int a = nums[i];
nums[i] = nums[j];
nums[j] = a;
}
}