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
| #include <vector>
using namespace std;
class Solution { public: vector<int> sortArray(vector<int>& nums) { quickSort(nums, 0, (int)nums.size()-1); return nums; }
void quickSort(vector<int>& nums, int l, int r) { if (l >= r) { return; } int pivot = nums[l]; int index = l; for (int i = l + 1; i <= r; ++i) { if (nums[i] < pivot) { ++index; swap(nums[i], nums[index]); } } swap(nums[l], nums[index]); quickSort(nums, l, index-1); quickSort(nums, index+1, r); } };
|