Skip to content

jalel-masmoudi/Algorithm-Library

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Algorithm Library πŸ“š

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.


πŸ“‹ Table of Contents


πŸ”„ Sorting Algorithms

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

πŸ” Searching Algorithms

Linear Search

  • Time: O(n)
  • Space: O(1)
  • Use Case: Unsorted arrays, small datasets
  • File: searching/linear_search.py

Binary Search

  • Time: O(log n)
  • Space: O(1)
  • Prerequisites: Sorted array
  • File: searching/binary_search.cpp

Jump Search

  • Time: O(√n)
  • Space: O(1)
  • File: searching/jump_search.py

🌐 Graph Algorithms

Traversal

  • Breadth-First Search (BFS) - O(V + E)
  • Depth-First Search (DFS) - O(V + E)
  • Topological Sort - O(V + E)

Shortest Path

  • Dijkstra's Algorithm - O((V + E) log V)
  • Bellman-Ford - O(VE)
  • Floyd-Warshall - O(VΒ³)

Minimum Spanning Tree

  • Kruskal's Algorithm - O(E log E)
  • Prim's Algorithm - O(VΒ²)

Implementation Files**

graphs/
β”œβ”€β”€ bfs.py
β”œβ”€β”€ dfs.cpp
β”œβ”€β”€ dijkstra.py
β”œβ”€β”€ bellman_ford.cpp
β”œβ”€β”€ kruskal.py
└── prim.cpp

πŸ“¦ Data Structures

Basic Structures

  • Linked List - O(n) search, O(1) insertion
  • Stack - LIFO, O(1) push/pop
  • Queue - FIFO, O(1) enqueue/dequeue

Tree Structures

  • 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-Based

  • Hash Table - O(1) average lookup
  • Hash Map - Dynamic key-value storage

Advanced

  • Graph - Adjacency List/Matrix representation
  • Disjoint Set (Union-Find) - O(Ξ±(n)) amortized

βš™οΈ Dynamic Programming

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

πŸš€ Installation & Usage

Clone Repository

git clone https://github.com/jalel-masmoudi/algorithm-library.git
cd algorithm-library

Python Examples

# 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

C++ Examples

# 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
./dijkstra

πŸ“Š Complexity Analysis

Every implementation includes:

Example: Quick Sort

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

πŸ“š Structure

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

🎯 Learning Path

Beginner β†’ Start with:

  1. Sorting algorithms (Bubble, Merge, Quick)
  2. Binary search
  3. Basic data structures (List, Stack, Queue)

Intermediate β†’ Learn:

  1. Graph algorithms (BFS, DFS)
  2. Dynamic programming basics
  3. Tree structures (BST, AVL)

Advanced β†’ Master:

  1. Complex graph algorithms
  2. Advanced DP problems
  3. Optimization techniques

πŸ’‘ Key Concepts

Big O Notation

  • 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)

Practical Tips

  • βœ… Understand the problem before coding
  • βœ… Analyze time & space complexity
  • βœ… Consider edge cases
  • βœ… Write clean, readable code
  • βœ… Test thoroughly
  • βœ… Optimize after correctness

πŸ§ͺ Testing

# 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/

🀝 Contributing

Found a bug or want to add an algorithm? Contributions welcome!

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/new-algorithm)
  3. Add your algorithm with:
    • Correct implementation
    • Test cases
    • Complexity analysis
    • Documentation
  4. Submit a pull request

πŸ“– Resources

  • Books: "Introduction to Algorithms" (CLRS)
  • Websites: LeetCode, HackerRank, GeeksforGeeks
  • Visualizers: VisuAlgo, Algorithm Visualizer
  • Practice: Project Euler, CodeSignal

πŸ“ License

MIT License - Feel free to use for learning and projects!


πŸ‘€ Author

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!

About

Comprehensive collection of algorithms and data structures implemented in Python and C++

Topics

Resources

License

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors