题目描述
快速排序
java 代码 参考了其他人怎么写java读入数据的写法
import java.util.Scanner;
import java.io.BufferedInputStream;
import java.util.Arrays;
public class Main{
public static void main(String[] args){
Scanner scanner=new Scanner(new BufferedInputStream(System.in));
int n=scanner.nextInt();
int[]nums=new int[n];
for(int i=0;i<n;i++){
nums[i]=scanner.nextInt();
}
quickSort(nums,0,n-1);
// for(int i=0;i<n;i++){
// System.out.print(nums[i]+" ");
// }
Arrays.stream(nums).forEach(a->System.out.print(a+" "));
}
public static void quickSort(int []nums,int left,int right){
if(left>=right){
return;
}
int i=left-1;
int j=right+1;
int privot=nums[left];
while(i<j){
do i++;while(nums[i]<privot);
do j--;while(nums[j]>privot);
if(i<j){
swap(nums,i,j);
}
}
quickSort(nums,left,j);
quickSort(nums,j+1,right);
}
public static void swap(int[]nums,int left,int right){
int t=nums[left];
nums[left]=nums[right];
nums[right]=t;
}
}