# 第 3 节 快速排序(第 1 版代码)
参考代码 1:
class Solution {
public int[] sortArray(int[] nums) {
quickSort(nums, 0, nums.length - 1);
return nums;
}
private void quickSort(int[] nums, int left, int right) {
if (left >= right) {
return;
}
int pivotIndex = partition(nums, left, right);
quickSort(nums, left, pivotIndex - 1);
quickSort(nums, pivotIndex + 1, right);
}
private int partition(int[] nums, int left, int right) {
int pivot = nums[left];
int j = left;
// all in nums[left + 1..j] <= pivot
// all in nums(j..i) > pivot
for (int i = left + 1; i <= right; i++){
if (nums[i] <= pivot) {
j++;
swap(nums, i, j);
}
}
swap(nums, left, j);
return j;
}
private void swap(int[] nums, int index1, int index2) {
int temp = nums[index1];
nums[index1] = nums[index2];
nums[index2] = temp;
}
}
修改定义。
参考代码 2:
class Solution {
public int[] sortArray(int[] nums) {
quickSort(nums, 0, nums.length - 1);
return nums;
}
private void quickSort(int[] nums, int left, int right) {
if (left >= right) {
return;
}
int pivotIndex = partition(nums, left, right);
quickSort(nums, left, pivotIndex - 1);
quickSort(nums, pivotIndex + 1, right);
}
private int partition(int[] nums, int left, int right) {
int pivot = nums[left];
int j = left + 1;
// all in nums[left + 1..j) <= pivot
// all in nums[j..i) > pivot
for (int i = left + 1; i <= right; i++){
if (nums[i] <= pivot) {
swap(nums, i, j);
j++;
}
}
swap(nums, left, j - 1);
return j - 1;
}
private void swap(int[] nums, int index1, int index2) {
int temp = nums[index1];
nums[index1] = nums[index2];
nums[index2] = temp;
}
}
作者:liweiwei1419 链接:https://suanfa8.com/quick-sort/quick-sort-basic 来源:算法吧 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。