SORTING
75. Sort Colors
912. Sort an Array快排
int[] sort(int[] nums){
quickSort(nums, 0, nums.length-1);
return nums;
}
void quickSort(int[] nums, int start, int end){
if(start<end){
int pivot = partition(nums, start, end);
quickSort(nums, start, pivot-1);
quickSort(nums, pivot+1, end);
}
}
int partition(int[] nums, int start, int end){
int pivot = nums[start];
int left = start, right=end;
while(left<right){
while(left<right && nums[right]>=pivot)
right--;
while(left<right && nums[left]<=pivot)
left++;
swap(nums, start, end);
}
swap(nums, start, left);
}
|