Add cpp codes for the chapter
computational complexity, sorting, searching.
This commit is contained in:
@@ -10,6 +10,7 @@ class SolutionBruteForce {
|
||||
public:
|
||||
vector<int> twoSum(vector<int>& nums, int target) {
|
||||
int size = nums.size();
|
||||
// 两层循环,时间复杂度 O(n^2)
|
||||
for (int i = 0; i < size - 1; i++) {
|
||||
for (int j = i + 1; j < size; j++) {
|
||||
if (nums[i] + nums[j] == target)
|
||||
@@ -24,7 +25,9 @@ class SolutionHashMap {
|
||||
public:
|
||||
vector<int> twoSum(vector<int>& nums, int target) {
|
||||
int size = nums.size();
|
||||
// 辅助哈希表,空间复杂度 O(n)
|
||||
unordered_map<int, int> dic;
|
||||
// 单层循环,时间复杂度 O(n)
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (dic.find(target - nums[i]) != dic.end()) {
|
||||
return { dic[target - nums[i]], i };
|
||||
@@ -45,9 +48,13 @@ int main() {
|
||||
// 方法一
|
||||
SolutionBruteForce* slt1 = new SolutionBruteForce();
|
||||
vector<int> res = slt1->twoSum(nums, target);
|
||||
cout << "方法一 res = ";
|
||||
PrintUtil::printVector(res);
|
||||
// 方法二
|
||||
SolutionHashMap* slt2 = new SolutionHashMap();
|
||||
res = slt2->twoSum(nums, target);
|
||||
cout << "方法二 res = ";
|
||||
PrintUtil::printVector(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,3 +6,97 @@
|
||||
|
||||
#include "../include/include.hpp"
|
||||
|
||||
/* 函数 */
|
||||
int func() {
|
||||
// do something
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 常数阶 */
|
||||
void constant(int n) {
|
||||
// 常量、变量、对象占用 O(1) 空间
|
||||
const int a = 0;
|
||||
int b = 0;
|
||||
vector<int> nums(10000);
|
||||
ListNode* node = new ListNode(0);
|
||||
// 循环中的变量占用 O(1) 空间
|
||||
for (int i = 0; i < n; i++) {
|
||||
int c = 0;
|
||||
}
|
||||
// 循环中的函数占用 O(1) 空间
|
||||
for (int i = 0; i < n; i++) {
|
||||
func();
|
||||
}
|
||||
}
|
||||
|
||||
/* 线性阶 */
|
||||
void linear(int n) {
|
||||
// 长度为 n 的数组占用 O(n) 空间
|
||||
vector<int> nums(n);
|
||||
// 长度为 n 的列表占用 O(n) 空间
|
||||
vector<ListNode*> nodes;
|
||||
for (int i = 0; i < n; i++) {
|
||||
nodes.push_back(new ListNode(i));
|
||||
}
|
||||
// 长度为 n 的哈希表占用 O(n) 空间
|
||||
unordered_map<int, string> map;
|
||||
for (int i = 0; i < n; i++) {
|
||||
map[i] = to_string(i);
|
||||
}
|
||||
}
|
||||
|
||||
/* 线性阶(递归实现) */
|
||||
void linearRecur(int n) {
|
||||
cout << "递归 n = " << n << endl;
|
||||
if (n == 1) return;
|
||||
linearRecur(n - 1);
|
||||
}
|
||||
|
||||
/* 平方阶 */
|
||||
void quadratic(int n) {
|
||||
// 二维列表占用 O(n^2) 空间
|
||||
vector<vector<int>> numMatrix;
|
||||
for (int i = 0; i < n; i++) {
|
||||
vector<int> tmp;
|
||||
for (int j = 0; j < n; j++) {
|
||||
tmp.push_back(0);
|
||||
}
|
||||
numMatrix.push_back(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
/* 平方阶(递归实现) */
|
||||
int quadraticRecur(int n) {
|
||||
if (n <= 0) return 0;
|
||||
vector<int> nums(n);
|
||||
cout << "递归 n = " << n << " 中的 nums 长度 = " << nums.size() << endl;
|
||||
return quadraticRecur(n - 1);
|
||||
}
|
||||
|
||||
/* 指数阶(建立满二叉树) */
|
||||
TreeNode* buildTree(int n) {
|
||||
if (n == 0) return nullptr;
|
||||
TreeNode* root = new TreeNode(0);
|
||||
root->left = buildTree(n - 1);
|
||||
root->right = buildTree(n - 1);
|
||||
return root;
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int n = 5;
|
||||
// 常数阶
|
||||
constant(n);
|
||||
// 线性阶
|
||||
linear(n);
|
||||
linearRecur(n);
|
||||
// 平方阶
|
||||
quadratic(n);
|
||||
quadraticRecur(n);
|
||||
// 指数阶
|
||||
TreeNode* root = buildTree(n);
|
||||
PrintUtil::printTree(root);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,3 +6,161 @@
|
||||
|
||||
#include "../include/include.hpp"
|
||||
|
||||
/* 常数阶 */
|
||||
int constant(int n) {
|
||||
int count = 0;
|
||||
int size = 100000;
|
||||
for (int i = 0; i < size; i++)
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 线性阶 */
|
||||
int linear(int n) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 线性阶(遍历数组) */
|
||||
int arrayTraversal(vector<int>& nums) {
|
||||
int count = 0;
|
||||
// 循环次数与数组长度成正比
|
||||
for (int num : nums) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 平方阶 */
|
||||
int quadratic(int n) {
|
||||
int count = 0;
|
||||
// 循环次数与数组长度成平方关系
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 平方阶(冒泡排序) */
|
||||
int bubbleSort(vector<int>& nums) {
|
||||
int count = 0; // 计数器
|
||||
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
||||
for (int i = nums.size() - 1; i > 0; i--) {
|
||||
// 内循环:冒泡操作
|
||||
for (int j = 0; j < i; j++) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// 交换 nums[j] 与 nums[j + 1]
|
||||
int tmp = nums[j];
|
||||
nums[j] = nums[j + 1];
|
||||
nums[j + 1] = tmp;
|
||||
count += 3; // 元素交换包含 3 个单元操作
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 指数阶(循环实现) */
|
||||
int exponential(int n) {
|
||||
int count = 0, base = 1;
|
||||
// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < base; j++) {
|
||||
count++;
|
||||
}
|
||||
base *= 2;
|
||||
}
|
||||
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 指数阶(递归实现) */
|
||||
int expRecur(int n) {
|
||||
if (n == 1) return 1;
|
||||
return expRecur(n - 1) + expRecur(n - 1) + 1;
|
||||
}
|
||||
|
||||
/* 对数阶(循环实现) */
|
||||
int logarithmic(float n) {
|
||||
int count = 0;
|
||||
while (n > 1) {
|
||||
n = n / 2;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 对数阶(递归实现) */
|
||||
int logRecur(float n) {
|
||||
if (n <= 1) return 0;
|
||||
return logRecur(n / 2) + 1;
|
||||
}
|
||||
|
||||
/* 线性对数阶 */
|
||||
int linearLogRecur(float n) {
|
||||
if (n <= 1) return 1;
|
||||
int count = linearLogRecur(n / 2) +
|
||||
linearLogRecur(n / 2);
|
||||
for (int i = 0; i < n; i++) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 阶乘阶(递归实现) */
|
||||
int factorialRecur(int n) {
|
||||
if (n == 0) return 1;
|
||||
int count = 0;
|
||||
// 从 1 个分裂出 n 个
|
||||
for (int i = 0; i < n; i++) {
|
||||
count += factorialRecur(n - 1);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
// 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势
|
||||
int n = 8;
|
||||
cout << "输入数据大小 n = " << n << endl;
|
||||
|
||||
int count = constant(n);
|
||||
cout << "常数阶的计算操作数量 = " << count << endl;
|
||||
|
||||
count = linear(n);
|
||||
cout << "线性阶的计算操作数量 = " << count << endl;
|
||||
vector<int> arr(n);
|
||||
count = arrayTraversal(arr);
|
||||
cout << "线性阶(遍历数组)的计算操作数量 = " << count << endl;
|
||||
|
||||
count = quadratic(n);
|
||||
cout << "平方阶的计算操作数量 = " << count << endl;
|
||||
vector<int> nums(n);
|
||||
for (int i = 0; i < n; i++)
|
||||
nums[i] = n - i; // [n,n-1,...,2,1]
|
||||
count = bubbleSort(nums);
|
||||
cout << "平方阶(冒泡排序)的计算操作数量 = " << count << endl;
|
||||
|
||||
count = exponential(n);
|
||||
cout << "指数阶(循环实现)的计算操作数量 = " << count << endl;
|
||||
count = expRecur(n);
|
||||
cout << "指数阶(递归实现)的计算操作数量 = " << count << endl;
|
||||
|
||||
count = logarithmic((float) n);
|
||||
cout << "对数阶(循环实现)的计算操作数量 = " << count << endl;
|
||||
count = logRecur((float) n);
|
||||
cout << "对数阶(递归实现)的计算操作数量 = " << count << endl;
|
||||
|
||||
count = linearLogRecur((float) n);
|
||||
cout << "线性对数阶(递归实现)的计算操作数量 = " << count << endl;
|
||||
|
||||
count = factorialRecur(n);
|
||||
cout << "阶乘阶(递归实现)的计算操作数量 = " << count << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,3 +6,39 @@
|
||||
|
||||
#include "../include/include.hpp"
|
||||
|
||||
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
|
||||
vector<int> randomNumbers(int n) {
|
||||
vector<int> nums(n);
|
||||
// 生成数组 nums = { 1, 2, 3, ..., n }
|
||||
for (int i = 0; i < n; i++) {
|
||||
nums[i] = i + 1;
|
||||
}
|
||||
// 使用系统时间生成随机种子
|
||||
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
|
||||
// 随机打乱数组元素
|
||||
shuffle(nums.begin(), nums.end(), default_random_engine(seed));
|
||||
return nums;
|
||||
}
|
||||
|
||||
/* 查找数组 nums 中数字 1 所在索引 */
|
||||
int findOne(vector<int>& nums) {
|
||||
for (int i = 0; i < nums.size(); i++) {
|
||||
if (nums[i] == 1)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
int n = 100;
|
||||
vector<int> nums = randomNumbers(n);
|
||||
int index = findOne(nums);
|
||||
cout << "\n数组 [ 1, 2, ..., n ] 被打乱后 = ";
|
||||
PrintUtil::printVector(nums);
|
||||
cout << "数字 1 的索引为 " << index << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,3 +6,55 @@
|
||||
|
||||
#include "../include/include.hpp"
|
||||
|
||||
/* 二分查找(双闭区间) */
|
||||
int binarySearch(vector<int>& nums, int target) {
|
||||
// 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素
|
||||
int i = 0, j = nums.size() - 1;
|
||||
// 循环,当搜索区间为空时跳出(当 i > j 时为空)
|
||||
while (i <= j) {
|
||||
int m = (i + j) / 2; // 计算中点索引 m
|
||||
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j] 中
|
||||
i = m + 1;
|
||||
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m-1] 中
|
||||
j = m - 1;
|
||||
else // 找到目标元素,返回其索引
|
||||
return m;
|
||||
}
|
||||
// 未找到目标元素,返回 -1
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 二分查找(左闭右开) */
|
||||
int binarySearch1(vector<int>& nums, int target) {
|
||||
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||
int i = 0, j = nums.size();
|
||||
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
||||
while (i < j) {
|
||||
int m = (i + j) / 2; // 计算中点索引 m
|
||||
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j) 中
|
||||
i = m + 1;
|
||||
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m) 中
|
||||
j = m;
|
||||
else // 找到目标元素,返回其索引
|
||||
return m;
|
||||
}
|
||||
// 未找到目标元素,返回 -1
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int target = 6;
|
||||
vector<int> nums = { 1, 3, 6, 8, 12, 15, 23, 67, 70, 92 };
|
||||
|
||||
/* 二分查找(双闭区间) */
|
||||
int index = binarySearch(nums, target);
|
||||
cout << "目标元素 6 的索引 = " << index << endl;
|
||||
|
||||
/* 二分查找(左闭右开) */
|
||||
index = binarySearch1(nums, target);
|
||||
cout << "目标元素 6 的索引 = " << index << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,3 +6,49 @@
|
||||
|
||||
#include "../include/include.hpp"
|
||||
|
||||
/* 哈希查找(数组) */
|
||||
int hashingSearch(unordered_map<int, int> map, int target) {
|
||||
// 哈希表的 key: 目标元素,value: 索引
|
||||
// 若哈希表中无此 key ,返回 -1
|
||||
if (map.find(target) == map.end())
|
||||
return -1;
|
||||
return map[target];
|
||||
}
|
||||
|
||||
/* 哈希查找(链表) */
|
||||
ListNode* hashingSearch1(unordered_map<int, ListNode*> map, int target) {
|
||||
// 哈希表的 key: 目标结点值,value: 结点对象
|
||||
// 若哈希表中无此 key ,返回 nullptr
|
||||
if (map.find(target) == map.end())
|
||||
return nullptr;
|
||||
return map[target];
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int target = 3;
|
||||
|
||||
/* 哈希查找(数组) */
|
||||
vector<int> nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 };
|
||||
// 初始化哈希表
|
||||
unordered_map<int, int> map;
|
||||
for (int i = 0; i < nums.size(); i++) {
|
||||
map[nums[i]] = i; // key: 元素,value: 索引
|
||||
}
|
||||
int index = hashingSearch(map, target);
|
||||
cout << "目标元素 3 的索引 = " << index << endl;
|
||||
|
||||
/* 哈希查找(链表) */
|
||||
ListNode* head = vectorToLinkedList(nums);
|
||||
// 初始化哈希表
|
||||
unordered_map<int, ListNode*> map1;
|
||||
while (head != nullptr) {
|
||||
map1[head->val] = head; // key: 结点值,value: 结点
|
||||
head = head->next;
|
||||
}
|
||||
ListNode* node = hashingSearch1(map1, target);
|
||||
cout << "目标结点值 3 的对应结点对象为 " << node << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,3 +6,45 @@
|
||||
|
||||
#include "../include/include.hpp"
|
||||
|
||||
/* 线性查找(数组) */
|
||||
int linearSearch(vector<int>& nums, int target) {
|
||||
// 遍历数组
|
||||
for (int i = 0; i < nums.size(); i++) {
|
||||
// 找到目标元素,返回其索引
|
||||
if (nums[i] == target)
|
||||
return i;
|
||||
}
|
||||
// 未找到目标元素,返回 -1
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 线性查找(链表) */
|
||||
ListNode* linearSearch(ListNode* head, int target) {
|
||||
// 遍历链表
|
||||
while (head != nullptr) {
|
||||
// 找到目标结点,返回之
|
||||
if (head->val == target)
|
||||
return head;
|
||||
head = head->next;
|
||||
}
|
||||
// 未找到目标结点,返回 nullptr
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int target = 3;
|
||||
|
||||
/* 在数组中执行线性查找 */
|
||||
vector<int> nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 };
|
||||
int index = linearSearch(nums, target);
|
||||
cout << "目标元素 3 的索引 = " << index << endl;
|
||||
|
||||
/* 在链表中执行线性查找 */
|
||||
ListNode* head = vectorToLinkedList(nums);
|
||||
ListNode* node = linearSearch(head, target);
|
||||
cout << "目标结点值 3 的对应结点对象为 " << node << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,3 +6,53 @@
|
||||
|
||||
#include "../include/include.hpp"
|
||||
|
||||
/* 冒泡排序 */
|
||||
void bubbleSort(vector<int>& nums) {
|
||||
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
||||
for (int i = nums.size() - 1; i > 0; i--) {
|
||||
// 内循环:冒泡操作
|
||||
for (int j = 0; j < i; j++) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// 交换 nums[j] 与 nums[j + 1]
|
||||
int tmp = nums[j];
|
||||
nums[j] = nums[j + 1];
|
||||
nums[j + 1] = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 冒泡排序(标志优化)*/
|
||||
void bubbleSortWithFlag(vector<int>& nums) {
|
||||
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
||||
for (int i = nums.size() - 1; i > 0; i--) {
|
||||
bool flag = false; // 初始化标志位
|
||||
// 内循环:冒泡操作
|
||||
for (int j = 0; j < i; j++) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// 交换 nums[j] 与 nums[j + 1]
|
||||
int tmp = nums[j];
|
||||
nums[j] = nums[j + 1];
|
||||
nums[j + 1] = tmp;
|
||||
flag = true; // 记录交换元素
|
||||
}
|
||||
}
|
||||
if (!flag) break; // 此轮冒泡未交换任何元素,直接跳出
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
vector<int> nums = { 4, 1, 3, 1, 5, 2 };
|
||||
bubbleSort(nums);
|
||||
cout << "冒泡排序完成后 nums = ";
|
||||
PrintUtil::printVector(nums);
|
||||
|
||||
vector<int> nums1 = { 4, 1, 3, 1, 5, 2 };
|
||||
bubbleSortWithFlag(nums1);
|
||||
cout << "冒泡排序完成后 nums1 = ";
|
||||
PrintUtil::printVector(nums1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,3 +6,27 @@
|
||||
|
||||
#include "../include/include.hpp"
|
||||
|
||||
/* 插入排序 */
|
||||
void insertionSort(vector<int>& nums) {
|
||||
// 外循环:base = nums[1], nums[2], ..., nums[n-1]
|
||||
for (int i = 1; i < nums.size(); i++) {
|
||||
int base = nums[i], j = i - 1;
|
||||
// 内循环:将 base 插入到左边的正确位置
|
||||
while (j >= 0 && nums[j] > base) {
|
||||
nums[j + 1] = nums[j]; // 1. 将 nums[j] 向右移动一位
|
||||
j--;
|
||||
}
|
||||
nums[j + 1] = base; // 2. 将 base 赋值到正确位置
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
vector<int> nums = { 4, 1, 3, 1, 5, 2 };
|
||||
insertionSort(nums);
|
||||
cout << "插入排序完成后 nums = ";
|
||||
PrintUtil::printVector(nums);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,3 +6,54 @@
|
||||
|
||||
#include "../include/include.hpp"
|
||||
|
||||
/**
|
||||
* 合并左子数组和右子数组
|
||||
* 左子数组区间 [left, mid]
|
||||
* 右子数组区间 [mid + 1, right]
|
||||
*/
|
||||
void merge(vector<int>& nums, int left, int mid, int right) {
|
||||
// 初始化辅助数组
|
||||
vector<int> tmp(nums.begin() + left, nums.begin() + right + 1);
|
||||
// 左子数组的起始索引和结束索引
|
||||
int leftStart = left - left, leftEnd = mid - left;
|
||||
// 右子数组的起始索引和结束索引
|
||||
int rightStart = mid + 1 - left, rightEnd = right - left;
|
||||
// i, j 分别指向左子数组、右子数组的首元素
|
||||
int i = leftStart, j = rightStart;
|
||||
// 通过覆盖原数组 nums 来合并左子数组和右子数组
|
||||
for (int k = left; k <= right; k++) {
|
||||
// 若 “左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
||||
if (i > leftEnd)
|
||||
nums[k] = tmp[j++];
|
||||
// 否则,若 “右子数组已全部合并完” 或 “左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++
|
||||
else if (j > rightEnd || tmp[i] <= tmp[j])
|
||||
nums[k] = tmp[i++];
|
||||
// 否则,若 “左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
|
||||
else
|
||||
nums[k] = tmp[j++];
|
||||
}
|
||||
}
|
||||
|
||||
/* 归并排序 */
|
||||
void mergeSort(vector<int>& nums, int left, int right) {
|
||||
// 终止条件
|
||||
if (left >= right) return; // 当子数组长度为 1 时终止递归
|
||||
// 划分阶段
|
||||
int mid = (left + right) / 2; // 计算中点
|
||||
mergeSort(nums, left, mid); // 递归左子数组
|
||||
mergeSort(nums, mid + 1, right); // 递归右子数组
|
||||
// 合并阶段
|
||||
merge(nums, left, mid, right);
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 归并排序 */
|
||||
vector<int> nums = { 7, 3, 2, 6, 0, 1, 5, 4 };
|
||||
mergeSort(nums, 0, nums.size() - 1);
|
||||
cout << "归并排序完成后 nums = ";
|
||||
PrintUtil::printVector(nums);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,3 +6,164 @@
|
||||
|
||||
#include "../include/include.hpp"
|
||||
|
||||
/* 快速排序类 */
|
||||
class QuickSort {
|
||||
private:
|
||||
/* 元素交换 */
|
||||
static void swap(vector<int>& nums, int i, int j) {
|
||||
int tmp = nums[i];
|
||||
nums[i] = nums[j];
|
||||
nums[j] = tmp;
|
||||
}
|
||||
|
||||
/* 哨兵划分 */
|
||||
static int partition(vector<int>& nums, int left, int right) {
|
||||
// 以 nums[left] 作为基准数
|
||||
int i = left, j = right;
|
||||
while (i < j) {
|
||||
while (i < j && nums[j] >= nums[left])
|
||||
j--; // 从右向左找首个小于基准数的元素
|
||||
while (i < j && nums[i] <= nums[left])
|
||||
i++; // 从左向右找首个大于基准数的元素
|
||||
swap(nums, i, j); // 交换这两个元素
|
||||
}
|
||||
swap(nums, i, left); // 将基准数交换至两子数组的分界线
|
||||
return i; // 返回基准数的索引
|
||||
}
|
||||
|
||||
public:
|
||||
/* 快速排序 */
|
||||
static void quickSort(vector<int>& nums, int left, int right) {
|
||||
// 子数组长度为 1 时终止递归
|
||||
if (left >= right)
|
||||
return;
|
||||
// 哨兵划分
|
||||
int pivot = partition(nums, left, right);
|
||||
// 递归左子数组、右子数组
|
||||
quickSort(nums, left, pivot - 1);
|
||||
quickSort(nums, pivot + 1, right);
|
||||
}
|
||||
};
|
||||
|
||||
/* 快速排序类(中位基准数优化) */
|
||||
class QuickSortMedian {
|
||||
private:
|
||||
/* 元素交换 */
|
||||
static void swap(vector<int>& nums, int i, int j) {
|
||||
int tmp = nums[i];
|
||||
nums[i] = nums[j];
|
||||
nums[j] = tmp;
|
||||
}
|
||||
|
||||
/* 选取三个元素的中位数 */
|
||||
static int medianThree(vector<int>& nums, int left, int mid, int right) {
|
||||
// 使用了异或操作来简化代码
|
||||
// 异或规则为 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;
|
||||
}
|
||||
|
||||
/* 哨兵划分(三数取中值) */
|
||||
static int partition(vector<int>& nums, int left, int right) {
|
||||
// 选取三个候选元素的中位数
|
||||
int med = medianThree(nums, left, (left + right) / 2, right);
|
||||
// 将中位数交换至数组最左端
|
||||
swap(nums, left, med);
|
||||
// 以 nums[left] 作为基准数
|
||||
int i = left, j = right;
|
||||
while (i < j) {
|
||||
while (i < j && nums[j] >= nums[left])
|
||||
j--; // 从右向左找首个小于基准数的元素
|
||||
while (i < j && nums[i] <= nums[left])
|
||||
i++; // 从左向右找首个大于基准数的元素
|
||||
swap(nums, i, j); // 交换这两个元素
|
||||
}
|
||||
swap(nums, i, left); // 将基准数交换至两子数组的分界线
|
||||
return i; // 返回基准数的索引
|
||||
}
|
||||
|
||||
public:
|
||||
/* 快速排序 */
|
||||
static void quickSort(vector<int>& nums, int left, int right) {
|
||||
// 子数组长度为 1 时终止递归
|
||||
if (left >= right)
|
||||
return;
|
||||
// 哨兵划分
|
||||
int pivot = partition(nums, left, right);
|
||||
// 递归左子数组、右子数组
|
||||
quickSort(nums, left, pivot - 1);
|
||||
quickSort(nums, pivot + 1, right);
|
||||
}
|
||||
};
|
||||
|
||||
/* 快速排序类(尾递归优化) */
|
||||
class QuickSortTailCall {
|
||||
private:
|
||||
/* 元素交换 */
|
||||
static void swap(vector<int>& nums, int i, int j) {
|
||||
int tmp = nums[i];
|
||||
nums[i] = nums[j];
|
||||
nums[j] = tmp;
|
||||
}
|
||||
|
||||
/* 哨兵划分 */
|
||||
static int partition(vector<int>& nums, int left, int right) {
|
||||
// 以 nums[left] 作为基准数
|
||||
int i = left, j = right;
|
||||
while (i < j) {
|
||||
while (i < j && nums[j] >= nums[left])
|
||||
j--; // 从右向左找首个小于基准数的元素
|
||||
while (i < j && nums[i] <= nums[left])
|
||||
i++; // 从左向右找首个大于基准数的元素
|
||||
swap(nums, i, j); // 交换这两个元素
|
||||
}
|
||||
swap(nums, i, left); // 将基准数交换至两子数组的分界线
|
||||
return i; // 返回基准数的索引
|
||||
}
|
||||
|
||||
public:
|
||||
/* 快速排序(尾递归优化) */
|
||||
static void quickSort(vector<int>& nums, int left, int right) {
|
||||
// 子数组长度为 1 时终止
|
||||
while (left < right) {
|
||||
// 哨兵划分操作
|
||||
int pivot = partition(nums, left, right);
|
||||
// 对两个子数组中较短的那个执行快排
|
||||
if (pivot - left < right - pivot) {
|
||||
quickSort(nums, left, pivot - 1); // 递归排序左子数组
|
||||
left = pivot + 1; // 剩余待排序区间为 [pivot + 1, right]
|
||||
} else {
|
||||
quickSort(nums, pivot + 1, right); // 递归排序右子数组
|
||||
right = pivot - 1; // 剩余待排序区间为 [left, pivot - 1]
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 快速排序 */
|
||||
vector<int> nums { 2, 4, 1, 0, 3, 5 };
|
||||
QuickSort::quickSort(nums, 0, nums.size() - 1);
|
||||
cout << "快速排序完成后 nums = ";
|
||||
PrintUtil::printVector(nums);
|
||||
|
||||
/* 快速排序(中位基准数优化) */
|
||||
vector<int> nums1 = { 2, 4, 1, 0, 3, 5 };
|
||||
QuickSortMedian::quickSort(nums1, 0, nums1.size() - 1);
|
||||
cout << "快速排序(中位基准数优化)完成后 nums = ";
|
||||
PrintUtil::printVector(nums);
|
||||
|
||||
/* 快速排序(尾递归优化) */
|
||||
vector<int> nums2 = { 2, 4, 1, 0, 3, 5 };
|
||||
QuickSortTailCall::quickSort(nums2, 0, nums2.size() - 1);
|
||||
cout << "快速排序(尾递归优化)完成后 nums = ";
|
||||
PrintUtil::printVector(nums);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ struct ListNode {
|
||||
* @param list
|
||||
* @return ListNode*
|
||||
*/
|
||||
ListNode* vectorToLinkedList(vector<int> list) {
|
||||
ListNode* vectorToLinkedList(vector<int>& list) {
|
||||
ListNode *dum = new ListNode(0);
|
||||
ListNode *head = dum;
|
||||
for (int val : list) {
|
||||
|
||||
@@ -23,7 +23,7 @@ struct TreeNode {
|
||||
* @param list
|
||||
* @return TreeNode*
|
||||
*/
|
||||
TreeNode* vectorToTree(vector<int> list) {
|
||||
TreeNode* vectorToTree(vector<int>& list) {
|
||||
TreeNode *root = new TreeNode(list[0]);
|
||||
queue<TreeNode*> que;
|
||||
que.emplace(root);
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <set>
|
||||
#include <random>
|
||||
|
||||
#include "ListNode.hpp"
|
||||
#include "TreeNode.hpp"
|
||||
|
||||
Reference in New Issue
Block a user