Convert Figma logo to code with AI

prakhar1989 logoAlgorithms

:computer: Data Structures and Algorithms in Python

3,040
828
3,040
51

Top Related Projects

183,979

All Algorithms implemented in Python

Minimal examples of data structures and algorithms in Python

📝 Algorithms and data structures implemented in JavaScript with explanations and links to further readings

120+ interactive Python coding interview challenges (algorithms and data structures). Includes Anki flashcards.

A collection of algorithms and data structures

My collection of Python Programs

Quick Overview

The prakhar1989/Algorithms repository is a collection of various algorithms and data structures implemented in Python. It serves as a learning resource and reference for computer science students and programmers interested in understanding and implementing fundamental algorithms.

Pros

  • Comprehensive collection of algorithms and data structures
  • Well-organized and easy to navigate
  • Implementations are in Python, making them accessible to many developers
  • Includes explanations and comments for better understanding

Cons

  • Some implementations may not be optimized for production use
  • Limited language support (primarily Python)
  • Not actively maintained (last commit was several years ago)
  • Lacks unit tests for some algorithms

Code Examples

Here are a few code examples from the repository:

  1. Binary Search implementation:
def binary_search(arr, x):
    low = 0
    high = len(arr) - 1
    mid = 0

    while low <= high:
        mid = (high + low) // 2
        if arr[mid] < x:
            low = mid + 1
        elif arr[mid] > x:
            high = mid - 1
        else:
            return mid
    return -1
  1. Quicksort implementation:
def quicksort(arr):
    if len(arr) <= 1:
        return arr
    pivot = arr[len(arr) // 2]
    left = [x for x in arr if x < pivot]
    middle = [x for x in arr if x == pivot]
    right = [x for x in arr if x > pivot]
    return quicksort(left) + middle + quicksort(right)
  1. Depth-First Search (DFS) implementation:
def dfs(graph, start, visited=None):
    if visited is None:
        visited = set()
    visited.add(start)
    for next in graph[start] - visited:
        dfs(graph, next, visited)
    return visited

Getting Started

To use the algorithms in this repository:

  1. Clone the repository:

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

    cd Algorithms/algorithms
    
  3. Import and use the algorithm in your Python script:

    from sorting.quicksort import quicksort
    
    arr = [3, 6, 8, 10, 1, 2, 1]
    sorted_arr = quicksort(arr)
    print(sorted_arr)
    

Note: Make sure you have Python installed on your system before running the code.

Competitor Comparisons

183,979

All Algorithms implemented in Python

Pros of Python

  • Larger collection of algorithms and data structures
  • More active community with frequent updates and contributions
  • Better organization with categorized folders for different algorithm types

Cons of Python

  • Less focus on algorithm explanations and theory
  • May be overwhelming for beginners due to the large number of implementations
  • Some implementations may lack optimization or best practices

Code Comparison

Algorithms:

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

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
        if array[mid] < target:
            left = mid + 1
        else:
            right = mid - 1
    return -1

Both repositories provide implementations of common algorithms, but Python offers a more extensive collection with better organization. Algorithms focuses more on explanations and may be more suitable for learning purposes, while Python is better for finding a wide range of implementations. The code comparison shows similar approaches to binary search, with minor differences in variable naming and style.

Minimal examples of data structures and algorithms in Python

Pros of algorithms

  • More comprehensive coverage of algorithms and data structures
  • Better organized with clear categorization of topics
  • More active development and community engagement

Cons of algorithms

  • Less detailed explanations for each algorithm implementation
  • Some implementations may be less optimized or efficient

Code Comparison

algorithms:

def binary_search(list, item):
    low = 0
    high = len(list) - 1
    while low <= high:
        mid = (low + high) // 2
        guess = list[mid]
        if guess == item:
            return mid
        if guess > item:
            high = mid - 1
        else:
            low = mid + 1
    return None

Algorithms:

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

Both implementations are similar, with algorithms using more descriptive variable names and returning None instead of -1 for unsuccessful searches.

📝 Algorithms and data structures implemented in JavaScript with explanations and links to further readings

Pros of javascript-algorithms

  • Focuses specifically on JavaScript implementations, making it more accessible for web developers
  • Includes a wider variety of algorithms and data structures
  • Provides detailed explanations and complexity analysis for each implementation

Cons of javascript-algorithms

  • Less language diversity compared to Algorithms, which covers multiple programming languages
  • May not be as suitable for users looking to learn algorithms in languages other than JavaScript
  • Larger repository size, potentially making it more overwhelming for beginners

Code Comparison

Algorithms (Python):

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

javascript-algorithms (JavaScript):

function binarySearch(sortedArray, seekElement) {
  let startIndex = 0;
  let endIndex = sortedArray.length - 1;
  while (startIndex <= endIndex) {
    const middleIndex = startIndex + Math.floor((endIndex - startIndex) / 2);
    if (sortedArray[middleIndex] === seekElement) {
      return middleIndex;
    }
    if (sortedArray[middleIndex] < seekElement) {
      startIndex = middleIndex + 1;
    } else {
      endIndex = middleIndex - 1;
    }
  }
  return -1;
}

Both repositories offer valuable resources for learning algorithms, with javascript-algorithms being more focused on JavaScript implementations and providing more detailed explanations, while Algorithms offers a broader language coverage.

120+ interactive Python coding interview challenges (algorithms and data structures). Includes Anki flashcards.

Pros of interactive-coding-challenges

  • More comprehensive coverage of data structures and algorithms
  • Includes interactive Jupyter notebooks for hands-on learning
  • Provides test cases and solutions for each challenge

Cons of interactive-coding-challenges

  • Larger repository size, potentially overwhelming for beginners
  • Less focused on pure algorithmic implementations
  • May require additional setup for Jupyter notebooks

Code Comparison

Algorithms (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:
            return mid
        elif arr[mid] < x:
            low = mid + 1
        else:
            high = mid - 1
    return -1

interactive-coding-challenges (Binary Search implementation):

class BinarySearch(object):
    def search(self, nums, target):
        left, right = 0, len(nums) - 1
        while left <= right:
            mid = (left + right) // 2
            if nums[mid] == target:
                return mid
            elif nums[mid] < target:
                left = mid + 1
            else:
                right = mid - 1
        return -1

Both repositories offer valuable resources for learning algorithms and data structures. Algorithms focuses on concise implementations, while interactive-coding-challenges provides a more interactive and comprehensive learning experience with additional features like test cases and Jupyter notebooks.

A collection of algorithms and data structures

Pros of Algorithms (williamfiset)

  • More comprehensive coverage of algorithms and data structures
  • Includes implementations in multiple programming languages (Java, C++, Python)
  • Actively maintained with regular updates and contributions

Cons of Algorithms (williamfiset)

  • Less focus on explanations and theory behind algorithms
  • May be overwhelming for beginners due to the large number of implementations

Code Comparison

Algorithms (prakhar1989):

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

Algorithms (williamfiset):

public static int binarySearch(int[] arr, int target) {
    int left = 0, right = arr.length - 1;
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (arr[mid] == target) return mid;
        if (arr[mid] < target) left = mid + 1;
        else right = mid - 1;
    }
    return -1;
}

Both repositories offer valuable resources for learning algorithms and data structures. Algorithms (prakhar1989) is more suitable for beginners, with clearer explanations and a focus on Python implementations. Algorithms (williamfiset) provides a wider range of algorithms and implementations in multiple languages, making it a better choice for more advanced users or those looking for a comprehensive reference.

My collection of Python Programs

Pros of Python-Programs

  • Focuses specifically on Python implementations, making it more accessible for Python learners
  • Includes a wider variety of programming concepts beyond just algorithms (e.g., data structures, design patterns)
  • Provides more detailed explanations and comments within the code

Cons of Python-Programs

  • Less comprehensive coverage of algorithms compared to Algorithms
  • May not be as suitable for users looking to learn implementations in other languages
  • Repository structure is less organized, making it harder to navigate

Code Comparison

Python-Programs (Binary Search):

def binary_search(arr, l, r, x):
    if r >= l:
        mid = l + (r - l) // 2
        if arr[mid] == x:
            return mid
        elif arr[mid] > x:
            return binary_search(arr, l, mid-1, x)
        else:
            return binary_search(arr, mid+1, r, x)
    else:
        return -1

Algorithms (Binary Search in C++):

int binarySearch(int arr[], int l, int r, int x) {
    if (r >= l) {
        int mid = l + (r - l) / 2;
        if (arr[mid] == x)
            return mid;
        if (arr[mid] > x)
            return binarySearch(arr, l, mid - 1, x);
        return binarySearch(arr, mid + 1, r, x);
    }
    return -1;
}

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

Algorithms in Python

Implementations of a few algorithms and datastructures for fun and profit!

Completed

  • Karatsuba Multiplication
  • Basic Sorting
  • Rabin-Miller primality test
  • Sieve of Eratosthenes for prime numbers
  • Binary Search
  • Counting Inversions in an array
  • Selecting ith order statistic in an array
  • Graph datastructure (directed & undirected)
  • Graph Algos
    • Topological Sorting
    • Shortest hops
    • DFS
    • BFS
    • Connected Components
    • Dijkstra's Shortest Path - O(mlogn)
    • Prim's Minimum Cost Spanning Tree - O(mlogn)
    • Kruskal's Minimum Spanning Tree - O(mlogn)
    • Max k Clustering
    • Bellman Ford
    • Floyd Warshall
    • Johnson's Algorithm
  • Heap datastructure
    • Max heaps
    • Min heaps (priority queue)
    • Heapsort
  • Job Scheduling
  • UnionFind Data Structure
  • Binary Search Tree
  • Kandane's Algorithm
  • Knapsack Problem (0/1 and unbounded)
  • Longest Increasing Subsequence
  • Longest Common Subsequence
  • Prefix Tries
  • Stack ADT (with example problems)
    • String Reverse
    • Parenthesis Matching
    • Infix to Postfix
  • Modular exponentiation
  • Modular multiplicative inverse

Tests

python -m tests.graph_test
python -m tests.digraph_test
python -m tests.graph_algorithms_test
python -m tests.heap_test
python -m tests.unionfind_test
python -m tests.singly_linked_list_test
python -m tests.modular_exponentiation_test
python -m tests.modular_multiplicative_inverse_test