import java.util.Arrays;
import java.util.Random;
public class KuaiSuSort {
public static void main(String[] args) {
int []a=randomArray();
System.out.println(Arrays.toString(a));
System.out.println("==================================");
sort(a,0,a.length-1);
System.out.println("==================================");
System.out.println(Arrays.toString(a));
}
private static void sort(int[] a, int lo, int hi) {
/**
* [77, 65, 79, 95, 37, 89, 98, 41, 84]
* t=77
*
* low hi
* [ 65, 79, 95, 37, 89, 98, 41, 84]
* i 找<-j
* i 找<-j
*
*/
if(lo>=hi)return;//最简问题
int i=lo;
int j=hi;
int t=a[lo];
while(i<j)
{
//j向左找<=t的值
while(a[j]>t&&j>i){
j--;
}
a[i]=a[j];
//i向右找>t的值
while(a[i]<=t&&j>i)
{
i++;
}
a[j]=a[i];
}
a[i]=t;
System.out.println(t);
System.out.println("-------");
System.out.println(Arrays.toString(a));
sort(a,lo,i-1);
sort(a,i+1,hi);
}
private static int[] randomArray() {
//5+[0,6)随机长度的数组,填入100以内随机值
int[] a=new int[new Random().nextInt(6)+5];
for (int i = 0; i < a.length; i++) {
a[i]=new Random().nextInt(100);
}
return a;
}
}
运行结果
[54, 22, 44, 44, 70, 59]
==================================
54
-------
[44, 22, 44, 54, 70, 59]
44
-------
[44, 22, 44, 54, 70, 59]
44
-------
[22, 44, 44, 54, 70, 59]
70
-------
[22, 44, 44, 54, 59, 70]
==================================
[22, 44, 44, 54, 59, 70]