快排再了解

package com.taiji.xmt.modular.test;

/**
 * @Author: Chasers
 * @Date: 2022/3/9 23:18
 * @Description:
 */
public class QuickSort {

    public static void quickSort(int[]a,int low,int high){// 不是每次排序都是从头尾开始排序的
        int start = low; //左边的游标
        int end = high; // 右边的游标
        int curr = a[start]; // 刻度 每次循环都不会变
        while (start<end){
            // 出来的点一定是两个游标相等的时候
            while (start<end&&a[end]>curr){
                end--;
            }
            // 循环出来的条件有两个一个游标相等的时候另一个是达到要求所以下面要判断
            if(start<end){
                // 一定是start和end的交换
                int temp = a[start];
                a[start] = a[end];
                a[end] = temp;
            }

            while (start<end&&a[start]<curr){
                start++;
            }

            if(start<end){
                int temp = a[start];
                a[start] = a[end];
                a[end] = temp;
            }

        }

        // 一轮循环后比curr 小的都在左边,比curr大的都在右边
        if(start>low){
            quickSort(a,low,start-1);
        }

        if(end<high){
            quickSort(a,end+1,high);
        }

    }

    public static void print(int[] a){
        for (int i:a){
            System.out.print(i+",");
        }
    }




    public static void main(String[] args) {
        int[] a= {6,1,3,7,9,2,10,4};

        quickSort(a,0,a.length-1);

        print(a);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值