Java---快排

import java.util.Scanner;

public class Main {
	public static void main (String[] args){
		int a[] = new int[1000];
		int n;
		Scanner read = new Scanner(System.in);
		n = read.nextInt();
		for (int i = 0; i < n;i++){
			a[i] = read.nextInt();
		}
		Qsort (a,0,n-1);
		Print(a,n);
		read.close();
	}
	
	static void Qsort (int a[],int l,int r){
		int x = a[l],i = l,j = r;
		if ( i >= j )
			return ;
		while (i < j){
			while (i < j && a[j] >= x)	j--;
			a[i] = a[j];
			while ( i < j && a[i] <= x)	i++;
			a[j] = a[i];
		}
		a[i] = x;
		Qsort (a,l, i - 1);
		Qsort (a,i + 1 ,r);
	}
	static void Print (int a[],int n){
		for(int i = 0; i < n;i++){
			System.out.print(a[i] +" ");
		}
	}

}

你可能感兴趣的:(java)