9-2 快速排序的微观解读

【实现】

public class QuickSort2 {
    private QuickSort2(){}
    public static <E extends Comparable<E>> void sort(E[] arr){
        sort(arr,0,arr.length-1,0);
    }
    public static <E extends Comparable<E>> void sort(E[] arr,int l,int r,int depth){
        if(l >= r) return;
        //查看 将要将要进行 Partition 的数组 和 区间信息
        System.out.print(generateDepthString(depth));
        System.out.print(String.format("first arr[%d,%d] ",l,r));

        for (int i = l; i <= r; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();

        int p = partition(arr,l,r,depth+1);

        //查看进行过portion的数组
        System.out.print(generateDepthString(depth));
        System.out.print(String.format("partition arr[%d,%d] ",l,r));
        for (int i = l; i <= r; i++) {
            if(i==p){
                System.out.print("["+arr[i]+"] ");
            }else{
                System.out.print(arr[i] + " ");
            }
        }
        System.out.println();

        // 对 arr[l,p-1]进行排序
        sort(arr,l,p-1,depth+1);
        //对 arr[p+1,r]进行排序
        sort(arr,p+1,r,depth+1);

        //查看排好序的数组
        System.out.print(generateDepthString(depth));
        System.out.print(String.format("final arr[%d,%d] ",l,r));
        for (int i = l; i <= r; i++) {
            if(i==p){
                System.out.print("["+arr[i]+"] ");
            }else{
                System.out.print(arr[i] + " ");
            }
        }
        System.out.println();
    }
    //将 p 放在合适的位置 使左边的都比 p 小,右边的都比 p 大
    public static <E extends Comparable<E>> int partition(E[] arr,int l,int r,int depth){
        //把数组分为  arr[l] | arr(l+1,j) | arr(j+1,i-1) | arr[e] | arr(e+1,r) 的形式
        //初始化 j 的索引,
        int j = l;
        for (int i = l + 1; i <= r; i++) {
            if(arr[i].compareTo(arr[l]) < 0){
                j++;
                swap(arr,i,j);
            }
        }
        swap(arr,l,j);
        return j;
    }
    public static <E extends Comparable<E>> void swap (E[] arr,int i,int j){
        E temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
    //观察递归深度
    public static String generateDepthString(int depth){
        StringBuilder res = new StringBuilder();
        for (int i = 0;i <= depth;i++) {
            res.append("---");
        }
        return res.toString();
    }
    public static void main(String[] args) {
        Integer[] arr = {5,9,8,7,6,4,3,2,1};
        sort(arr);
    }
}

【结果】

---first arr[0,8] 1 2 3 4 5 6 7 8 9 
---partition arr[0,8] [1] 2 3 4 5 6 7 8 9 
------first arr[1,8] 2 3 4 5 6 7 8 9 
------partition arr[1,8] [2] 3 4 5 6 7 8 9 
---------first arr[2,8] 3 4 5 6 7 8 9 
---------partition arr[2,8] [3] 4 5 6 7 8 9 
------------first arr[3,8] 4 5 6 7 8 9 
------------partition arr[3,8] [4] 5 6 7 8 9 
---------------first arr[4,8] 5 6 7 8 9 
---------------partition arr[4,8] [5] 6 7 8 9 
------------------first arr[5,8] 6 7 8 9 
------------------partition arr[5,8] [6] 7 8 9 
---------------------first arr[6,8] 7 8 9 
---------------------partition arr[6,8] [7] 8 9 
------------------------first arr[7,8] 8 9 
------------------------partition arr[7,8] [8] 9 
------------------------final arr[7,8] [8] 9 
---------------------final arr[6,8] [7] 8 9 
------------------final arr[5,8] [6] 7 8 9 
---------------final arr[4,8] [5] 6 7 8 9 
------------final arr[3,8] [4] 5 6 7 8 9 
---------final arr[2,8] [3] 4 5 6 7 8 9 
------final arr[1,8] [2] 3 4 5 6 7 8 9 
---final arr[0,8] [1] 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值