1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
void quickSort(vector<int>& nums, int start, int end) { if (start >= end) return; int left = start, right = end, pivot = nums[start]; while (left < right) { for (; left < right && nums[right] >= pivot; ) --right; nums[left] = nums[right]; for (; left < right && nums[left] <= pivot; ) ++left; nums[right] = nums[left]; } nums[left] = pivot; quickSort(nums, start, left - 1); quickSort(nums, left + 1, end); }
void quickSort2(vector<int>& nums, int l, int r) { if (l >= r) return; int pivot = nums[l], slow = l; for (int i = l; i <= r; ++i) { if (nums[i] <= pivot) { swap(nums, i, slow); ++slow; } } swap(nums, slow - 1, l); quickSort2(nums, l, slow - 2); quickSort2(nums, slow, r); }
|