import java.util.Arrays;
public class 快速排序 {
public static int partition(int nums[],int start,int end){
int num=nums[start];
int left=start;
int right=end;
while(left<right){
while(left<=end&&nums[left]<=num)
left++;
while(right>=start&&nums[right]>num)
right--;
if(left<right){
swap(nums, left, right);
}
}
swap(nums,start, right);
return right;
}
public void sort(int[] nums,int start,int end){
if(end-start<=1){
return;
}
int index=partition(nums, start, end);
sort(nums,start,index-1);
sort(nums,index+1,end);
}
public static void swap(int []nums,int i,int j){
int tmp=nums[i];
nums[i]=nums[j];
nums[j]=tmp;
}
public static void main(String args[]){
int a[]={2,3,5,4,1,9,7};
new 快速排序().sort(a,0,a.length-1);
System.out.print(Arrays.toString(a));
}
}
快排Java
最新推荐文章于 2023-05-06 18:38:36 发布