简单的快速排序算法

public class SortUtil {
    public static void main(String[] args) {
        int[] from = new int[]{25, 12, 34, 56, 10, 3, 77, 88, 50,5};
        standardSort1(from, 0, from.length - 1);
        Arrays.stream(from).forEach(e-> System.out.print(e+","));
    }

    /**
     * 快速排序
     *
     * @param list
     */
    static void standardSort1(int[] list, int start, int over) {
        if (start >= over) {
            return;
        }
        if (list.length == 2 && list[start] > list[over]) {
            int tmp = list[over];
            list[start] = list[over];
            list[over] = tmp;
            return;
        }
        //标杆值
        int key = list[start];
        //空洞下标
        int dump = start;
        int i = start;
        int j = over;

        //从右往左
        boolean fromRight = true;
        while (i < j) {
            if (fromRight) {
                if (key > list[j]) {
                    //将小于值移到数组左边
                    list[dump] = list[j];
                    //临时存放key
                    list[j] = key;
                    dump = j;
                    fromRight = false;
                    ++i;
                } else {
                    --j;
                }
            } else {
                if (key < list[i]) {
                    //将大于key的值移到数组右边
                    list[dump] = list[i];
                    //临时存放key
                    list[i] = key;
                    dump = i;
                    --j;
                    fromRight = true;
                } else {
                    ++i;
                }
            }
        }
        standardSort1(list,start,i-1);
        standardSort1(list,i+1,over);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值