cpp-algo-cases/chapter_sorting/quick_sort.cpp

169 lines
5.2 KiB
C++
Raw Normal View History

2024-02-20 16:13:23 +08:00
/**
* File: quick_sort.cpp
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
class QuickSort {
private:
/* Ԫ<>ؽ<EFBFBD><D8BD><EFBFBD> */
static void swap(vector<int> &nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
/* <20>ڱ<EFBFBD><DAB1><EFBFBD><EFBFBD><EFBFBD> */
static int partition(vector<int> &nums, int left, int right) {
// <20><> nums[left] Ϊ<><CEAA>׼<EFBFBD><D7BC>
int i = left, j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left])
j--; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׸<EFBFBD>С<EFBFBD>ڻ<EFBFBD>׼<EFBFBD><D7BC><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
while (i < j && nums[i] <= nums[left])
i++; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׸<EFBFBD><D7B8><EFBFBD><EFBFBD>ڻ<EFBFBD>׼<EFBFBD><D7BC><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
swap(nums, i, j); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
}
swap(nums, i, left); // <20><><EFBFBD><EFBFBD>׼<EFBFBD><D7BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ķֽ<C4B7><D6BD><EFBFBD>
return i; // <20><><EFBFBD>ػ<EFBFBD>׼<EFBFBD><D7BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
public:
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
static void quickSort(vector<int> &nums, int left, int right) {
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E9B3A4>Ϊ 1 ʱ<><CAB1>ֹ<EFBFBD>ݹ<EFBFBD>
if (left >= right)
return;
// <20>ڱ<EFBFBD><DAB1><EFBFBD><EFBFBD><EFBFBD>
int pivot = partition(nums, left, right);
// <20>ݹ<EFBFBD><DDB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E9A1A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
quickSort(nums, left, pivot - 1);
quickSort(nums, pivot + 1, right);
}
};
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E0A3A8>λ<EFBFBD><CEBB>׼<EFBFBD><D7BC><EFBFBD>Ż<EFBFBD><C5BB><EFBFBD> */
class QuickSortMedian {
private:
/* Ԫ<>ؽ<EFBFBD><D8BD><EFBFBD> */
static void swap(vector<int> &nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
/* ѡȡ<D1A1><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡԪ<D1A1>ص<EFBFBD><D8B5><EFBFBD>λ<EFBFBD><CEBB> */
static int medianThree(vector<int> &nums, int left, int mid, int right) {
// <20>˴<EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>򻯴<EFBFBD><F2BBAFB4><EFBFBD>
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1
if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))
return left;
else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))
return mid;
else
return right;
}
/* <20>ڱ<EFBFBD><DAB1><EFBFBD><EFBFBD>֣<EFBFBD><D6A3><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1>ֵ<EFBFBD><D6B5> */
static int partition(vector<int> &nums, int left, int right) {
// ѡȡ<D1A1><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡԪ<D1A1>ص<EFBFBD><D8B5><EFBFBD>λ<EFBFBD><CEBB>
int med = medianThree(nums, left, (left + right) / 2, right);
// <20><><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
swap(nums, left, med);
// <20><> nums[left] Ϊ<><CEAA>׼<EFBFBD><D7BC>
int i = left, j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left])
j--; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׸<EFBFBD>С<EFBFBD>ڻ<EFBFBD>׼<EFBFBD><D7BC><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
while (i < j && nums[i] <= nums[left])
i++; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׸<EFBFBD><D7B8><EFBFBD><EFBFBD>ڻ<EFBFBD>׼<EFBFBD><D7BC><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
swap(nums, i, j); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
}
swap(nums, i, left); // <20><><EFBFBD><EFBFBD>׼<EFBFBD><D7BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ķֽ<C4B7><D6BD><EFBFBD>
return i; // <20><><EFBFBD>ػ<EFBFBD>׼<EFBFBD><D7BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
public:
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
static void quickSort(vector<int> &nums, int left, int right) {
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E9B3A4>Ϊ 1 ʱ<><CAB1>ֹ<EFBFBD>ݹ<EFBFBD>
if (left >= right)
return;
// <20>ڱ<EFBFBD><DAB1><EFBFBD><EFBFBD><EFBFBD>
int pivot = partition(nums, left, right);
// <20>ݹ<EFBFBD><DDB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E9A1A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
quickSort(nums, left, pivot - 1);
quickSort(nums, pivot + 1, right);
}
};
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ࣨβ<E0A3A8>ݹ<EFBFBD><DDB9>Ż<EFBFBD><C5BB><EFBFBD> */
class QuickSortTailCall {
private:
/* Ԫ<>ؽ<EFBFBD><D8BD><EFBFBD> */
static void swap(vector<int> &nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
/* <20>ڱ<EFBFBD><DAB1><EFBFBD><EFBFBD><EFBFBD> */
static int partition(vector<int> &nums, int left, int right) {
// <20><> nums[left] Ϊ<><CEAA>׼<EFBFBD><D7BC>
int i = left, j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left])
j--; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׸<EFBFBD>С<EFBFBD>ڻ<EFBFBD>׼<EFBFBD><D7BC><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
while (i < j && nums[i] <= nums[left])
i++; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׸<EFBFBD><D7B8><EFBFBD><EFBFBD>ڻ<EFBFBD>׼<EFBFBD><D7BC><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
swap(nums, i, j); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
}
swap(nums, i, left); // <20><><EFBFBD><EFBFBD>׼<EFBFBD><D7BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ķֽ<C4B7><D6BD><EFBFBD>
return i; // <20><><EFBFBD>ػ<EFBFBD>׼<EFBFBD><D7BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
public:
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD>ݹ<EFBFBD><DDB9>Ż<EFBFBD><C5BB><EFBFBD> */
static void quickSort(vector<int> &nums, int left, int right) {
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E9B3A4>Ϊ 1 ʱ<><CAB1>ֹ
while (left < right) {
// <20>ڱ<EFBFBD><DAB1><EFBFBD><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD>
int pivot = partition(nums, left, right);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>н϶̵<CFB6><CCB5>Ǹ<EFBFBD>ִ<EFBFBD>п<EFBFBD><D0BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (pivot - left < right - pivot) {
quickSort(nums, left, pivot - 1); // <20>ݹ<EFBFBD><DDB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
left = pivot + 1; // ʣ<><CAA3>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ [pivot + 1, right]
} else {
quickSort(nums, pivot + 1, right); // <20>ݹ<EFBFBD><DDB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
right = pivot - 1; // ʣ<><CAA3>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ [left, pivot - 1]
}
}
}
};
/* Driver Code */
int main() {
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
vector<int> nums{2, 4, 1, 0, 3, 5};
QuickSort::quickSort(nums, 0, nums.size() - 1);
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɺ<EFBFBD> nums = ";
printVector(nums);
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>׼<EFBFBD><D7BC><EFBFBD>Ż<EFBFBD><C5BB><EFBFBD> */
vector<int> nums1 = {2, 4, 1, 0, 3, 5};
QuickSortMedian::quickSort(nums1, 0, nums1.size() - 1);
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD>׼<EFBFBD><EFBFBD><EFBFBD>Ż<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɺ<EFBFBD> nums = ";
printVector(nums);
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD>ݹ<EFBFBD><DDB9>Ż<EFBFBD><C5BB><EFBFBD> */
vector<int> nums2 = {2, 4, 1, 0, 3, 5};
QuickSortTailCall::quickSort(nums2, 0, nums2.size() - 1);
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD>ݹ<EFBFBD><EFBFBD>Ż<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɺ<EFBFBD> nums = ";
printVector(nums);
return 0;
}