让我们来写个算法吧,(5)快速排序

package com.demo.sort;

public class QuickSort {
    
    
    public static void main(String[] args) {
        
        int [] t= {6,5,4,2,1,8,11,9,10};
        QuickSort obj = new QuickSort();
        obj.quickSort(0, t.length-1, t);
        for (int i : t) {
            System.out.print(i+" ");
        }
    }
    
    
    public void quickSort(int low,int high ,int[] arr) {
        
        
        // rec 出口
        if(low>high) {
            return;
        }
        System.out.println("quick sort start low ="+low +" high = "+high);
        // 保存指针
        int i =low ,j = high,temp;
        int pivot = arr[i];
        
        while(i<j) {
            
            while(arr[j] >= pivot && i<j) {
                j--;
            }
            while(arr[i]<= pivot &&i<j) {
                i++;
            }
            //swap
            if(i<j) {
                System.out.println("swap");
                temp = arr [i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
            
        }
        // 替换基数
        arr[low]=arr[i];
        arr[i] = pivot;
        
        quickSort(low, j-1, arr);
        quickSort(j+1, high, arr);
        
        
    }

}
原文地址:https://www.cnblogs.com/leaveast/p/12470488.html