快速排序思想
- 选定pivot中心轴(一般选择最左侧)
- 将大于pivot的数字放在左侧
- 将小于pivot的数字放在右侧
- 递归pivot左侧和右侧的数列
代码实现:
public void quickSort(int[] nums) {
int length = nums.length;
quickSort(nums, 0, length - 1);
}
public void quickSort(int[] nums, int start, int end) {
if (start >= end) {
return;
}
int left = start;
int right = end;
int pivot = nums[left];
while (left < right) {
while (left < right && nums[right] > pivot) {
right--;
}
if (left < right) {
nums[left] = nums[right];
left++;
}
while (left < right && nums[left] < pivot) {
left++;
}
if (left < right) {
nums[right] = nums[left];
right--;
}
}
nums[left] = pivot;
quickSort(nums, start, left - 1);
quickSort(nums, left + 1, end);
}