算法--快速排序

1、快速排序,首先选择一个对比分水岭数,可以选择数组第一个数,在第一次对比时,便可以将数组分为两个独立的数组,一边大于基准数,一边小于基准数,再次对比时候,分别对比两个独立数组,运用递归的方式,当低位大于或等于高位时,退出,返回数组,完成排序。

2、代码:

private int getBase(int[] order,int low,int height){

    int temp = order[low];
    while (low<height){
        while (low<height && order[height] >= order[low]){
            height--;
        }
        order[low] = order[height];
        while (low < height && order[low] <= order[height]) {
            low++;
        }
        order[height] = order[low];
        order[low] = temp;
    }
    return low;
}

private int[] quickOrder(int[] order,int low,int height){
    if(low<height){
        int base = getBase(order,low,height);
        quickOrder(order,low,base-1);
        quickOrder(order,base+1,height);
    }
    return order;

总结:快速排序,在每次排序完成后,排序方式,方式,一高一低,高位对比交换一次,低位对比交换一次。直到角标碰头。可以把数组分成两个独立数组,独立数组再通过同样递归方式,排序,直到两边角标碰头,完成排序。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值