Algorithm Library

Explore complexity, stability, and implementation details.

Binary Insertion Sort

O(n2)O(n^2)

A variant of Insertion Sort that uses binary search to find the correct position for the next element, significantly reducing the number of comparisons.

Insertion
Sorts by building a final sorted array one item at a time, inserting it into place.
In-Place
Requires a constant amount of extra memory space (O(1)), regardless of input size.
Stable
Preserves the relative order of elements with equal values.

Bogo Sort

O(nn!)O(n \cdot n!)

A famously inefficient algorithm that repeatedly generates random permutations of a list until it discovers one that is sorted.

Esoteric
Algorithms that are more conceptual, humorous, or illustrative of a theoretical point than for practical use.
Brute Force
Sorts by generating and testing possibilities until a solution is found.
Probabilistic
Uses random choices to reduce the probability of worst-case performance.
In-Place
Requires a constant amount of extra memory space (O(1)), regardless of input size.
Unstable
Does not guarantee the relative order of elements with equal values.

Bogobogo Sort

O((n!)!)O((n!)!)

A monumentally inefficient recursive sorting algorithm that sorts the first n-1 elements before checking the nth, leading to an astronomically large complexity.

Esoteric
Algorithms that are more conceptual, humorous, or illustrative of a theoretical point than for practical use.
Brute Force
Sorts by generating and testing possibilities until a solution is found.
Probabilistic
Uses random choices to reduce the probability of worst-case performance.
Unstable
Does not guarantee the relative order of elements with equal values.

Bozo Sort

O(n!)O(n!)

A terribly inefficient algorithm that, if the list is not sorted, swaps two randomly chosen elements and checks again.

Esoteric
Algorithms that are more conceptual, humorous, or illustrative of a theoretical point than for practical use.
Brute Force
Sorts by generating and testing possibilities until a solution is found.
Probabilistic
Uses random choices to reduce the probability of worst-case performance.
In-Place
Requires a constant amount of extra memory space (O(1)), regardless of input size.
Unstable
Does not guarantee the relative order of elements with equal values.

Bubble Sort

O(n2)O(n^2)

The simplest sorting algorithm that repeatedly swaps adjacent elements if they are in the wrong order.

Exchange
Sorts by repeatedly swapping adjacent elements to move them to their correct positions.
Adaptive
Performance improves significantly on data that is already partially sorted.
In-Place
Requires a constant amount of extra memory space (O(1)), regardless of input size.
Stable
Preserves the relative order of elements with equal values.

Cocktail Sort

O(n2)O(n^2)

A bidirectional variation of Bubble Sort that sorts in both directions on each pass to improve performance by moving elements to their correct position more quickly.

Exchange
Sorts by repeatedly swapping adjacent elements to move them to their correct positions.
Adaptive
Performance improves significantly on data that is already partially sorted.
In-Place
Requires a constant amount of extra memory space (O(1)), regardless of input size.
Stable
Preserves the relative order of elements with equal values.

Comb Sort

O(n2/2p)O(n^2 / 2^p)

An improvement on Bubble Sort that eliminates "turtles" (small values near the end) by comparing and swapping elements that are far apart.

Exchange
Sorts by repeatedly swapping adjacent elements to move them to their correct positions.
In-Place
Requires a constant amount of extra memory space (O(1)), regardless of input size.
Unstable
Does not guarantee the relative order of elements with equal values.

Counting Sort

O(n+k)O(n+k)

An integer sorting algorithm that operates by counting the number of objects that possess distinct key values, then using arithmetic to determine the positions of each key value in the output sequence.

Counting
Counts occurrences of each distinct element and reconstructs the sorted array.
Stable
Preserves the relative order of elements with equal values.

Cycle Sort

O(n2)O(n^2)

An in-place, unstable sorting algorithm that is theoretically optimal in terms of the total number of writes to the original array.

Exchange
Sorts by repeatedly swapping adjacent elements to move them to their correct positions.
In-Place
Requires a constant amount of extra memory space (O(1)), regardless of input size.
Unstable
Does not guarantee the relative order of elements with equal values.

Gnome Sort

O(n2)O(n^2)

A simple sorting algorithm that moves an element to its correct place through a series of adjacent swaps, resembling a garden gnome sorting flower pots.

Insertion
Sorts by building a final sorted array one item at a time, inserting it into place.
Adaptive
Performance improves significantly on data that is already partially sorted.
In-Place
Requires a constant amount of extra memory space (O(1)), regardless of input size.
Stable
Preserves the relative order of elements with equal values.

Heap Sort

O(nlogn)O(n \log n)

A comparison-based sorting algorithm that uses a Binary Heap data structure. It is an optimized version of selection sort that builds a Max-Heap to efficiently find the maximum element.

Heap
Uses a heap data structure to efficiently find and extract elements in sorted order.
Selection
Sorts by repeatedly finding the minimum element and placing it at the beginning.
In-Place
Requires a constant amount of extra memory space (O(1)), regardless of input size.
Unstable
Does not guarantee the relative order of elements with equal values.

Insertion Sort

O(n2)O(n^2)

Builds the final sorted array one item at a time by repeatedly taking the next element and inserting it into its correct position among the already-sorted elements.

Insertion
Sorts by building a final sorted array one item at a time, inserting it into place.
Adaptive
Performance improves significantly on data that is already partially sorted.
In-Place
Requires a constant amount of extra memory space (O(1)), regardless of input size.
Stable
Preserves the relative order of elements with equal values.

Intelligent Design Sort

O(1)O(1)

A sorting algorithm based on the theological theory of intelligent design.

Esoteric
Algorithms that are more conceptual, humorous, or illustrative of a theoretical point than for practical use.
Adaptive
Performance improves significantly on data that is already partially sorted.
In-Place
Requires a constant amount of extra memory space (O(1)), regardless of input size.
Stable
Preserves the relative order of elements with equal values.

Intro Sort

O(nlogn)O(n \log n)

A hybrid sorting algorithm that provides both fast average performance and optimal worst-case performance by combining Quick Sort, Heap Sort, and Insertion Sort.

Hybrid
Combines two or more algorithms to leverage their specific strengths for different data sizes.
Divide and Conquer
Recursively breaks the problem into smaller sub-problems, solves them, and combines the results.
Heap
Uses a heap data structure to efficiently find and extract elements in sorted order.
Insertion
Sorts by building a final sorted array one item at a time, inserting it into place.
In-Place
Requires a constant amount of extra memory space (O(1)), regardless of input size.
Unstable
Does not guarantee the relative order of elements with equal values.

Merge Sort

O(nlogn)O(n \log n)

A highly reliable, stable sorting algorithm that works by recursively dividing the array into halves, sorting them, and merging them back together.

Divide and Conquer
Recursively breaks the problem into smaller sub-problems, solves them, and combines the results.
Stable
Preserves the relative order of elements with equal values.

Miracle Sort

O()O(\infty)

A humorous algorithm that repeatedly checks if an array is sorted, waiting for a "miracle" to spontaneously sort the data.

Esoteric
Algorithms that are more conceptual, humorous, or illustrative of a theoretical point than for practical use.
Adaptive
Performance improves significantly on data that is already partially sorted.
In-Place
Requires a constant amount of extra memory space (O(1)), regardless of input size.
Stable
Preserves the relative order of elements with equal values.

Odd-Even Sort

O(n2)O(n^2)

A variation of Bubble Sort that sorts by repeatedly applying comparison and swap operations to elements in separate "odd" and "even" phases.

Exchange
Sorts by repeatedly swapping adjacent elements to move them to their correct positions.
Adaptive
Performance improves significantly on data that is already partially sorted.
In-Place
Requires a constant amount of extra memory space (O(1)), regardless of input size.
Stable
Preserves the relative order of elements with equal values.

Pancake Sort

O(n2)O(n^2)

Sorts a stack of elements (pancakes) by repeatedly flipping the spatula under the largest unsorted element to move it to the top, then flipping the whole stack to move it to the bottom.

Selection
Sorts by repeatedly finding the minimum element and placing it at the beginning.
In-Place
Requires a constant amount of extra memory space (O(1)), regardless of input size.
Unstable
Does not guarantee the relative order of elements with equal values.

Patience Sort

O(nlogn)O(n \log n)

A sorting algorithm based on the card game "Patience". It sorts by distributing elements into piles according to specific rules, then merging the piles efficiently using a priority queue.

Insertion
Sorts by building a final sorted array one item at a time, inserting it into place.
Heap
Uses a heap data structure to efficiently find and extract elements in sorted order.
Unstable
Does not guarantee the relative order of elements with equal values.

Quantum Bogo Sort

O(n)O(n)

A hypothetical algorithm that generates a single random permutation of a list, and if it is not sorted, destroys the universe.

Esoteric
Algorithms that are more conceptual, humorous, or illustrative of a theoretical point than for practical use.
Probabilistic
Uses random choices to reduce the probability of worst-case performance.
In-Place
Requires a constant amount of extra memory space (O(1)), regardless of input size.
Unstable
Does not guarantee the relative order of elements with equal values.

Quick Sort

O(nlogn)O(n \log n)

An efficient, divide-and-conquer algorithm that works by selecting a "pivot" element and partitioning the other elements into two sub-arrays.

Divide and Conquer
Recursively breaks the problem into smaller sub-problems, solves them, and combines the results.
Exchange
Sorts by repeatedly swapping adjacent elements to move them to their correct positions.
In-Place
Requires a constant amount of extra memory space (O(1)), regardless of input size.
Unstable
Does not guarantee the relative order of elements with equal values.

Radix Sort

O(d(n+k))O(d(n+k))

A non-comparative sorting algorithm that sorts integers by processing them digit by digit, from least significant to most significant.

Radix
Sorts by processing individual digits or characters from least to most significant.
Stable
Preserves the relative order of elements with equal values.

Selection Sort

O(n2)O(n^2)

A simple sorting algorithm that repeatedly finds the minimum element from the unsorted part and puts it at the beginning.

Selection
Sorts by repeatedly finding the minimum element and placing it at the beginning.
In-Place
Requires a constant amount of extra memory space (O(1)), regardless of input size.
Unstable
Does not guarantee the relative order of elements with equal values.

Shell Sort

O(n3/2)O(n^{3/2})

An optimization of Insertion Sort that allows the exchange of items that are far apart, improving efficiency by moving elements to their correct position more quickly.

Insertion
Sorts by building a final sorted array one item at a time, inserting it into place.
Adaptive
Performance improves significantly on data that is already partially sorted.
In-Place
Requires a constant amount of extra memory space (O(1)), regardless of input size.
Unstable
Does not guarantee the relative order of elements with equal values.

Slow Sort

O(nlog2(n)2+ϵ)O(n^{\frac{\log_2(n)}{2+\epsilon}})

A humorous and notoriously inefficient sorting algorithm based on the parody principle of 'multiply and surrender'.

Esoteric
Algorithms that are more conceptual, humorous, or illustrative of a theoretical point than for practical use.
Divide and Conquer
Recursively breaks the problem into smaller sub-problems, solves them, and combines the results.
In-Place
Requires a constant amount of extra memory space (O(1)), regardless of input size.
Unstable
Does not guarantee the relative order of elements with equal values.

Smooth Sort

O(nlogn)O(n \log n)

A variation of Heapsort that uses a forest of Leonardo-number-sized heaps to adapt to pre-sorted input, achieving O(n) best-case time while maintaining O(1) auxiliary space.

Heap
Uses a heap data structure to efficiently find and extract elements in sorted order.
Selection
Sorts by repeatedly finding the minimum element and placing it at the beginning.
Adaptive
Performance improves significantly on data that is already partially sorted.
In-Place
Requires a constant amount of extra memory space (O(1)), regardless of input size.
Unstable
Does not guarantee the relative order of elements with equal values.

Stalin Sort

O(n)O(n)

A ruthless and efficient single-pass algorithm that eliminates any element that is out of order, sending it to the Gulag.

Esoteric
Algorithms that are more conceptual, humorous, or illustrative of a theoretical point than for practical use.
Adaptive
Performance improves significantly on data that is already partially sorted.
Stable
Preserves the relative order of elements with equal values.

Stooge Sort

O(n2.709)O(n^{2.709})

A highly inefficient recursive sorting algorithm that works by recursively sorting overlapping two-thirds sections of the array.

Divide and Conquer
Recursively breaks the problem into smaller sub-problems, solves them, and combines the results.
Esoteric
Algorithms that are more conceptual, humorous, or illustrative of a theoretical point than for practical use.
Unstable
Does not guarantee the relative order of elements with equal values.

Strand Sort

O(n2)O(n^2)

A recursive sorting algorithm that repeatedly pulls the longest increasing subsequence (strand) from the list and merges it into a sorted output.

Selection
Sorts by repeatedly finding the minimum element and placing it at the beginning.
Adaptive
Performance improves significantly on data that is already partially sorted.
Stable
Preserves the relative order of elements with equal values.

Thanos Sort

O(n2)O(n^2)

A simple calculus. This universe is finite, its resources finite. If data is left unchecked, it will cease to be ordered. It needs correction.

Esoteric
Algorithms that are more conceptual, humorous, or illustrative of a theoretical point than for practical use.
Probabilistic
Uses random choices to reduce the probability of worst-case performance.
Adaptive
Performance improves significantly on data that is already partially sorted.
In-Place
Requires a constant amount of extra memory space (O(1)), regardless of input size.
Stable
Preserves the relative order of elements with equal values.

Tree Sort

O(nlogn)O(n \log n)

A sorting algorithm that builds a Binary Search Tree (BST) from the input elements and then performs an in-order traversal to retrieve the elements in sorted order.

Tree
Constructs a tree data structure to organize elements, then traverses it to produce a sorted sequence.
Unstable
Does not guarantee the relative order of elements with equal values.