Skip to content

Commit

Permalink
Merge branch 'main' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
krahets committed May 6, 2024
2 parents 3947dc1 + 8e60d12 commit f111249
Show file tree
Hide file tree
Showing 112 changed files with 6,994 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/chapter_heap/heap.md
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,6 @@

## 堆的常见应用

- **优先队列**:堆通常作为实现优先队列的首选数据结构,其入队和出队操作的时间复杂度均为 $O(\log n)$ ,而建队操作为 $O(n)$ ,这些操作都非常高效。
- **优先队列**:堆通常作为实现优先队列的首选数据结构,其入队和出队操作的时间复杂度均为 $O(\log n)$ ,而建堆操作为 $O(n)$ ,这些操作都非常高效。
- **堆排序**:给定一组数据,我们可以用它们建立一个堆,然后不断地执行元素出堆操作,从而得到有序数据。然而,我们通常会使用一种更优雅的方式实现堆排序,详见“堆排序”章节。
- **获取最大的 $k$ 个元素**:这是一个经典的算法问题,同时也是一种典型应用,例如选择热度前 10 的新闻作为微博热搜,选取销量前 10 的商品等。
10 changes: 10 additions & 0 deletions en/codes/cpp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Ignore all
*
# Unignore all with extensions
!*.*
# Unignore all dirs
!*/

*.dSYM/

build/
20 changes: 20 additions & 0 deletions en/codes/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
cmake_minimum_required(VERSION 3.10)
project(hello_algo CXX)

set(CMAKE_CXX_STANDARD 11)

include_directories(./include)

add_subdirectory(chapter_computational_complexity)
add_subdirectory(chapter_array_and_linkedlist)
add_subdirectory(chapter_stack_and_queue)
add_subdirectory(chapter_hashing)
add_subdirectory(chapter_tree)
add_subdirectory(chapter_heap)
add_subdirectory(chapter_graph)
add_subdirectory(chapter_searching)
add_subdirectory(chapter_sorting)
add_subdirectory(chapter_divide_and_conquer)
add_subdirectory(chapter_backtracking)
add_subdirectory(chapter_dynamic_programming)
add_subdirectory(chapter_greedy)
4 changes: 4 additions & 0 deletions en/codes/cpp/chapter_array_and_linkedlist/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
add_executable(array array.cpp)
add_executable(linked_list linked_list.cpp)
add_executable(list list.cpp)
add_executable(my_list my_list.cpp)
113 changes: 113 additions & 0 deletions en/codes/cpp/chapter_array_and_linkedlist/array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* File: array.cpp
* Created Time: 2022-11-25
* Author: krahets ([email protected])
*/

#include "../utils/common.hpp"

/* Random access to elements */
int randomAccess(int *nums, int size) {
// Randomly select a number in the range [0, size)
int randomIndex = rand() % size;
// Retrieve and return a random element
int randomNum = nums[randomIndex];
return randomNum;
}

/* Extend array length */
int *extend(int *nums, int size, int enlarge) {
// Initialize an extended length array
int *res = new int[size + enlarge];
// Copy all elements from the original array to the new array
for (int i = 0; i < size; i++) {
res[i] = nums[i];
}
// Free memory
delete[] nums;
// Return the new array after expansion
return res;
}

/* Insert element num at `index` */
void insert(int *nums, int size, int num, int index) {
// Move all elements after `index` one position backward
for (int i = size - 1; i > index; i--) {
nums[i] = nums[i - 1];
}
// Assign num to the element at index
nums[index] = num;
}

/* Remove the element at `index` */
void remove(int *nums, int size, int index) {
// Move all elements after `index` one position forward
for (int i = index; i < size - 1; i++) {
nums[i] = nums[i + 1];
}
}

/* Traverse array */
void traverse(int *nums, int size) {
int count = 0;
// Traverse array by index
for (int i = 0; i < size; i++) {
count += nums[i];
}
}

/* Search for a specified element in the array */
int find(int *nums, int size, int target) {
for (int i = 0; i < size; i++) {
if (nums[i] == target)
return i;
}
return -1;
}

/* Driver Code */
int main() {
/* Initialize an array */
int size = 5;
int *arr = new int[size];
cout << "Array arr = ";
printArray(arr, size);

int *nums = new int[size]{1, 3, 2, 5, 4};
cout << "Array nums = ";
printArray(nums, size);

/* Random access */
int randomNum = randomAccess(nums, size);
cout << "Get a random element from nums = " << randomNum << endl;

/* Length extension */
int enlarge = 3;
nums = extend(nums, size, enlarge);
size += enlarge;
cout << "Extend the array length to 8, resulting in nums = ";
printArray(nums, size);

/* Insert element */
insert(nums, size, 6, 3);
cout << "Insert the number 6 at index 3, resulting in nums = ";
printArray(nums, size);

/* Remove element */
remove(nums, size, 2);
cout << "Remove the element at index 2, resulting in nums = ";
printArray(nums, size);

/* Traverse array */
traverse(nums, size);

/* Search for elements */
int index = find(nums, size, 3);
cout << "Find element 3 in nums, index = " << index << endl;

// Free memory
delete[] arr;
delete[] nums;

return 0;
}
89 changes: 89 additions & 0 deletions en/codes/cpp/chapter_array_and_linkedlist/linked_list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* File: linked_list.cpp
* Created Time: 2022-11-25
* Author: krahets ([email protected])
*/

#include "../utils/common.hpp"

/* Insert node P after node n0 in the linked list */
void insert(ListNode *n0, ListNode *P) {
ListNode *n1 = n0->next;
P->next = n1;
n0->next = P;
}

/* Remove the first node after node n0 in the linked list */
void remove(ListNode *n0) {
if (n0->next == nullptr)
return;
// n0 -> P -> n1
ListNode *P = n0->next;
ListNode *n1 = P->next;
n0->next = n1;
// Free memory
delete P;
}

/* Access the node at `index` in the linked list */
ListNode *access(ListNode *head, int index) {
for (int i = 0; i < index; i++) {
if (head == nullptr)
return nullptr;
head = head->next;
}
return head;
}

/* Search for the first node with value target in the linked list */
int find(ListNode *head, int target) {
int index = 0;
while (head != nullptr) {
if (head->val == target)
return index;
head = head->next;
index++;
}
return -1;
}

/* Driver Code */
int main() {
/* Initialize linked list */
// Initialize each node
ListNode *n0 = new ListNode(1);
ListNode *n1 = new ListNode(3);
ListNode *n2 = new ListNode(2);
ListNode *n3 = new ListNode(5);
ListNode *n4 = new ListNode(4);
// Build references between nodes
n0->next = n1;
n1->next = n2;
n2->next = n3;
n3->next = n4;
cout << "The initialized linked list is" << endl;
printLinkedList(n0);

/* Insert node */
insert(n0, new ListNode(0));
cout << "Linked list after inserting the node is" << endl;
printLinkedList(n0);

/* Remove node */
remove(n0);
cout << "Linked list after removing the node is" << endl;
printLinkedList(n0);

/* Access node */
ListNode *node = access(n0, 3);
cout << "The value of the node at index 3 in the linked list = " << node->val << endl;

/* Search node */
int index = find(n0, 2);
cout << "The index of the node with value 2 in the linked list = " << index << endl;

// Free memory
freeMemoryLinkedList(n0);

return 0;
}
72 changes: 72 additions & 0 deletions en/codes/cpp/chapter_array_and_linkedlist/list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* File: list.cpp
* Created Time: 2022-11-25
* Author: krahets ([email protected])
*/

#include "../utils/common.hpp"

/* Driver Code */
int main() {
/* Initialize list */
vector<int> nums = {1, 3, 2, 5, 4};
cout << "List nums = ";
printVector(nums);

/* Access element */
int num = nums[1];
cout << "Access the element at index 1, obtained num = " << num << endl;

/* Update element */
nums[1] = 0;
cout << "Update the element at index 1 to 0, resulting in nums = ";
printVector(nums);

/* Clear list */
nums.clear();
cout << "After clearing the list, nums = ";
printVector(nums);

/* Add element at the end */
nums.push_back(1);
nums.push_back(3);
nums.push_back(2);
nums.push_back(5);
nums.push_back(4);
cout << "After adding elements, nums = ";
printVector(nums);

/* Insert element in the middle */
nums.insert(nums.begin() + 3, 6);
cout << "Insert the number 6 at index 3, resulting in nums = ";
printVector(nums);

/* Remove element */
nums.erase(nums.begin() + 3);
cout << "Remove the element at index 3, resulting in nums = ";
printVector(nums);

/* Traverse the list by index */
int count = 0;
for (int i = 0; i < nums.size(); i++) {
count += nums[i];
}
/* Traverse the list elements */
count = 0;
for (int x : nums) {
count += x;
}

/* Concatenate two lists */
vector<int> nums1 = {6, 8, 7, 10, 9};
nums.insert(nums.end(), nums1.begin(), nums1.end());
cout << "Concatenate list nums1 to nums, resulting in nums = ";
printVector(nums);

/* Sort list */
sort(nums.begin(), nums.end());
cout << "After sorting the list, nums = ";
printVector(nums);

return 0;
}
Loading

0 comments on commit f111249

Please sign in to comment.