快速排序实现(java实现)

记录自己理解实现快速排序
1、可运行的实现

public class Quicksort1 {
    public static void main(String[] args) {
        Random random = new Random();
        Entity[] entities = new Entity[60000000];
        for (int i = 0;i < 60000000;++i){
            Entity entity = new Entity(random.nextInt(600000000));
            entities[i] = entity;
        }
        long start = System.currentTimeMillis();
        Quicksort(entities,0,entities.length - 1);
        long end = System.currentTimeMillis();
        System.out.println("Quicksort :"+(end - start) / 1000);
        assert SortUtil.isSorted(entities);
    }

    private static int partition(Entity[] entities,int left,int right){

        boolean right2left = true;
        boolean left2right = false;
        Entity temp = entities[left];
        while (left != right){
             if (right2left){//右到左扫描
                 if (entities[right].compareTo(temp) >= 0){
                     right--;
                 }else {
                     entities[left] = entities[right];
                     left2right = true;
                     right2left = false;
                     continue;
                 }
             }

            if (left2right){//左到右扫描
                if (entities[left].compareTo(temp) <= 0){
                    left++;
                }else {
                    entities[right]  = entities[left];
                    right2left = true;
                    left2right = false;
                    continue;
                }
            }
        }
        //这边得到了temp的正确坑位
        entities[left]= temp;
        return left;
    }

    public static void Quicksort(Entity[] entities,int left,int right){
        if (left >= right){
            return;
        }

        if (entities == null || entities.length== 0){
            return;
        }

        int partition = partition(entities,left,right);
        Quicksort(entities,left,partition-1);//递归的对左边子数组进行 partition 和 左右子数组排序操作
        Quicksort(entities,partition + 1,right);//递归的对右边子数组进行 partition 和 左右子数组排序操作
    }
    
}

2、有问题实现会出现栈深度溢出问题但是程序逻辑没问题在此记录(重要事情mark)

public class Quicksort {
    public static void main(String[] args) {
        Random random = new Random();
        Entity[] entities = new Entity[60000000];
        for (int i = 0;i < 60000000;++i){
            Entity entity = new Entity(random.nextInt(600000000));
            entities[i] = entity;
        }
        long start = System.currentTimeMillis();
        Quicksort(entities,0,entities.length - 1);
        long end = System.currentTimeMillis();
        System.out.println("Quicksort :"+(end - start) / 1000);
        assert SortUtil.isSorted(entities);
    }

    private static int partition(Entity[] entities,int left,int right){
       return right2leftScan(entities,left,right,entities[left]);
    }

    public static void Quicksort(Entity[] entities,int left,int right){
        if (left >= right){
            return;
        }

        if (entities == null || entities.length== 0){
            return;
        }

        int partition = partition(entities,left,right);
        Quicksort(entities,left,partition-1);
        Quicksort(entities,partition + 1,right);
    }

    private static int right2leftScan(Entity[] entities,int left,int right,Entity temp){
       // Entity temp = entities[left];


        while (left != right){
            Entity righte = entities[right];
            if (righte.compareTo(temp) < 0){
              entities[left] = entities[right];
             return  left2rightScan(entities,left,right,temp);
            }else if(righte.compareTo(temp) >= 0){
                right--;
            }
        }
        entities[left] = temp;
        return left;

    }

    private static int left2rightScan(Entity[] entities,int left,int right,Entity temp){
        //Entity temp = entities[left];

        while (left != right){
            Entity lefte = entities[left];
            if (lefte.compareTo(temp) <= 0){
                left++;
            }else if(lefte.compareTo(temp) >0){
                entities[right]  = entities[left];
            return  right2leftScan(entities,left,right,temp);
            }
        }
        entities[left] = temp;
        return left;
    }
}

3、实体辅助类

public class Entity implements Comparable<Entity> {

    private int order;

    Entity(int order){
        this.order = order;
    }
    @Override
    public int compareTo(Entity o) {
        if (o == null){
            return 0;
        }
        return  this.order - o.order;
    }

    public int getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }
}

排序断言代码参考

4、原理参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值