一日一算法------快速排序

346 查看

import java.util.Arrays;

public class QuickSort {
    public static int povit(int[] arr, int left, int right) {
        int Temp = arr[left];
        
        while(left < right) {
            while(arr[right] >= Temp && left < right) right --;
            if(left < right) {
                arr[left ++] = arr[right];
            }
            
            while(arr[left]  < Temp && left < right) left ++;
            
            if(left < right) {
                arr[right--] = arr[left];
            }
        }
    
        arr[left] = Temp;
        System.out.println(Arrays.toString(arr));
        return left;
    
        
    }
    
    public static void sort(int[] arr, int left, int right) {
        int povit = povit(arr, left, right);
        if(left < povit - 1) {
            sort(arr, left, povit - 2);
        }
        
        if(povit + 1 < right) {
            sort(arr, povit +  1 , right);
        }
    }
    
    
    
    public static void main(String[] args) {
        int[] arr = {2,5,5,1,7,4};
        sort(arr, 0, arr.length -1);
        
        System.out.println(Arrays.toString(arr));
    }
}