快速排序小练习

public class FastSort {


public static void main(String[] args) {
int[] array = new int[]{2,1,5,3,4};
FastSort.sort(array,0,array.length-1);
for(int i:array){
System.out.print(i+" ");
}
}


static void sort(int[] param,int fast,int end){
int l = fast;
int h = end;
int target = param[fast];
if(h<l){
return;
}
while(l<h){
while(l<h&&param[h]>=target)
h--;
if(l<h){
int temp = param[h];
param[h] = param[l];
param[l] = temp;
l++;


}
while(l<h&&param[l]<=target)
l++;
if(l<h){
int temp = param[h];
param[h]=param[l];
param[l]=temp;
h--;
}

}
if(l>fast)sort(param,fast,l-1);
if(h<end)sort(param, l+1, end);

}
}

猜你喜欢

转载自blog.csdn.net/qq_33500630/article/details/78810143