快排实现

@Test
public void test(){
        int[] num = { 1,99, 3, 4, 8, 5, 10, 22, 15, 16,165,165 };
        this.quickSort(num, 0, num.length - 1);
        System.out.println(Arrays.toString(num));
    }

public static void quickSort(int[] a, int start, int end) {
        // 该值定义了从哪个位置开始分割数组
        int ref;
        if (start < end) {
            // 调用partition方法对数组进行排序
            ref = partition(a, start, end);
            // 对分割之后的两个数组继续进行排序
            quickSort(a, start, ref - 1);
            quickSort(a, ref + 1, end);
        }
    }

    /**
     * 选定参考值对给定数组进行一趟快速排序
     * @param a 数组
     * @param start (切分)每个数组的第一个的元素的位置
     * @param end (切分)每个数组的最后一个的元素位置
     * @return 下一次要切割数组的位置
     */
    public static int partition(int[] a, int start, int end) {
        int refvalue = a[start];
        while (start < end) {
            while (end > start && a[end] >= refvalue) {
                end--;
            }
            a[start] = a[end];
            a[end] = refvalue;

            while (end > start && a[start] <= refvalue) {
                start++;
            }
            a[end] = a[start];
            a[start] = refvalue;
            return end;
        }
        a[start] = refvalue;
        return start;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值