快速排序

import java.util.*;
public class QuickSort{
    public void getQuick(int[] array,int left,int right){
        if(array == null || array.length == 0) return ;
        if(left<right){
            int mid = getMid(array,left,right);
            getQuick(array,0,mid-1);
            getQuick(array,mid+1,right);
        }
    }
    public int getMid(int[] array,int left,int right){
        while(left<right){
   
            while(left<right && array[left]<=array[right]){
          
                right--;
            }
            
            if(left<right){
                
                swap(array,left,right);
            }
       
            while(left<right && array[left]<=array[right]){
            
                left++;
            }
       
            
            if(left<right){
                swap(array,left,right);
            }
            
            
        }
        
        return left;
    }
    public void swap(int[] array,int left,int right){
        int temp = array[left];
        array[left] = array[right];
        array[right] = temp;
    }
    public static void main(String[] args){
        int[] array = {3,6,9, 2, 3, 1, 5, 4 };
        QuickSort q = new QuickSort();
        q.getQuick(array,0,7);
        System.out.println(Arrays.toString(array));
    }
}

 

posted @ 2018-07-05 00:32  樱圃  阅读(118)  评论(0编辑  收藏  举报