A comprehensive, well-documented collection of fundamental algorithms and data structures implemented in Python and C++. This repository is designed for learning, reference, and interview preparation.
- Sorting Algorithms
- Searching Algorithms
- Graph Algorithms
- Data Structures
- Dynamic Programming
- Installation & Usage
- Complexity Analysis
| Algorithm | Time (Avg) | Time (Worst) | Space | Stable | Implementation |
|---|---|---|---|---|---|
| Bubble Sort | O(nΒ²) | O(nΒ²) | O(1) | β | sorting/bubble_sort.py |
| Quick Sort | O(n log n) | O(nΒ²) | O(log n) | β | sorting/quick_sort.cpp |
| Merge Sort | O(n log n) | O(n log n) | O(n) | β | sorting/merge_sort.py |
| Heap Sort | O(n log n) | O(n log n) | O(1) | β | sorting/heap_sort.cpp |
| Insertion Sort | O(n) | O(nΒ²) | O(1) | β | sorting/insertion_sort.py |
| Selection Sort | O(nΒ²) | O(nΒ²) | O(1) | β | sorting/selection_sort.py |
- Time: O(n)
- Space: O(1)
- Use Case: Unsorted arrays, small datasets
- File:
searching/linear_search.py
- Time: O(log n)
- Space: O(1)
- Prerequisites: Sorted array
- File:
searching/binary_search.cpp
- Time: O(βn)
- Space: O(1)
- File:
searching/jump_search.py
- Breadth-First Search (BFS) - O(V + E)
- Depth-First Search (DFS) - O(V + E)
- Topological Sort - O(V + E)
- Dijkstra's Algorithm - O((V + E) log V)
- Bellman-Ford - O(VE)
- Floyd-Warshall - O(VΒ³)
- Kruskal's Algorithm - O(E log E)
- Prim's Algorithm - O(VΒ²)
graphs/
βββ bfs.py
βββ dfs.cpp
βββ dijkstra.py
βββ bellman_ford.cpp
βββ kruskal.py
βββ prim.cpp
- Linked List - O(n) search, O(1) insertion
- Stack - LIFO, O(1) push/pop
- Queue - FIFO, O(1) enqueue/dequeue
- Binary Search Tree - O(log n) average operations
- AVL Tree - Self-balancing, O(log n) guaranteed
- Heap - O(log n) insertion/deletion
- Trie - O(m) string operations (m = string length)
- Hash Table - O(1) average lookup
- Hash Map - Dynamic key-value storage
- Graph - Adjacency List/Matrix representation
- Disjoint Set (Union-Find) - O(Ξ±(n)) amortized
| Problem | DP State | Time | Space | File |
|---|---|---|---|---|
| Fibonacci | fib[n] | O(n) | O(n) | dp/fibonacci.py |
| 0/1 Knapsack | dp[i][w] | O(nW) | O(nW) | dp/knapsack.cpp |
| Longest Common Subsequence | dp[i][j] | O(mn) | O(mn) | dp/lcs.py |
| Coin Change | dp[i] | O(n*amount) | O(amount) | dp/coin_change.py |
| Matrix Chain Mult. | dp[i][j] | O(nΒ³) | O(nΒ²) | dp/matrix_chain.cpp |
| Longest Increasing Subseq. | dp[i] | O(nΒ²) | O(n) | dp/lis.py |
git clone https://github.com/jalel-masmoudi/algorithm-library.git
cd algorithm-library# Run a sorting algorithm
python3 sorting/quick_sort.py
# Run with test cases
python3 -m pytest sorting/test_sorts.py
# Profile performance
python3 benchmark/compare_sorts.py# Compile
g++ -O2 -o binary_search searching/binary_search.cpp
# Run
./binary_search
# With optimization flags
g++ -O3 -std=c++17 -o dijkstra graphs/dijkstra.cpp
./dijkstraEvery implementation includes:
Algorithm: Quick Sort
Time Complexity:
- Best Case: O(n log n)
- Average Case: O(n log n)
- Worst Case: O(nΒ²) - when pivot is always smallest/largest
Space Complexity: O(log n) - recursion depth
Advantages:
β In-place sorting (O(1) extra space)
β Fast in practice
β Good cache locality
Disadvantages:
β Not stable
β Worst case O(nΒ²)
β Not adaptive
algorithm-library/
βββ sorting/
β βββ bubble_sort.py
β βββ quick_sort.cpp
β βββ merge_sort.py
β βββ test_sorts.py
βββ searching/
β βββ binary_search.cpp
β βββ linear_search.py
β βββ test_search.py
βββ graphs/
β βββ bfs.py
β βββ dijkstra.cpp
β βββ graph_utils.py
βββ data_structures/
β βββ linked_list.py
β βββ binary_tree.cpp
β βββ hash_table.py
βββ dp/
β βββ fibonacci.py
β βββ knapsack.cpp
β βββ lcs.py
βββ benchmark/
β βββ compare_sorts.py
βββ tests/
β βββ test_all.py
βββ README.md
Beginner β Start with:
- Sorting algorithms (Bubble, Merge, Quick)
- Binary search
- Basic data structures (List, Stack, Queue)
Intermediate β Learn:
- Graph algorithms (BFS, DFS)
- Dynamic programming basics
- Tree structures (BST, AVL)
Advanced β Master:
- Complex graph algorithms
- Advanced DP problems
- Optimization techniques
- O(1) - Constant time (best)
- O(log n) - Logarithmic
- O(n) - Linear
- O(n log n) - Linearithmic (optimal for sorting)
- O(nΒ²) - Quadratic
- O(2βΏ) - Exponential
- O(n!) - Factorial (worst)
- β Understand the problem before coding
- β Analyze time & space complexity
- β Consider edge cases
- β Write clean, readable code
- β Test thoroughly
- β Optimize after correctness
# Run all tests
python3 -m pytest tests/ -v
# Run specific test file
python3 -m pytest tests/test_sorts.py
# Generate coverage report
pytest --cov=. tests/Found a bug or want to add an algorithm? Contributions welcome!
- Fork the repository
- Create a feature branch (
git checkout -b feature/new-algorithm) - Add your algorithm with:
- Correct implementation
- Test cases
- Complexity analysis
- Documentation
- Submit a pull request
- Books: "Introduction to Algorithms" (CLRS)
- Websites: LeetCode, HackerRank, GeeksforGeeks
- Visualizers: VisuAlgo, Algorithm Visualizer
- Practice: Project Euler, CodeSignal
MIT License - Feel free to use for learning and projects!
Jalel Masmoudi
Computer Science Student | North American University of Sfax
π§ Contact: m.j.masmoudi1@gmail.com
Last Updated: November 2025
β If this helps you learn, please star the repository!