快速排序java版

这里是快速排序算法
public class QuickSort {
	public static void main(String[] args) {
		int []a = {1,5,2,6,8,9,10,3,5,13,18,0,7,43,26,47};
		sort(a,0,a.length-1);
		print(a);
	}
	public static void sort(int arr[],int low,int high){
	  int l=low;
	 int h=high;
	 int povit=arr[low];
	 
	while(l<h){
	  
		while(l<h&&arr[h]>=povit)h--;
	   
		if(l<h){
	      int temp=arr[h];
	      arr[h]=arr[l];
	      arr[l]=temp;
          l++;
	      }
	 
	   while(l<h&&arr[l]<=povit)l++;
	 
	   if(l<h){
		   int temp=arr[h];
		   arr[h]=arr[l];
		   arr[l]=temp;
		   h--;
	          }
      }
      print(arr);
      System.out.println("l= "+(l+1)+" h="+(h+1)+" povit="+povit+"\n");
      if(l>low)sort(arr,low,h-1);
      if(h<high)sort(arr,l+1,high);
      }
	private static void print(int[] arr) {
		// TODO 自动生成的方法存根
		for(int i=0;i<arr.length;i++){
			System.out.print(arr[i]+" ");
			if((i+1) % 8 == 0 )System.out.print("\n");
		}
		System.out.print("\n");
	}
}

你可能感兴趣的:(java,eclipse,实现,快速排序)