- package quicksort;
- public class Swap{
- private int x;
- private int y;
- public Swap(int x,int y){
- this.x=x;
- this.y=y;
- }
- public void doSwap(){
- int temp=x;
- x=y;
- y=temp;
- }
- public int getx(){return x;}
- public int gety(){return y;}
- }
- package quicksort;
- public class QuickSort{
- private int[] array;
- private int left;
- private int right;
- public QuickSort(int[] array,int left,int right){
- this.array=array;
- this.left=left;
- this.right=right;
- this.doQuickSort(array,left,right);
- }
- private int partition(int[] array,int left,int right){
- int i=left;
- int j=right+1;
- int point=array[left];
- while(true){
- while(array[++i]<point&&i<right);
- while(array[--j]>point);
- if(i>=j)break;
- Swap temp=new Swap(array[i],array[j]);
- temp.doSwap();
- array[i]=temp.getx();
- array[j]=temp.gety();
- }
- array[left]=array[j];
- array[j]=point;
- return j;//balance point
- }
- private void doQuickSort(int[] array,int left,int right){
- if(left<right){
- int point=partition(array,left,right);
- doQuickSort(array,left,point-1);
- doQuickSort(array,point+1,right);
- }
- }
- public static void main(String[] args){
- int[] array={2,4,5,6,76,3,312,44,21,332,3};//排序数组
- QuickSort test=new QuickSort(array,0,9);//QuickSort(数组,数组起始下标,数组末尾下标)
- for(int i=0;i<=9;i++){
- System.out.println(array[i]);
- }
- }
- }