Top Related Projects
All Algorithms implemented in Python
Learn how to design large-scale systems. Prep for the system design interview. Includes Anki flashcards.
📝 Algorithms and data structures implemented in JavaScript with explanations and links to further readings
Demonstrate all the questions on LeetCode in the form of animation.(用动画的形式呈现解LeetCode题目的思路)
LeetCode Solutions: A Record of My Problem Solving Journey.( leetcode题解,记录自己的leetcode解题之路。)
🌍 针对小白的算法训练 | 包括四部分:①.大厂面经 ②.力扣图解 ③.千本开源电子书 ④.百张技术思维导图(项目花了上百小时,希望可以点 star 支持,🌹感谢~)推荐免费ChatGPT使用网站
Quick Overview
The keon/algorithms repository is a comprehensive collection of algorithms and data structures implemented in Python. It serves as an educational resource for computer science students, developers, and anyone interested in learning about various algorithmic concepts and their implementations.
Pros
- Extensive coverage of algorithms and data structures
- Well-organized and categorized implementations
- Clear and readable Python code
- Includes unit tests for most implementations
Cons
- Some implementations may not be optimized for performance
- Limited documentation for some algorithms
- Not actively maintained (last commit over a year ago)
- Lacks advanced or specialized algorithms in certain areas
Code Examples
- 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
This code implements the binary search algorithm, which efficiently finds a target value in a sorted array.
- Bubble Sort implementation:
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
This code implements the bubble sort algorithm, which sorts an array by repeatedly swapping adjacent elements if they are in the wrong order.
- Depth-First Search (DFS) implementation:
def dfs(graph, start, visited=None):
if visited is None:
visited = set()
visited.add(start)
print(start)
for next in graph[start] - visited:
dfs(graph, next, visited)
return visited
This code implements the depth-first search algorithm for traversing or searching tree or graph data structures.
Getting Started
To use the algorithms in this repository:
-
Clone the repository:
git clone https://github.com/keon/algorithms.git
-
Navigate to the desired algorithm's directory:
cd algorithms/algorithms/sort
-
Import and use the algorithm in your Python code:
from bubble_sort import bubble_sort arr = [64, 34, 25, 12, 22, 11, 90] sorted_arr = bubble_sort(arr) print(sorted_arr)
This will import and use the bubble sort algorithm from the repository.
Competitor Comparisons
All Algorithms implemented in Python
Pros of TheAlgorithms/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 TheAlgorithms/Python
- Less focus on optimization and efficiency in some implementations
- May be overwhelming for beginners due to the vast number of algorithms
Code Comparison
algorithms implementation of binary search:
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
TheAlgorithms/Python implementation of binary search:
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 in approach, with minor differences in variable naming and return values for unsuccessful searches.
Learn how to design large-scale systems. Prep for the system design interview. Includes Anki flashcards.
Pros of system-design-primer
- Comprehensive coverage of system design concepts and best practices
- Includes real-world examples and case studies from large-scale systems
- Provides interactive learning resources like quizzes and exercises
Cons of system-design-primer
- Focuses primarily on high-level system design, less on implementation details
- May be overwhelming for beginners due to its extensive content
- Requires more time investment to fully grasp all concepts
Code comparison
system-design-primer doesn't typically include code snippets, as it focuses on high-level design concepts. In contrast, algorithms provides implementation examples:
# Example from algorithms
def bubble_sort(collection):
length = len(collection)
for i in range(length - 1):
for j in range(length - 1 - i):
if collection[j] > collection[j + 1]:
collection[j], collection[j + 1] = collection[j + 1], collection[j]
return collection
algorithms offers practical implementations of various algorithms and data structures, making it more suitable for those looking to improve their coding skills and understand algorithm implementation details. system-design-primer, on the other hand, is better suited for developers preparing for system design interviews or looking to understand large-scale system architecture concepts.
📝 Algorithms and data structures implemented in JavaScript with explanations and links to further readings
Pros of javascript-algorithms
- Focused specifically on JavaScript implementations
- More comprehensive coverage of algorithms and data structures
- Includes explanations and complexity analysis for each algorithm
Cons of javascript-algorithms
- Larger repository size, potentially overwhelming for beginners
- Less language diversity compared to 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
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 and data structures. algorithms provides implementations in multiple languages, making it versatile for different programming backgrounds. javascript-algorithms offers a more in-depth focus on JavaScript, with detailed explanations and analysis. The choice between them depends on the user's specific language needs and depth of study desired.
Demonstrate all the questions on LeetCode in the form of animation.(用动画的形式呈现解LeetCode题目的思路)
Pros of LeetCodeAnimation
- Provides animated visualizations of algorithms, making them easier to understand
- Focuses specifically on LeetCode problems, which is beneficial for interview preparation
- Includes explanations in Chinese, catering to a wider audience
Cons of LeetCodeAnimation
- Limited to a smaller set of algorithms compared to algorithms
- Animations may not be as helpful for experienced programmers who prefer text-based explanations
- Less comprehensive coverage of general algorithmic concepts
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
LeetCodeAnimation:
public int search(int[] nums, int target) {
int left = 0, right = nums.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] == target) return mid;
if (target < nums[mid]) right = mid - 1;
Both repositories provide implementations of binary search, but algorithms uses Python while LeetCodeAnimation uses Java. The algorithms implementation is more concise, while LeetCodeAnimation's version includes additional logic for handling different cases.
LeetCode Solutions: A Record of My Problem Solving Journey.( leetcode题解,记录自己的leetcode解题之路。)
Pros of leetcode
- More comprehensive coverage of LeetCode problems
- Solutions in multiple programming languages (JavaScript, Python, Java, etc.)
- Detailed explanations and analysis for each problem
Cons of leetcode
- Less focus on general algorithms and data structures
- May be overwhelming for beginners due to the large number of problems
Code Comparison
leetcode (JavaScript):
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);
}
};
algorithms (Python):
def two_sum(nums, target):
dic = {}
for i, num in enumerate(nums):
if num in dic:
return [dic[num], i]
else:
dic[target - num] = i
Both repositories provide solutions to the "Two Sum" problem, but leetcode offers implementations in multiple languages, while algorithms focuses on Python. The leetcode solution includes more detailed comments and explanations in the full repository.
🌍 针对小白的算法训练 | 包括四部分:①.大厂面经 ②.力扣图解 ③.千本开源电子书 ④.百张技术思维导图(项目 花了上百小时,希望可以点 star 支持,🌹感谢~)推荐免费ChatGPT使用网站
Pros of hello-algorithm
- Extensive collection of algorithms and data structures in multiple programming languages
- Includes visual explanations and diagrams for better understanding
- Offers additional resources like interview preparation materials
Cons of hello-algorithm
- Less organized structure compared to algorithms
- May be overwhelming for beginners due to the large amount of content
- Some explanations are in Chinese, which may be a barrier for non-Chinese speakers
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
hello-algorithm (Java):
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 provide implementations of common algorithms, but hello-algorithm offers a wider range of languages and more comprehensive explanations. However, algorithms has a more structured and beginner-friendly approach, making it easier to navigate and learn from.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Pythonic Data Structures and Algorithms
Minimal and clean example implementations of data structures and algorithms in Python 3.
Contributing
Thanks for your interest in contributing! There are many ways to contribute to this project. Get started here
Tests
Use unittest
For running all tests write down:
$ python3 -m unittest discover tests
For running some specific tests you can do this as following (Ex: sort):
$ python3 -m unittest tests.test_sort
Use pytest
For running all tests write down:
$ python3 -m pytest tests
Install
If you want to use the API algorithms in your code, it is as simple as:
$ pip3 install algorithms
You can test by creating a python file: (Ex: use merge_sort
in sort
)
from algorithms.sort import merge_sort
if __name__ == "__main__":
my_list = [1, 8, 3, 5, 6]
my_list = merge_sort(my_list)
print(my_list)
Uninstall
If you want to uninstall algorithms, it is as simple as:
$ pip3 uninstall -y algorithms
List of Implementations
- arrays
- greedy
- automata
- backtrack
- bfs
- bit
- compression
- dfs
- distribution
- dp
- buy_sell_stock
- climbing_stairs
- coin_change
- combination_sum
- egg_drop
- house_robber
- int_divide
- job_scheduling
- knapsack
- longest_increasing
- matrix_chain_order
- max_product_subarray
- max_subarray
- min_cost_path
- num_decodings
- regex_matching
- rod_cut
- word_break
- fibonacci
- hosoya triangle
- K-Factor_strings
- planting_trees
- graph
- heap
- linkedlist
- map
- maths
- base_conversion
- chinese_remainder_theorem
- combination
- cosine_similarity
- decimal_to_binary_ip
- diffie_hellman_key_exchange
- euler_totient
- extended_gcd
- factorial
- find_order
- find_primitive_root
- gcd/lcm
- generate_strobogrammtic
- hailstone
- is_strobogrammatic
- krishnamurthy_number
- magic_number
- modular_exponential
- modular_inverse
- next_bigger
- next_perfect_square
- nth_digit
- num_perfect_squares
- polynomial
- power
- prime_check
- primes_sieve_of_eratosthenes
- pythagoras
- rabin_miller
- recursive_binomial_coefficient
- rsa
- sqrt_precision_factor
- summing_digits
- symmetry_group_cycle_index
- matrix
- queues
- search
- set
- sort
- stack
- streaming
- strings
- fizzbuzz
- delete_reoccurring
- strip_url_params
- validate_coordinates
- domain_extractor
- merge_string_checker
- add_binary
- breaking_bad
- decode_string
- encode_decode
- group_anagrams
- int_to_roman
- is_palindrome
- license_number
- make_sentence
- multiply_strings
- one_edit_distance
- rabin_karp
- reverse_string
- reverse_vowel
- reverse_words
- roman_to_int
- word_squares
- unique_morse
- judge_circle
- strong_password
- caesar_cipher
- check_pangram
- contain_string
- count_binary_substring
- repeat_string
- min_distance
- longest_common_prefix
- rotate
- first_unique_char
- repeat_substring
- longest_palindromic_substring
- knuth_morris_pratt
- panagram
- tree
- bst
- fenwick_tree
- red_black_tree
- segment_tree
- traversal
- trie
- b_tree
- binary_tree_paths
- bin_tree_to_list
- construct_tree_preorder_postorder
- deepest_left
- invert_tree
- is_balanced
- is_subtree
- is_symmetric
- longest_consecutive
- lowest_common_ancestor
- max_height
- max_path_sum
- min_height
- path_sum
- path_sum2
- pretty_print
- same_tree
- tree
- unix
- unionfind
Contributors
Thanks to all the contributors who helped in building the repo.
Top Related Projects
All Algorithms implemented in Python
Learn how to design large-scale systems. Prep for the system design interview. Includes Anki flashcards.
📝 Algorithms and data structures implemented in JavaScript with explanations and links to further readings
Demonstrate all the questions on LeetCode in the form of animation.(用动画的形式呈现解LeetCode题目的思路)
LeetCode Solutions: A Record of My Problem Solving Journey.( leetcode题解,记录自己的leetcode解题之路。)
🌍 针对小白的算法训练 | 包括四部分:①.大厂面经 ②.力扣图解 ③.千本开源电子书 ④.百张技术思维导图(项目花了上百小时,希望可以点 star 支持,🌹感谢~)推荐免费ChatGPT使用网站
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot