public static void main(String[] args) {
int[] nums = new int[]{2,3,4,5,1,3,4,5,7,8,24,56,45,75,98,12,34};
quickSort(nums,0,nums.length-1);
for(int i : nums){
System.out.println(i);
}
}
public static void quickSort(int[] nums,int left,int right){
int i,j,temp,t;
if(left>right){
return;
}
i=left;
j=right;
//temp就是基准位
temp = nums[left];
while (i<j) {
//先看右边,依次往左递减
while (temp<=nums[j]&&i<j) {
j--;
}
//再看左边,依次往右递增
while (temp>=nums[i]&&i<j) {
i++;
}
//如果满足条件则交换
if (i<j) {
t = nums[j];
nums[j] = nums[i];
nums[i] = t;
}
}
//最后将基准为与i和j相等位置的数字交换
nums[left] = nums[i];
nums[i] = temp;
//递归调用左半数组
quickSort(nums, left, j-1);
//递归调用右半数组
quickSort(nums, j+1, right);
}
输出结果:
1
2
3
3
4
4
5
5
7
8
12
24
34
45
56
75
98
Process finished with exit code 0