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); } }
快排再了解
最新推荐文章于 2024-07-29 14:22:26 发布