MCPcopy Index your code
hub / github.com/subbarayudu-j/TheAlgorithms-Python

github.com/subbarayudu-j/TheAlgorithms-Python @main sqlite

repository ↗ · DeepWiki ↗
936 symbols 2,317 edges 272 files 196 documented · 21%
README

The Algorithms - Python

All algorithms implemented in Python (for education)

These implementations are for demonstration purposes. They are less efficient than the implementations in the Python standard library.

Sorting Algorithms

Bubble Sort

![alt text][bubble-image]

Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.

Properties * Worst case performance O(n2) * Best case performance O(n) * Average case performance O(n2)

Source: [Wikipedia][bubble-wiki]
View the algorithm in [action][bubble-toptal]

Bucket

![alt text][bucket-image-1] ![alt text][bucket-image-2]

Bucket sort, or bin sort, is a sorting algorithm that works by distributing the elements of an array into a number of buckets. Each bucket is then sorted individually, either using a different sorting algorithm, or by recursively applying the bucket sorting algorithm.

Properties * Worst case performance O(n2) * Best case performance O(n+k) * Average case performance O(n+k)

Source: [Wikipedia][bucket-wiki]

Cocktail shaker

![alt text][cocktail-shaker-image]

Cocktail shaker sort, also known as bidirectional bubble sort, cocktail sort, shaker sort (which can also refer to a variant of selection sort), ripple sort, shuffle sort, or shuttle sort, is a variation of bubble sort that is both a stable sorting algorithm and a comparison sort. The algorithm differs from a bubble sort in that it sorts in both directions on each pass through the list.

Properties * Worst case performance O(n2) * Best case performance O(n) * Average case performance O(n2)

Source: [Wikipedia][cocktail-shaker-wiki]

Insertion Sort

![alt text][insertion-image]

Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort.

Properties * Worst case performance O(n2) * Best case performance O(n) * Average case performance O(n2)

Source: [Wikipedia][insertion-wiki]
View the algorithm in [action][insertion-toptal]

Merge Sort

![alt text][merge-image]

Merge sort (also commonly spelled mergesort) is an efficient, general-purpose, comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the implementation preserves the input order of equal elements in the sorted output. Mergesort is a divide and conquer algorithm that was invented by John von Neumann in 1945.

Properties * Worst case performance O(n log n) * Best case performance O(n log n) * Average case performance O(n log n)

Source: [Wikipedia][merge-wiki]
View the algorithm in [action][merge-toptal]

Quick

![alt text][quick-image]

Quicksort (sometimes called partition-exchange sort) is an efficient sorting algorithm, serving as a systematic method for placing the elements of an array in order.

Properties * Worst case performance O(n2) * Best case performance O(n log n) or O(n) with three-way partition * Average case performance O(n log n)

Source: [Wikipedia][quick-wiki]
View the algorithm in [action][quick-toptal]

Heap

Heapsort is a comparison-based sorting algorithm. It can be thought of as an improved selection sort. It divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region.

Properties * Worst case performance O(n log n) * Best case performance O(n log n) * Average case performance O(n log n)

Source: [Wikipedia][heap-wiki]
View the algorithm in action

Radix

From [Wikipedia][radix-wiki]: Radix sort is a non-comparative integer sorting algorithm that sorts data with integer keys by grouping keys by the individual digits which share the same significant position and value.

Properties * Worst case performance O(wn) * Best case performance O(wn) * Average case performance O(wn)

Source: [Wikipedia][radix-wiki]

Selection

![alt text][selection-image]

Selection sort is an algorithm that divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.

Properties * Worst case performance O(n2) * Best case performance O(n2) * Average case performance O(n2)

Source: [Wikipedia][selection-wiki]
View the algorithm in [action][selection-toptal]

Shell

![alt text][shell-image]

Shellsort is a generalization of insertion sort that allows the exchange of items that are far apart. The idea is to arrange the list of elements so that, starting anywhere, considering every nth element gives a sorted list. Such a list is said to be h-sorted. Equivalently, it can be thought of as h interleaved lists, each individually sorted.

Properties * Worst case performance O(nlog2n) * Best case performance O(n log n) * Average case performance depends on gap sequence

Source: [Wikipedia][shell-wiki]
View the algorithm in [action][shell-toptal]

Topological

From [Wikipedia][topological-wiki]: Topological sort, or topological ordering of a directed graph is a linear ordering of its vertices such that for every directed edge uv from vertex u to vertex v, u comes before v in the ordering. For instance, the vertices of the graph may represent tasks to be performed, and the edges may represent constraints that one task must be performed before another; in this application, a topological ordering is just a valid sequence for the tasks. A topological ordering is possible if and only if the graph has no directed cycles, that is, if it is a directed acyclic graph (DAG). Any DAG has at least one topological ordering, and algorithms are known for constructing a topological ordering of any DAG in linear time.

Time-Complexity Graphs

Comparing the complexity of sorting algorithms (Bubble Sort, Insertion Sort, Selection Sort)

Complexity Graphs

Comparing the sorting algorithms:

-Quicksort is a very fast algorithm but can be pretty tricky to implement

-Bubble sort is a slow algorithm but is very easy to implement. To sort small sets of data, bubble sort may be a better option since it can be implemented quickly, but for larger datasets, the speedup from quicksort might be worth the trouble implementing the algorithm.


Search Algorithms

Linear

![alt text][linear-image]

Linear search or sequential search is a method for finding a target value within a list. It sequentially checks each element of the list for the target value until a match is found or until all the elements have been searched. Linear search runs in at worst linear time and makes at most n comparisons, where n is the length of the list.

Properties * Worst case performance O(n) * Best case performance O(1) * Average case performance O(n) * Worst case space complexity O(1) iterative

Source: [Wikipedia][linear-wiki]

Binary

![alt text][binary-image]

Binary search, also known as half-interval search or logarithmic search, is a search algorithm that finds the position of a target value within a sorted array. It compares the target value to the middle element of the array; if they are unequal, the half in which the target cannot lie is eliminated and the search continues on the remaining half until it is successful.

Properties * Worst case performance O(log n) * Best case performance O(1) * Average case performance O(log n) * Worst case space complexity O(1)

Source: [Wikipedia][binary-wiki]

Interpolation

Interpolation search is an algorithm for searching for a key in an array that has been ordered by numerical values assigned to the keys (key values). It was first described by W. W. Peterson in 1957. Interpolation search resembles the method by which people search a telephone directory for a name (the key value by which the book's entries are ordered): in each step the algorithm calculates where in the remaining search space the sought item might be, based on the key values at the bounds of the search space and the value of the sought key, usually via a linear interpolation. The key value actually found at this estimated position is then compared to the key value being sought. If it is not equal, then depending on the comparison, the remaining search space is reduced to the part before or after the estimated position. This method will only work if calculations on the size of differences between key values are sensible.

By comparison, binary search always chooses the middle of the remaining search space, discarding one half or the other, depending on the comparison between the key found at the estimated position and the key sought — it does not require numerical values for the keys, just a total order on them. The remaining search space is reduced to the part before or after the estimated position. The linear search uses equality only as it compares elements one-by-one from the start, ignoring any sorting.

On average the interpolation search makes about log(log(n)) comparisons (if the elements are uniformly distributed), where n is the number of elements to be searched. In the worst case (for instance where the numerical values of the keys increase exponentially) it can make up to O(n) comparisons.

In interpolation-sequential search, interpolation is used to find an item near the one being searched for, then linear search is used to find the exact item.

Source: [Wikipedia][interpolation-wiki]

Jump Search

Jump search or block search refers to a search algorithm for ordered lists. It works by first checking all items Lkm, where {\displaystyle k\in \mathbb {N} } k\in \mathbb {N} and m is the block size, until an item is found that is larger than the search key. To find the exact position of the search key in the list a linear search is performed on the sublist L[(k-1)m, km].

The optimal value of m is √n, where n is the length of the list L. Because both steps of the algorithm look at, at most, √n items the algorithm runs in O(√n) time. This is better than a linear search, but worse than a binary search. The advantage over the latter is that a jump search only needs to jump backwards once, while a binary can jump backwards up to log n times. This can be important if a jumping backwards takes significantly more time than jumping forward.

The algorithm can be modified by performing multiple levels of jump search on the sublists, before finally performing the linear search. For an k-level jump search the optimum block size ml for the lth level (counting from 1) is n(k-l)/k. The modified algorithm will perform k backward jumps and runs in O(kn1/(k+1)) time.

Source: [Wikipedia][jump-wiki]

Quick Select

![alt text][QuickSelect-image]

Quick Select is a selection algorithm to find the kth smallest element in an unordered list. It is related to the quicksort sorting algorithm. Like quicksort, it was developed by Tony Hoare, and thus is also known as Hoare's selection algorithm.[1] Like quicksort, it is efficient in practice and has good average-case performance, but has poor worst-case performance. Quickselect and its variants are the selection algorithms most often used in efficient real-world implementations.

Quickselect uses the same overall approach as quicksort, choosing one element as a pivot and partitioning the data in two based on the pivot, accordingly as less than or greater than the pivot. However, instead of recursing into both sides, as in quicksort, quickselect only recurses into one side – the side with the element it is searching for. This reduces the average complexity from O(n log n) to O(n), with a worst case of O(n2).

As with quicksort, quickselect is generally implemented as an in-place algorithm, and beyond selecting the k'th element, it also partially sorts the data. See selection algorithm for further discussion of the connection with sorting.

Source: [Wikipedia][quick-wiki]

Tabu

Tabu search uses a local or neighborhood search procedure to iteratively move from one potential solution {\displaystyle x} x to an improved solution {\displaystyle x'} x' in the neighborhood of {\displaystyle x} x, until some stopping criterion has been satisfied (generally, an attempt limit or a score threshold). Local search procedures often become stuck in poor-scoring areas or areas where scores plateau. In order to avoid these pitfalls and explore regions of the search space that would be left unexplored by other local search procedures, tabu search carefully explores the ne

Core symbols most depended-on inside this repo

split
called by 67
sorts/external-sort.py
pop
called by 43
data_structures/stacks/stack.py
count
called by 29
data_structures/binary tree/AVLtree.py
getright
called by 28
data_structures/binary tree/AVLtree.py
getleft
called by 27
data_structures/binary tree/AVLtree.py
getheight
called by 26
data_structures/binary tree/AVLtree.py
add
called by 22
data_structures/linked_list/__init__.py
keys
called by 19
data_structures/hashing/hash_table.py

Shape

Function 481
Method 384
Class 71

Languages

Python100%

Modules by API surface

data_structures/binary tree/AVLtree.py34 symbols
linear_algebra_python/src/lib.py28 symbols
Graphs/Directed and Undirected (Weighted) Graph.py28 symbols
data_structures/binary tree/binary_search_tree.py26 symbols
sorts/external-sort.py23 symbols
linear_algebra_python/src/tests.py20 symbols
other/primelib.py18 symbols
Graphs/dijkstra_algorithm.py18 symbols
data_structures/avl.py17 symbols
Graphs/multi_hueristic_astar.py17 symbols
neural_network/bpnn.py16 symbols
neural_network/convolution_neural_network.py15 symbols

For agents

$ claude mcp add TheAlgorithms-Python \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact