public class QuickSort{
public static void main(String[] args){
int[] test = {9,8,45,6,5,4,3,2,4,5,67,4,2,1,0};
quickSort(test);
for(int show : test){
System.out.println(show);
}
}
public static void swap(int[] nums, int i, int j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
public static void quickSort(int[] data){
doQuickSort(data, 0, data.length-1);
}
public static void doQuickSort(int[] data, int start, int end){
if(start>=end) return;
int flag = start;
int low = start;
int high = end + 1;
while(true){
while(++low < end && data[low] <= data[flag]);
while(--high > start && data[high] >= data[flag]);
if(low >=high) break;
swap(data, low, high);
}
swap(data, flag, high);//关键,用high
doQuickSort(data, start, high - 1);
doQuickSort(data, high + 1, end);
}
}
快速排序 QuickSort Java代码
最新推荐文章于 2022-08-13 10:35:22 发布