快速排序

 

/**
 * 快速排序
 * 快速排序采用了分治策略。就是在一个数组中取一个基准数字,把小的数放基准的左边,大的数放基准的右边。
 * 基准左边和右边分别是新的序列。在新的序列中再取一个基准数字,小的放左边,大的放右边。
 * 这个里面用到的递归。我们需要三个参数,一个是数组,另外两个是序列的边界
 * @author HJS
 */
public class QuickSort{

    void sort(int num[],int left,int right){
        if (left<right){
            int index=partition(num,left,right); //算出枢轴值
            sort(num,left,index-1);       //对低子表递归排序
            sort(num,index+1,right);        //对高子表递归排序
        }
    }

    /**
     * 调用partition(num,left,right)时,对num[]做划分,
     * 并返回基准记录的位置
     * @param num
     * @param left
     * @param right
     * @return
     */
    public int partition(int[] num,int left,int right){
        if(num==null || num.length<=0 || left<0 || right>=num.length){
            return 0;
        }
        int prio=num[left+(right-left)/2];   //获取数组中间元素的下标
        while (left<=right){                 //从两端交替向中间扫描
            while (num[left]<prio) {
                left++;
            }
            while (num[right]>prio) {
                right--;
            }
            if (left<=right){
                swap(num,left,right);        //最终将基准数归位
                left++;
                right--;
            }
        }

        return left;
    }

    public void swap(int[] num,int left,int right){
        int temp = num[left];
        num[left] = num[right];
        num[right] = temp;
    }
    public static void main(String args[]) {
        int[] num={7,3,5,1,2,8,9,2,6};
        new QuickSort().sort(num,0,num.length-1);
        for(int n:num) {
            System.out.print(n+" ");
        }
    }
}

参考:https://www.cnblogs.com/jiansen/p/7343867.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值