AcWing 785. 快速排序-java
原题链接
简单
作者:
komorebi_2
,
2021-03-20 12:27:26
,
所有人可见
,
阅读 328
import java.util.*;
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 m:nums){
System.out.print(m+" ");
}
}
public static void quicksort(int[] nums,int l, int r){
if(l>=r){
return;
}
int pivlot = nums[l];
int i = l-1;
int j = r+1;
while(i<j){
do{
i++;
}while(nums[i]<pivlot);
do{
j--;
}while(nums[j]>pivlot);
if(i<j){
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
}
quicksort(nums,l,j);
quicksort(nums,j+1,r);
}
}