选择第一个元素为基准,把数组分为两部分,左边小于基准,右边大于基本。
再分别对左区间和右区间进行同样的操作。
void quickSort(vector<int>& nums, int begin, int end) {
if (begin >= end) return;
int first = begin, last = end;
int key = nums[first];
while (first < last) {
while (first < last && nums[last] >= key) {
last--;
}
if (first < last) {
nums[first++] = nums[last];
}
while (first < last && nums[first] <= key {
first++;
}
if (first < last) {
nums[last--] = nums[first];
}
}
nums[first] = key;
quickSort(nums, begin, first - 1);
quickSort(nums, first + 1, end);
}