随机快速排序

快速排序和随机快排 递归版

package com.lzf2.class04;

/**
 * 快速排序、随机快速排序
 */
public class QuickSort {

    //1.快随排序
    public static void quickSort1(int[] arr){
        if (arr == null || arr.length < 2){
            return;
        }
        process(arr,0,arr.length - 1);
    }
    //递归
    private static void process(int[] arr,int L, int R){
        //base case
        if(L >= R){
            return;
        }
        int[] equalArea = partition(arr, L, R);
        process(arr,L,equalArea[0] - 1);
        process(arr,equalArea[1] + 1,R);
    }
    //2.随机快排
    public static void quickSort2(int[] arr){
        if (arr == null || arr.length < 2){
            return;
        }
        process2(arr,0,arr.length - 1);
    }
    //递归
    private static void process2(int[] arr,int L, int R){
        //base case
        if(L >= R){
            return;
        }
        //随机 最后一个数 和 随机一个数交换 时间复杂度变位O(logN)
        swap(arr, L + (int) (Math.random() * (R - L + 1)), R);
        int[] equalArea = partition(arr, L, R);
        process2(arr,L,equalArea[0] - 1);
        process2(arr,equalArea[1] + 1,R);
    }


    //荷兰国旗
    private static int[] partition(int[] arr, int L, int R) {
        if (L > R) {
            return new int[]{-1, -1};
        }
        if (L == R) {
            return new int[]{L, R};
        }
        int less = L - 1;
        int more = R;
        int index = L;
        while (index < more) {
            if (arr[index] == arr[R]) {
                index++;
            } else if (arr[index] < arr[R]) {
                swap(arr, index++, ++less);
            } else { // >
                swap(arr, index, --more);
            }
        }
        swap(arr, more, R);
        return new int[]{less + 1, more};
    }
    private static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
}

随机快排非递归版本

package com.lzf2.class04;

import java.util.Stack;

/**
 * 随机快排,非递归版本
 */
public class QuickSortUnrecursive {
    //荷兰国旗
    private static int[] partition(int[] arr, int L, int R) {
        if (L > R) {
            return new int[]{-1, -1};
        }
        if (L == R) {
            return new int[]{L, R};
        }
        int less = L - 1;
        int more = R;
        int index = L;
        while (index < more) {
            if (arr[index] == arr[R]) {
                index++;
            } else if (arr[index] < arr[R]) {
                swap(arr, index++, ++less);
            } else {
                swap(arr, index, --more);
            }
        }
        swap(arr, more, R);
        return new int[]{less + 1, more};
    }

    private static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    //快排非递归版本需要的辅助类 要处理的是什么范围上的排序
    public static class Op {
        public int l;
        public int r;

        public Op(int left, int right) {
            l = left;
            r = right;
        }
    }
    //非递归,自己用栈实现
    public static void quickSort(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        int N = arr.length;
        swap(arr, (int) (Math.random() * N), N - 1);
        int[] equalArea = partition(arr, 0, N - 1);
        int el = equalArea[0];
        int er = equalArea[1];
        Stack<Op> stack = new Stack<>();
        stack.push(new Op(0, el - 1));
        stack.push(new Op(er + 1, N - 1));
        while (!stack.isEmpty()){
            Op op = stack.pop();
            if(op.l < op.r){
                swap(arr, op.l + (int) (Math.random() * (op.r - op.l + 1)), op.r);
                equalArea = partition(arr,op.l,op.r);
                el = equalArea[0];
                er = equalArea[1];
                stack.push(new Op(op.l, el - 1));
                stack.push(new Op(er + 1, op.r));
            }
        }

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值