package sort; import java.util.Scanner; public class QuickSort { public static int[] arr; static{ System.out.println("Enter the array to sort, and use ‘,‘ as a separator:"); Scanner sc=new Scanner(System.in); String s=sc.nextLine(); String n[]=s.split(","); arr=new int[n.length]; System.out.println("Before sort:"); for(int i=0;i<arr.length;i++){ arr[i]=Integer.parseInt(n[i]); System.out.print(arr[i]+" "); } } public static void main(String args[]){ int m=arr.length-1; quickSort(0,m); System.out.println("Before sort:"); for(int i=0;i<arr.length;i++) System.out.print(arr[i]+" "); System.out.println(); } public static void quickSort(int left,int right){ int i=left; int j=right; int pivot=arr[left]; int temp=0; if(i<j){ do{ do{ if(arr[j]<pivot){ temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; break; } else j--; }while(j>i); do{ if(arr[i]>pivot){ temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; break; } else i++; }while(i<j); }while(i<j); System.out.print("Current sorting:"); for(int k=0;k<arr.length;k++) System.out.print(arr[k]+" "); System.out.println(); if(j-1>left) quickSort(left,j-1); if(right>i+1) quickSort(j+1,right); } } }
原文:http://www.cnblogs.com/lvbubu/p/4681435.html