Algorithms
Common algorithms implemented in pure Python.
Sorting
heap_sort(arr)
Perform an O(n log n) Heap Sort on a list. Returns a new sorted list.
Source code in pure_python_ds/algorithms/sorting.py
77 78 79 80 81 82 83 84 | |
insertion_sort(arr)
Perform an O(n^2) Insertion Sort on a list. Efficient for small or nearly sorted arrays. Returns a new sorted list.
Source code in pure_python_ds/algorithms/sorting.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
merge_sort(arr)
Perform an O(n log n) Merge Sort on a list. Returns a new sorted list.
Source code in pure_python_ds/algorithms/sorting.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |
quick_sort(arr)
Perform an O(n log n) expected Quick Sort on a list. Returns a new sorted list.
Source code in pure_python_ds/algorithms/sorting.py
46 47 48 49 50 51 52 53 54 55 56 57 | |
radix_sort(arr)
Perform an O(nk) Radix Sort on a list of non-negative integers. Returns a new sorted list.
Source code in pure_python_ds/algorithms/sorting.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | |
Searching
binary_search(arr, target)
Performs an iterative binary search on a sorted list. Returns the index of the target if found, else -1.
Source code in pure_python_ds/algorithms/searching.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |
exponential_search(arr, target)
Performs an O(log i) exponential search on a sorted list. Returns the index of the target if found, else -1.
Source code in pure_python_ds/algorithms/searching.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | |
jump_search(arr, target)
Performs an O(sqrt(n)) jump search on a sorted list. Returns the index of the target if found, else -1.
Source code in pure_python_ds/algorithms/searching.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
linear_search(arr, target)
Performs an O(n) linear search on a list. Returns the index of the target if found, else -1.
Source code in pure_python_ds/algorithms/searching.py
27 28 29 30 31 32 33 34 35 | |
Dynamic Programming
edit_distance(word1, word2)
Computes the Levenshtein minimum edit distance between two strings.
Source code in pure_python_ds/algorithms/dp.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
fibonacci(n, memo=None)
Computes the nth Fibonacci number using Top-Down DP (Memoization). O(n) time complexity vs O(2^n) recursive complexity.
Source code in pure_python_ds/algorithms/dp.py
4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
knapsack(weights, values, capacity)
Solves the 0-1 Knapsack problem. Returns the maximum value that can be put in a knapsack of capacity W.
Source code in pure_python_ds/algorithms/dp.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
longest_common_subsequence(text1, text2)
Finds the length of the longest common subsequence of two strings.
Source code in pure_python_ds/algorithms/dp.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
longest_increasing_subsequence(arr)
Finds the length of the longest increasing subsequence.
Source code in pure_python_ds/algorithms/dp.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
Strings
kmp_search(text, pattern)
Knuth-Morris-Pratt (KMP) Algorithm. Returns all starting indices of the pattern in the text.
Source code in pure_python_ds/algorithms/strings.py
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
rabin_karp(text, pattern)
Rabin-Karp Algorithm for string matching. Returns all starting indices of the pattern in the text. Uses rolling hash to verify match candidates in expected O(N+M) time.
Source code in pure_python_ds/algorithms/strings.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |