Skip to content

Commit af8c610

Browse files
committed
add tested C++ algorithm library
0 parents  commit af8c610

8 files changed

Lines changed: 705 additions & 0 deletions

File tree

.github/workflows/ci.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: ci
2+
on:
3+
push:
4+
pull_request:
5+
jobs:
6+
build-and-test:
7+
strategy:
8+
matrix:
9+
os: [ubuntu-latest, windows-latest]
10+
runs-on: ${{ matrix.os }}
11+
steps:
12+
- uses: actions/checkout@v4
13+
- run: cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
14+
- run: cmake --build build --config Release
15+
- run: ctest --test-dir build -C Release --output-on-failure

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
build/
2+
out/
3+
.vs/
4+
.vscode/
5+
*.user
6+
*.suo
7+
*.obj
8+
*.exe

CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
project(cpp_algorithms VERSION 1.0.0 LANGUAGES CXX)
3+
4+
set(CMAKE_CXX_STANDARD 20)
5+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
6+
set(CMAKE_CXX_EXTENSIONS OFF)
7+
8+
add_library(portfolio_algorithms INTERFACE)
9+
target_include_directories(portfolio_algorithms INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include)
10+
11+
if(MSVC)
12+
target_compile_options(portfolio_algorithms INTERFACE /W4 /permissive-)
13+
else()
14+
target_compile_options(portfolio_algorithms INTERFACE -Wall -Wextra -Wpedantic)
15+
endif()
16+
17+
enable_testing()
18+
add_executable(algorithm_tests tests/test_main.cpp)
19+
target_link_libraries(algorithm_tests PRIVATE portfolio_algorithms)
20+
add_test(NAME algorithm_tests COMMAND algorithm_tests)

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2026 Qiaoxi Guo
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# C++ Algorithms and Data Structures
2+
3+
A compact C++20 review library for interview fundamentals. It favors explainable implementations, edge-case tests, and complexity notes over a large dump of copied problem solutions.
4+
5+
## Included implementations
6+
7+
| Category | Implementations | Time complexity |
8+
| --- | --- | --- |
9+
| Search/sort | binary search, merge sort, quick sort, heap sort | `O(log n)` search; `O(n log n)` sort average/guaranteed as documented in code |
10+
| Data structures | binary min-heap, trie, disjoint set, LRU cache | heap `O(log n)`; trie `O(k)`; DSU amortized `O(alpha(n))`; LRU `O(1)` |
11+
| Graphs | BFS, DFS, Dijkstra, topological sort, cycle detection, connected components | `O(V + E)` except Dijkstra `O((V+E) log V)` |
12+
| Arrays/strings | two sum, sliding-window maximum, longest unique substring, KMP | `O(n)` |
13+
| Dynamic programming | coin change, LIS, 0/1 knapsack, edit distance | from `O(n log n)` LIS to `O(mn)` edit distance |
14+
| Trees | BST validation, level-order traversal, BST lowest common ancestor | `O(n)` or `O(h)` |
15+
16+
There are 25 focused implementations across two header files. The tests cover normal behavior, duplicates, missing values, graph cycles, deep BST violations, and cache eviction.
17+
18+
## Build and test
19+
20+
```bash
21+
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
22+
cmake --build build --config Release
23+
ctest --test-dir build -C Release --output-on-failure
24+
```
25+
26+
The GitHub Actions matrix runs on both Linux and Windows.
27+
28+
## Repository layout
29+
30+
- `include/portfolio/data_structures.hpp` — heap, trie, disjoint set, and LRU cache.
31+
- `include/portfolio/algorithms.hpp` — search, sorting, graphs, arrays, strings, DP, and tree algorithms.
32+
- `tests/test_main.cpp` — dependency-free executable test harness.
33+
34+
## My contributions
35+
36+
- Selected a bounded set of fundamentals relevant to new-graduate SDE interviews.
37+
- Implemented each algorithm from first principles in modern C++20.
38+
- Added explicit edge-case behavior and a cross-platform warning-enabled build.
39+
- Documented complexity and kept the test harness dependency-free for quick review.
40+
41+
## Study approach
42+
43+
For each implementation I practice explaining the invariant, complexity, failure cases, and one alternative—not memorizing the source. This repository supports interview preparation; it is not a substitute for timed problem solving.

0 commit comments

Comments
 (0)