Convert Figma logo to code with AI

TheAlgorithms logoJava

All Algorithms implemented in Java

58,536
18,948
58,536
14

Top Related Projects

《Hello 算法》:动画图解、一键运行的数据结构与算法教程。支持 Python, Java, C++, C, C#, JS, Go, Swift, Rust, Ruby, Kotlin, TS, Dart 代码。简体版和繁体版同步更新,English version ongoing

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

🌍 针对小白的算法训练 | 包括四部分:①.大厂面经 ②.力扣图解 ③.千本开源电子书 ④.百张技术思维导图(项目花了上百小时,希望可以点 star 支持,🌹感谢~)推荐免费ChatGPT使用网站

174,630

:books: 技术面试必备基础知识、Leetcode、计算机操作系统、计算机网络、系统设计

Demonstrate all the questions on LeetCode in the form of animation.(用动画的形式呈现解LeetCode题目的思路)

54,397

LeetCode Solutions: A Record of My Problem Solving Journey.( leetcode题解,记录自己的leetcode解题之路。)

Quick Overview

TheAlgorithms/Java is a comprehensive collection of various algorithms and data structures implemented in Java. It serves as an educational resource for learning about algorithms and their implementations, as well as a reference for developers looking to understand or use specific algorithms in their projects.

Pros

  • Extensive collection of algorithms covering various categories (sorting, searching, cryptography, etc.)
  • Well-organized directory structure for easy navigation
  • Implementations are generally clear and well-commented
  • Active community with regular contributions and updates

Cons

  • Some implementations may not be optimized for production use
  • Lack of comprehensive documentation for each algorithm
  • Inconsistent coding style across different contributions
  • Some algorithms may be missing or incomplete

Code Examples

  1. Binary Search implementation:
public class BinarySearch implements SearchAlgorithm {
    @Override
    public <T extends Comparable<T>> int find(T[] array, T key) {
        return search(array, key, 0, array.length - 1);
    }

    private <T extends Comparable<T>> int search(T[] array, T key, int left, int right) {
        if (right < left) return -1; // this means that the key not found

        // find median
        int median = (left + right) >>> 1;
        int comp = key.compareTo(array[median]);

        if (comp == 0) {
            return median;
        } else if (comp < 0) {
            return search(array, key, left, median - 1);
        } else {
            return search(array, key, median + 1, right);
        }
    }
}
  1. Bubble Sort implementation:
public class BubbleSort implements SortAlgorithm {
    @Override
    public <T extends Comparable<T>> T[] sort(T[] array) {
        for (int i = 0, size = array.length; i < size - 1; ++i) {
            boolean swapped = false;
            for (int j = 0; j < size - 1 - i; ++j) {
                if (SortUtils.less(array[j + 1], array[j])) {
                    SortUtils.swap(array, j, j + 1);
                    swapped = true;
                }
            }
            if (!swapped) {
                break;
            }
        }
        return array;
    }
}
  1. Fibonacci sequence calculation:
public class FibonacciRecursion {
    public static long fibonacci(int n) {
        if (n <= 1) {
            return n;
        }
        return fibonacci(n - 1) + fibonacci(n - 2);
    }
}

Getting Started

To use algorithms from this repository:

  1. Clone the repository:

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

  3. Copy the Java file containing the algorithm implementation into your project.

  4. Import the algorithm class in your code and use it:

    import com.thealgorithms.searches.BinarySearch;
    
    public class Main {
        public static void main(String[] args) {
            Integer[] array = {1, 2, 3, 4, 5};
            BinarySearch binarySearch = new BinarySearch();
            int result = binarySearch.find(array, 3);
            System.out.println("Element found at index: " + result);
        }
    }
    

Note: Make sure to adjust the import statement according to the package structure in your project.

Competitor Comparisons

《Hello 算法》:动画图解、一键运行的数据结构与算法教程。支持 Python, Java, C++, C, C#, JS, Go, Swift, Rust, Ruby, Kotlin, TS, Dart 代码。简体版和繁体版同步更新,English version ongoing

Pros of hello-algo

  • Focuses on algorithm visualization and interactive learning
  • Provides implementations in multiple programming languages
  • Includes detailed explanations and illustrations for each algorithm

Cons of hello-algo

  • Smaller scope, covering fewer algorithms than Java
  • Less comprehensive in terms of algorithm variations and optimizations
  • Primarily aimed at beginners, may lack advanced topics

Code Comparison

hello-algo (Python implementation):

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

Java (Java implementation):

public static void bubbleSort(int[] arr) {
    for (int i = 0; i < arr.length - 1; i++)
        for (int j = 0; j < arr.length - 1 - i; j++)
            if (arr[j] > arr[j + 1]) {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
}

Both repositories offer valuable resources for learning algorithms, but they cater to different audiences and have distinct focuses. hello-algo emphasizes visual learning and provides implementations in multiple languages, making it more accessible for beginners. Java, on the other hand, offers a more comprehensive collection of algorithms and data structures specifically for Java developers, with a broader range of topics and optimizations.

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

Pros of javascript-algorithms

  • Written in JavaScript, a popular language for web development
  • Includes explanations and links to further reading for each algorithm
  • Organized by algorithm type (e.g., math, string, search) for easy navigation

Cons of javascript-algorithms

  • Fewer algorithms implemented compared to Java
  • Less comprehensive test coverage
  • May not be as optimized for performance as Java implementations

Code Comparison

Java implementation of binary search:

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;
}

JavaScript implementation of binary search:

function binarySearch(arr, target) {
  let left = 0, right = arr.length - 1;
  while (left <= right) {
    const mid = Math.floor((left + right) / 2);
    if (arr[mid] === target) return mid;
    if (arr[mid] < target) left = mid + 1;
    else right = mid - 1;
  }
  return -1;
}

Both implementations are similar in structure and logic, with minor syntax differences due to the languages used.

🌍 针对小白的算法训练 | 包括四部分:①.大厂面经 ②.力扣图解 ③.千本开源电子书 ④.百张技术思维导图(项目花了上百小时,希望可以点 star 支持,🌹感谢~)推荐免费ChatGPT使用网站

Pros of hello-algorithm

  • Offers a more comprehensive learning experience with explanations, visualizations, and multiple programming languages
  • Includes interview preparation materials and problem-solving strategies
  • Provides a structured learning path for algorithms and data structures

Cons of hello-algorithm

  • Less focused on Java-specific implementations compared to Java
  • May have fewer contributors and less frequent updates
  • Repository structure might be more complex due to multiple languages and resources

Code Comparison

hello-algorithm (Python implementation):

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

Java (Java implementation):

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, with hello-algorithm providing a broader scope and Java focusing specifically on Java implementations. The choice between them depends on the user's learning goals and preferred programming language.

174,630

:books: 技术面试必备基础知识、Leetcode、计算机操作系统、计算机网络、系统设计

Pros of CS-Notes

  • Comprehensive coverage of computer science topics beyond algorithms
  • Includes study notes and interview preparation materials
  • Written in Chinese, catering to a specific audience

Cons of CS-Notes

  • Less focused on practical implementation of algorithms
  • May not be as accessible to non-Chinese speakers
  • Updates less frequently compared to Java

Code Comparison

CS-Notes (example of a design pattern):

public class Singleton {
    private static Singleton instance;
    private Singleton() {}
    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}

Java (example of a sorting algorithm):

public static void bubbleSort(int[] arr) {
    for (int i = 0; i < arr.length - 1; i++)
        for (int j = 0; j < arr.length - i - 1; j++)
            if (arr[j] > arr[j + 1]) {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
}

The CS-Notes repository provides a broader range of computer science topics, including design patterns, system design, and interview preparation. It's particularly useful for Chinese-speaking developers. However, Java focuses more on practical algorithm implementations in Java, making it a better resource for those specifically looking to study and practice coding algorithms.

Demonstrate all the questions on LeetCode in the form of animation.(用动画的形式呈现解LeetCode题目的思路)

Pros of LeetCodeAnimation

  • Provides visual animations for algorithm explanations, making concepts easier to understand
  • Focuses specifically on LeetCode problems, which is beneficial for interview preparation
  • Includes solutions in multiple programming languages

Cons of LeetCodeAnimation

  • Limited scope, covering only LeetCode problems rather than a broad range of algorithms
  • Less comprehensive documentation compared to TheAlgorithms/Java
  • Fewer contributors and less frequent updates

Code Comparison

LeetCodeAnimation (Python):

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        hashmap = {}
        for i, num in enumerate(nums):
            complement = target - num
            if complement in hashmap:
                return [hashmap[complement], i]
            hashmap[num] = i

TheAlgorithms/Java:

public class TwoSum {
    public static int[] findTwoSum(int[] nums, int target) {
        Map<Integer, Integer> numMap = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int complement = target - nums[i];
            if (numMap.containsKey(complement)) {
                return new int[] { numMap.get(complement), i };
            }
            numMap.put(nums[i], i);
        }
        return new int[] {};
    }
}

Both repositories provide solutions to the Two Sum problem, but LeetCodeAnimation offers additional visual explanations, while TheAlgorithms/Java provides a more comprehensive collection of algorithms beyond just LeetCode problems.

54,397

LeetCode Solutions: A Record of My Problem Solving Journey.( leetcode题解,记录自己的leetcode解题之路。)

Pros of leetcode

  • Focuses specifically on LeetCode problems, providing targeted solutions for coding interviews
  • Includes solutions in multiple programming languages (JavaScript, Python, C++, etc.)
  • Offers detailed explanations and analysis for each problem solution

Cons of leetcode

  • Less comprehensive in terms of general algorithms and data structures
  • May not cover as wide a range of computer science topics as Java
  • Solutions might be more focused on passing LeetCode tests rather than real-world applications

Code Comparison

Java (TheAlgorithms/Java):

public static void bubbleSort(int[] array) {
    for (int i = 0; i < array.length - 1; i++) {
        for (int j = 0; j < array.length - i - 1; j++) {
            if (array[j] > array[j + 1]) {
                int temp = array[j];
                array[j] = array[j + 1];
                array[j + 1] = temp;
            }
        }
    }
}

leetcode (JavaScript solution):

var twoSum = function(nums, target) {
    const map = new Map();
    for (let i = 0; i < nums.length; i++) {
        const complement = target - nums[i];
        if (map.has(complement)) {
            return [map.get(complement), i];
        }
        map.set(nums[i], i);
    }
};

The Java example shows a general-purpose sorting algorithm, while the leetcode example demonstrates a specific problem solution optimized for efficiency.

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 - Java

Build codecov Discord chat Gitpod ready-to-code

You can run and edit the algorithms, or contribute to them using Gitpod.io (a free online development environment) with a single click.

Open in Gitpod

All algorithms are implemented in Java (for educational purposes)

These implementations are intended for learning purposes. As such, they may be less efficient than the Java standard library.

Contribution Guidelines

Please read our Contribution Guidelines before you contribute to this project.

Algorithms

Our directory has the full list of applications.