java版快排。

 
 
import java.util.Date;
import java.util.Random;

public class test {
	static Random rd = new Random(new Date().getTime());
	static int len;
	static void quick_sort(int[] arr,int l,int r){
		int i,j,x;
		if(l<r){
			i=l;
			j=r;
			x=arr[i];
			while(i<j){
				while(i<j&&arr[j]>x) j--;
				if(i<j) arr[i++]=arr[j];
				while(i<j&&arr[i]<x) i++;
				if(i<j) arr[j--]=arr[i];
			}
			arr[i]=x;
			if(l<i-1) quick_sort(arr,l,i-1);
			if(r>i+1) quick_sort(arr,i+1,r);
		}
	}
	static void swap (int[] arr,int i,int j){
		arr[j]=arr[i]+arr[j];
		arr[i]=arr[j]-arr[i];
		arr[j]=arr[j]-arr[i];
	}
	static void quick_sort_pro(int[] arr,int l,int r){
		
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int length=50;
		//int[] arr = new int[]{2,3,3,1,13,2,44,4,56,77,9,0,23};
		int[] arr = new int[length];
		for(int i=0;i<length;i++){
			arr[i]=rd.nextInt(length);
		}
		
		quick_sort(arr,0,arr.length-1);
		//quick_sort_r(arr,0,arr.length-1);
		for(int item : arr){
			System.out.println(item);
		}
	}
}


你可能感兴趣的:(java版快排。)