Convert Figma logo to code with AI

williamfiset logoAlgorithms

A collection of algorithms and data structures

17,050
4,324
17,050
112

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.

:fireworks:Interactive Online Platform that Visualizes Algorithms from Code

A Python module for learning all major algorithms

Quick Overview

The williamfiset/Algorithms repository is a comprehensive collection of algorithms and data structures implemented in Java. It serves as an educational resource for computer science students and professionals, offering clear implementations and explanations of various algorithmic concepts.

Pros

  • Extensive coverage of algorithms and data structures
  • Well-documented code with explanations and comments
  • Includes unit tests for most implementations
  • Regularly updated and maintained

Cons

  • Primarily focused on Java implementations
  • May lack some advanced or specialized algorithms
  • Some implementations might not be optimized for production use
  • Limited language support for non-Java developers

Code Examples

  1. Depth-First Search (DFS) on a graph:
public void dfs(int at) {
  visited[at] = true;
  for (int next : graph[at]) {
    if (!visited[next]) {
      dfs(next);
    }
  }
}

This code performs a depth-first search traversal on a graph, marking visited nodes.

  1. Binary search on a sorted array:
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;
}

This implementation performs a binary search on a sorted array to find a target value.

  1. Merge sort algorithm:
public static void mergeSort(int[] arr, int left, int right) {
  if (left < right) {
    int mid = (left + right) / 2;
    mergeSort(arr, left, mid);
    mergeSort(arr, mid + 1, right);
    merge(arr, left, mid, right);
  }
}

This code snippet shows the recursive part of the merge sort algorithm, dividing the array and merging sorted subarrays.

Getting Started

To use the algorithms in this repository:

  1. Clone the repository:

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

  3. Compile and run the Java file:

    javac AlgorithmName.java
    java AlgorithmName
    
  4. To use in your own project, copy the relevant Java files and import them as needed.

Competitor Comparisons

183,979

All Algorithms implemented in Python

Pros of Python

  • Larger collection of algorithms and data structures
  • Implementations in Python, which is more beginner-friendly
  • Active community with frequent contributions

Cons of Python

  • Less detailed explanations and comments in the code
  • Some implementations may not be optimized for performance
  • Lacks the comprehensive test coverage found in Algorithms

Code Comparison

Python (TheAlgorithms/Python):

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

Java (Algorithms):

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

Both repositories offer valuable resources for learning algorithms and data structures. Python provides a broader range of implementations in a more accessible language, while Algorithms offers more detailed explanations and robust testing. The choice between them depends on the user's preferred programming language and learning style.

Minimal examples of data structures and algorithms in Python

Pros of algorithms

  • Written in Python, which is more accessible for beginners
  • Includes implementations of machine learning algorithms
  • Provides interactive Jupyter notebooks for learning

Cons of algorithms

  • Less comprehensive coverage of advanced data structures
  • Fewer detailed explanations and comments in the code
  • Not as actively maintained as Algorithms

Code Comparison

algorithms (Python):

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 (Java):

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

Both repositories offer valuable algorithm implementations, but they cater to different audiences. algorithms is more suitable for Python enthusiasts and those interested in machine learning, while Algorithms provides a more comprehensive collection of data structures and algorithms with detailed explanations, making it ideal for in-depth study and Java developers.

📝 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 ideal for web developers
  • Includes explanations and links to further reading for each algorithm
  • Offers a wider variety of data structures and algorithms

Cons of javascript-algorithms

  • Less comprehensive test coverage compared to Algorithms
  • May not be as optimized for performance as the Java implementations in Algorithms
  • Lacks some advanced graph algorithms present in Algorithms

Code Comparison

Algorithms (Java):

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

javascript-algorithms (JavaScript):

function binarySearch(array, item) {
  let start = 0;
  let end = array.length - 1;
  while (start <= end) {
    const middle = Math.floor((start + end) / 2);
    if (array[middle] === item) {
      return middle;
    }
    if (item < array[middle]) {
      end = middle - 1;
    } else {
      start = middle + 1;
    }
  }
  return -1;
}

Both repositories offer high-quality implementations of algorithms and data structures. Algorithms provides a more comprehensive collection with a focus on Java, while javascript-algorithms caters specifically to JavaScript developers with additional explanations and resources.

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

Pros of interactive-coding-challenges

  • Focuses on interactive learning with Jupyter notebooks
  • Covers a wide range of topics including data structures, algorithms, and system design
  • Includes solutions in multiple programming languages (Python, C++, Java)

Cons of interactive-coding-challenges

  • Less comprehensive coverage of advanced algorithms compared to Algorithms
  • May not be as suitable for direct implementation in production code
  • Updates and maintenance are less frequent

Code Comparison

interactive-coding-challenges (Python):

def reverse_string(string):
    if string:
        return string[::-1]
    return string

Algorithms (Java):

public static String reverse(String s) {
  return new StringBuilder(s).reverse().toString();
}

Summary

interactive-coding-challenges offers an interactive learning experience with a broad range of topics and multiple language support. However, it may lack the depth and implementation focus of Algorithms. The code examples show different approaches to string reversal, with interactive-coding-challenges using Python's slicing and Algorithms utilizing Java's StringBuilder.

:fireworks:Interactive Online Platform that Visualizes Algorithms from Code

Pros of Algorithm Visualizer

  • Interactive visualization of algorithms, enhancing understanding
  • Web-based platform accessible from any device with a browser
  • Supports multiple programming languages for algorithm implementation

Cons of Algorithm Visualizer

  • Limited to a specific set of algorithms and data structures
  • May not be as comprehensive or in-depth as Algorithms
  • Focuses more on visualization than code implementation details

Code Comparison

Algorithms (Java):

public static List<Integer> breadthFirstSearch(Map<Integer, List<Integer>> graph, int start) {
    Queue<Integer> queue = new LinkedList<>();
    Set<Integer> visited = new HashSet<>();
    List<Integer> result = new ArrayList<>();
    queue.offer(start);
    visited.add(start);
    // ... (implementation continues)
}

Algorithm Visualizer (JavaScript):

function bfs(graph, start) {
  const queue = [start];
  const visited = new Set([start]);
  const result = [];
  while (queue.length > 0) {
    // ... (implementation continues)
  }
}

Both repositories offer valuable resources for learning algorithms, but they cater to different needs. Algorithms provides a comprehensive collection of algorithm implementations in various languages, focusing on code and explanations. Algorithm Visualizer, on the other hand, offers an interactive platform for visualizing algorithms, making it easier to understand their behavior but with a more limited scope.

A Python module for learning all major algorithms

Pros of pygorithm

  • Focused on Python implementations, making it more accessible for Python developers
  • Includes visualization tools for some algorithms, aiding in understanding
  • Provides a command-line interface for easy access to algorithm information

Cons of pygorithm

  • Smaller collection of algorithms compared to Algorithms
  • Less comprehensive documentation and explanations for each algorithm
  • Not as actively maintained, with fewer recent updates

Code Comparison

pygorithm (Python):

def binary_search(List, target):
    left = 0
    right = len(List) - 1
    while left <= right:
        mid = (left + right) // 2

Algorithms (Java):

public static int binarySearch(int[] ar, int key) {
    int lo = 0, hi = ar.length - 1;
    while (lo <= hi) {
      int mid = lo + ((hi - lo) / 2);

Both implementations show similar approaches to binary search, with pygorithm using Python's more concise syntax and Algorithms using Java's more verbose style. The core logic remains the same in both cases.

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

License: MIT Java CI with Gradle README Checker Donate

Algorithms & data structures project

Algorithms and data structures are fundamental to efficient code and good software design. Creating and designing excellent algorithms is required for being an exemplary programmer. This repository's goal is to demonstrate how to correctly implement common data structures and algorithms in the simplest and most elegant ways.

Contributing

This repository is contribution friendly :smiley:. If you'd like to add or improve an algorithm, your contribution is welcome! Please be sure to check out the Wiki for instructions.

Other programming languages?

This repository provides algorithm implementations in Java, however, there are other forks that provide implementations in other languages, most notably:

Running an algorithm implementation

To compile and run any of the algorithms here, you need at least JDK version 8. Gradle can make things more convenient for you, but it is not required.

Running with Gradle (recommended)

This project supports the Gradle Wrapper. The Gradle wrapper automatically downloads Gradle the first time it runs, so expect a delay when running the first command below.

If you are on Windows, use gradlew.bat instead of ./gradlew below.

Run a single algorithm like this:

./gradlew run -Palgorithm=<algorithm-subpackage>.<algorithm-class>

Alternatively, you can run a single algorithm specifying the full class name

./gradlew run -Pmain=<algorithm-fully-qualified-class-name>

For instance:

./gradlew run -Palgorithm=search.BinarySearch

or

./gradlew run -Pmain=com.williamfiset.algorithms.search.BinarySearch

Compiling and running with only a JDK

Create a classes folder

cd Algorithms
mkdir classes

Compile the algorithm

javac -sourcepath src/main/java -d classes src/main/java/ <relative-path-to-java-source-file>

Run the algorithm

java -cp classes <class-fully-qualified-name>

Example

$ javac -d classes -sourcepath src/main/java src/main/java/com/williamfiset/algorithms/search/BinarySearch.java
$ java -cp classes com.williamfiset.algorithms.search.BinarySearch

Data Structures

Dynamic Programming

Dynamic Programming Classics

Dynamic Programming Problem Examples

Adhoc

Tiling problems

Geometry

Graph theory

Tree algorithms

Network flow

Main graph theory algorithms

Linear algebra

Mathematics

Other

Search algorithms

Sorting algorithms

String algorithms

License

This repository is released under the MIT license. In short, this means you are free to use this software in any personal, open-source or commercial projects. Attribution is optional but appreciated.

Donate

Consider donating to support my creation of educational content:

paypal