public class Demo1 {
public static void main(String[] args) {
int[] nums={10,30,5,80,20,4};
kuaipai(nums,0,nums.length-1);
for (int i = 0; i < nums.length; i++) {
System.out.print(nums[i]+" ");
}
}
/*
快速排序-让数组内的元素递增排序
*/
static void kuaipai(int[] a,int left,int right){
int i,j;
if(left<right){
i= Division(a,left,right);
kuaipai(a,left,i-1);
kuaipai(a,i+1,right);
}
}
static int Division(int[] a,int left,int right){//分割
int base = a[left];//基准元素
while(left<right){
while(left<right&&a[right]>base)
--right;//
a[left]=a[right];
while (left<right&&a[left]<base)
++left;
a[right]=a[left];
}
a[left]=base;
return left;
}
}
java——快速排序算法
最新推荐文章于 2024-11-01 14:48:20 发布