import javax.imageio.plugins.jpeg.JPEGImageReadParam;
public class 快排 {
private static void quickSort(int[] nums,int low, int high){
if(low > high)
return;
int partion = quickPartion(nums, low, high);
quickSort(nums,low,partion-1);
quickSort(nums,partion+1,high);
}
private static int quickPartion(int[] nums, int low, int high){
int pivot = nums[low];
while(low < high){
while(low < high && pivot <= nums[high])
high--;
nums[low] = nums[high];
while(low < high && pivot >= nums[low])
low++;
nums[high] = nums[low];
}
nums[low] = pivot;
return low;
}
public static void main(String[] args) {
int[] nums = { -3, -4, 0, -9, 9, 2};
quickSort(nums, 0 , nums.length-1);
for (int num : nums) {
System.out.print(num+",");
}
}
}
java快排[王道解法]
于 2023-12-08 10:17:24 首次发布