Convert Figma logo to code with AI

TheAlgorithms logoPython

All Algorithms implemented in Python

183,979
44,287
183,979
236

Top Related Projects

Learn how to design large-scale systems. Prep for the system design interview. Includes Anki flashcards.

An opinionated list of awesome Python frameworks, libraries, software and resources.

Minimal examples of data structures and algorithms in Python

30,913

My Python Examples

A collection of design patterns/idioms in Python

:computer: Data Structures and Algorithms in Python

Quick Overview

TheAlgorithms/Python is a comprehensive collection of algorithms and data structures implemented in Python. It serves as an educational resource for learning about various algorithms, their implementations, and applications. The repository covers a wide range of topics, including sorting, searching, machine learning, cryptography, and more.

Pros

  • Extensive collection of algorithms and data structures
  • Well-organized and categorized for easy navigation
  • Educational resource with clear implementations and comments
  • Active community and regular updates

Cons

  • Some implementations may not be optimized for production use
  • Lack of comprehensive documentation for each algorithm
  • Inconsistent coding style across different contributions
  • May not always use the most up-to-date Python features

Code Examples

  1. Binary Search implementation:
def binary_search(array, target):
    left, right = 0, len(array) - 1
    while left <= right:
        mid = (left + right) // 2
        if array[mid] == target:
            return mid
        elif array[mid] < target:
            left = mid + 1
        else:
            right = mid - 1
    return -1
  1. Bubble Sort implementation:
def bubble_sort(array):
    n = len(array)
    for i in range(n):
        for j in range(0, n - i - 1):
            if array[j] > array[j + 1]:
                array[j], array[j + 1] = array[j + 1], array[j]
    return array
  1. Fibonacci sequence using dynamic programming:
def fibonacci(n):
    if n <= 1:
        return n
    fib = [0] * (n + 1)
    fib[1] = 1
    for i in range(2, n + 1):
        fib[i] = fib[i - 1] + fib[i - 2]
    return fib[n]

Getting Started

To use the algorithms from TheAlgorithms/Python repository:

  1. Clone the repository:

    git clone https://github.com/TheAlgorithms/Python.git
    
  2. Navigate to the desired algorithm's directory.

  3. Import and use the algorithm in your Python script:

    from sorting.bubble_sort import bubble_sort
    
    arr = [64, 34, 25, 12, 22, 11, 90]
    sorted_arr = bubble_sort(arr)
    print(sorted_arr)
    

Note: Make sure to review the implementation and adapt it to your specific needs before using it in production environments.

Competitor Comparisons

Learn how to design large-scale systems. Prep for the system design interview. Includes Anki flashcards.

Pros of system-design-primer

  • Focuses on system design concepts and principles, providing a comprehensive guide for software architects and engineers
  • Includes visual aids, diagrams, and real-world examples to illustrate complex system design concepts
  • Covers a wide range of topics, from scalability to load balancing and caching strategies

Cons of system-design-primer

  • Limited hands-on coding examples compared to Python, which provides implementations for various algorithms
  • May be less suitable for beginners learning programming fundamentals
  • Primarily text-based content, which may not appeal to all learning styles

Code Comparison

system-design-primer (SQL example):

SELECT
    UserID,
    COUNT(*) AS 'Total Orders'
FROM
    Orders
GROUP BY
    UserID
HAVING
    COUNT(*) > 100

Python (Bubble Sort algorithm):

def bubble_sort(arr):
    n = len(arr)
    for i in range(n):
        for j in range(0, n - i - 1):
            if arr[j] > arr[j + 1]:
                arr[j], arr[j + 1] = arr[j + 1], arr[j]
    return arr

An opinionated list of awesome Python frameworks, libraries, software and resources.

Pros of awesome-python

  • Comprehensive curated list of Python frameworks, libraries, and resources
  • Organized by categories, making it easy to find specific tools or libraries
  • Regularly updated with community contributions

Cons of awesome-python

  • Doesn't contain actual code implementations
  • May be overwhelming for beginners due to the sheer number of resources listed

Code comparison

Not applicable, as awesome-python doesn't contain code implementations. Python, on the other hand, provides actual algorithm implementations. For example:

Python (Binary Search implementation):

def binary_search(arr, x):
    low = 0
    high = len(arr) - 1
    while low <= high:
        mid = (low + high) // 2
        if arr[mid] < x:
            low = mid + 1
        elif arr[mid] > x:
            high = mid - 1
        else:
            return mid
    return -1

awesome-python (Example of a resource listing):

## Algorithms and Design Patterns

- [algorithms](https://github.com/keon/algorithms) - Minimal examples of data structures and algorithms.
- [PyPattyrn](https://github.com/tylerlaberge/PyPattyrn) - A simple yet effective library for implementing common design patterns.

Minimal examples of data structures and algorithms in Python

Pros of algorithms

  • More focused on data structures and algorithms specifically
  • Includes implementations in multiple languages (Python, JavaScript, C++)
  • Provides a cleaner, more organized directory structure

Cons of algorithms

  • Less comprehensive coverage of algorithms compared to Python
  • Lower community engagement (fewer stars, forks, and contributors)
  • Less frequently updated and maintained

Code Comparison

Python implementation of binary search:

# From algorithms
def binary_search(array, target):
    lower = 0
    upper = len(array)
    while lower < upper:
        x = lower + (upper - lower) // 2
        val = array[x]
        if target == val:
            return x
        elif target > val:
            if lower == x:
                break
            lower = x
        elif target < val:
            upper = x
# From Python
def binary_search(array, target):
    left, right = 0, len(array) - 1
    while left <= right:
        mid = (left + right) // 2
        if array[mid] == target:
            return mid
        elif array[mid] < target:
            left = mid + 1
        else:
            right = mid - 1
    return -1

Both implementations are similar, but Python's version is slightly more concise and follows common conventions more closely.

30,913

My Python Examples

Pros of Python (geekcomputers)

  • More diverse range of scripts and applications
  • Includes practical, real-world examples
  • Easier for beginners to understand and apply

Cons of Python (geekcomputers)

  • Less structured organization
  • Inconsistent coding style across contributions
  • Limited focus on algorithmic implementations

Code Comparison

Python (TheAlgorithms):

def bubble_sort(array):
    n = len(array)
    for i in range(n):
        for j in range(0, n - i - 1):
            if array[j] > array[j + 1]:
                array[j], array[j + 1] = array[j + 1], array[j]
    return array

Python (geekcomputers):

def bubble(list_a):
    indexing_length = len(list_a) - 1
    sorted = False
    while not sorted:
        sorted = True
        for i in range(0, indexing_length):
            if list_a[i] > list_a[i+1]:
                sorted = False
                list_a[i], list_a[i+1] = list_a[i+1], list_a[i]
    return list_a

The Python repository focuses on clean, educational implementations of algorithms, while the Python (geekcomputers) repository offers a wider variety of practical scripts and applications. The code comparison shows different approaches to implementing a bubble sort algorithm, with TheAlgorithms version being more concise and Python (geekcomputers) version including additional optimizations.

A collection of design patterns/idioms in Python

Pros of python-patterns

  • Focuses specifically on design patterns, providing a deeper dive into software architecture concepts
  • Includes UML diagrams for visual representation of patterns
  • Offers real-world examples and use cases for each pattern

Cons of python-patterns

  • Smaller community and less frequent updates compared to Python
  • Limited scope, covering only design patterns rather than a wide range of algorithms
  • Fewer code examples and implementations

Code Comparison

Python (Bubble Sort implementation):

def bubble_sort(array):
    n = len(array)
    for i in range(n):
        for j in range(0, n - i - 1):
            if array[j] > array[j + 1]:
                array[j], array[j + 1] = array[j + 1], array[j]
    return array

python-patterns (Singleton pattern implementation):

class Singleton:
    _instance = None
    def __new__(cls):
        if cls._instance is None:
            cls._instance = super().__new__(cls)
        return cls._instance

The code examples highlight the different focus areas of each repository. Python provides implementations of various algorithms, while python-patterns demonstrates design pattern implementations in Python.

:computer: Data Structures and Algorithms in Python

Pros of Algorithms

  • More focused on specific algorithm implementations
  • Cleaner, more concise codebase
  • Better documentation for individual algorithms

Cons of Algorithms

  • Less comprehensive coverage of algorithms
  • Not as actively maintained or updated
  • Fewer contributors and community involvement

Code Comparison

Python (from TheAlgorithms):

def bubble_sort(array):
    n = len(array)
    for i in range(n):
        for j in range(n - i - 1):
            if array[j] > array[j + 1]:
                array[j], array[j + 1] = array[j + 1], array[j]
    return array

Algorithms (from prakhar1989):

def bubble_sort(lst):
    for passnum in range(len(lst)-1,0,-1):
        for i in range(passnum):
            if lst[i] > lst[i+1]:
                lst[i], lst[i+1] = lst[i+1], lst[i]
    return lst

Both implementations are similar, but Algorithms uses a reverse range for the outer loop, potentially offering slight performance benefits in some cases. However, Python provides a more straightforward and readable implementation.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

The Algorithms - Python

Gitpod Ready-to-Code Contributions Welcome Discord chat Gitter chat
GitHub Workflow Status pre-commit code style: black

All algorithms implemented in Python - for education

Implementations are for learning purposes only. They may be less efficient than the implementations in the Python standard library. Use them at your discretion.

Getting Started

Read through our Contribution Guidelines before you contribute.

Community Channels

We are on Discord and Gitter! Community channels are a great way for you to ask questions and get help. Please join us!

List of Algorithms

See our directory for easier navigation and a better overview of the project.