作者:Geekwyz
排序数组
这题直接快排不多bb
class Solution {
int sort(vector<int>& nums,int left,int right) {
int i = left - 1;
int j = right + 1;
int x = nums[(left + right)>>1];
while (i < j) {
do i++; while(x > nums[i]);
do j--; while(x < nums[j]);
if (i < j) swap(nums[i],nums[j]);
}
return j;
}
void quick_sort(vector<int>& nums,int left,int right){
if (left >= right) return;
int j = sort(nums,left,right);
quick_sort(nums,left,j);
quick_sort(nums,j + 1,right);
}
public:
vector<int> sortArray(vector<int>& nums) {
quick_sort(nums,0,nums.size() - 1);
return nums;
}
};