public void quick(int[] nums,int l, int r){
if(l >= r)return;
int a = l,b = r;
// 拷贝数组
int[] copy = new int[r-l+1];
int start = 0,end = copy.length-1;
int e = r,s = l;
int privot = nums[l];
// 注意这里是 l<=r
while (l <= r){
while (l <= r && nums[r] >= privot){
// 直接向后移动
nums[e--] = nums[r--];
}
while (l <= r && nums[l] < privot){// 注意这里没有== 会将其移动到copy数组的左段
// 直接向前移动
nums[s++] = nums[l++];
}
if(l < r){
// 大的移到copy数组右段
copy[start++] = nums[l++];
// 小的移动到copy数组的左段
copy[end--] = nums[r--];
}
}
// 把比privot小的复制
// 将拷贝数组从左到右的复制
for(int i = end+1; i < copy.length; i++){
nums[s++] = copy[i];
}
// 将把比privot大的复制
for(int i = start-1 ; i >= 0 ;i--){
nums[e--] = copy[i];
}
// System.out.println(privot == nums[s]);
quick(nums,a,s-1);
quick(nums,s+1,b);
}
public void quick(int[] nums,int l, int r){
if(l >= r)return;
int a = l,b = r;
// 拷贝数组
int[] copy = new int[r-l+1];
int start = 0,end = copy.length-1;
int e = r,s = l;
int privot = nums[l];
// 注意这里是 l<=r
while (l <= r){
while (l <= r && nums[r] >= privot){
// 直接向后移动
nums[e--] = nums[r--];
}
while (l <= r && nums[l] < privot){// 注意这里没有== ,会将privot放到copy数组的第一个,因为此时进不了循环
// 直接向前移动
nums[s++] = nums[l++];
}
if(l < r){ // l==r 无需交换 该值无论>=privot还是<privot 都可以直接移动到num数组的前端或者后端
// 大的移到copy数组左段
copy[start++] = nums[l++];
// 小的移动到copy数组的右段
copy[end--] = nums[r--];
}
}
// 把比privot小的复制
// 将拷贝数组从左到右的复制
for(int i = end+1; i < copy.length; i++){
nums[s++] = copy[i];
}
int u = s; // 此时的s是prviot的所在的位置
// 将把比privot大的复制
for(int i = 0 ; i < start ;i++){
nums[s++] = copy[i];
}
// System.out.println(privot == nums[u]);
quick(nums,a,u-1);
quick(nums,u+1,b);
}