Sorting Algorithms
Pick an algorithm to watch comparisons, swaps and sorted regions animate on the graph-paper canvas — with pseudocode highlighting the line that's executing right now.
Bubble Sort
A comparison sort that repeatedly steps through the array, compares each pair of adjacent elements, and swaps them if they are in the wrong order. Each full pass 'bubbles' the largest remaining value to its final position, so the algorithm needs at most n-1 passes.
Open visualizer
O(n²) · O(1) spaceSelection Sort
A comparison sort that divides the array into a sorted prefix and an unsorted suffix. On each pass it selects the minimum element of the unsorted suffix and swaps it into the next position of the sorted prefix, growing it by one element at a time.
Open visualizer
O(n²) · O(1) spaceInsertion Sort
A comparison sort that builds the final sorted array one element at a time. It takes each element from the unsorted part and inserts it into its correct position within the already-sorted prefix, shifting larger elements right to make room — the same way you'd sort playing cards in your hand.
Open visualizer
O(n log n) · O(n) spaceMerge Sort
A divide-and-conquer comparison sort. It recursively splits the array in half until each piece is a single element (trivially sorted), then merges pairs of sorted pieces back together in order. The recursion forms a binary tree: dividing takes it down, merging brings the sorted result back up.
Open visualizer
O(n log n) avg, O(n²) worst · O(log n) spaceQuick Sort
A divide-and-conquer comparison sort. It chooses a pivot element and partitions the array so everything smaller than the pivot ends up on its left and everything larger ends up on its right, placing the pivot at its final sorted index. It then recurses on the two partitions independently — the pivot never needs to move again.
Open visualizer
O(n log n) · O(1) spaceHeap Sort
A comparison sort built on the binary heap data structure. It first arranges the array into a max-heap (every parent is ≥ its children), so the largest value sits at the root. It then repeatedly swaps the root with the last unsorted element and 'sifts down' the new root to restore the heap property, shrinking the heap by one each time.
Open visualizer
O(n log² n) (gap-dependent) · O(1) spaceShell Sort
A generalization of insertion sort. Instead of only comparing neighbours, it compares elements a fixed gap h apart, insertion-sorting each of those interleaved sub-sequences. The gap shrinks on each pass (here, halved every time) down to 1, so early passes move far-out-of-place elements long distances quickly, leaving little work for the final gap-1 pass.
Open visualizer
O(n + k) · O(k) spaceCounting Sort
A non-comparison integer sort. Instead of comparing elements, it counts how many times each distinct value (0 to k) occurs, then uses those counts to write every value directly into its final sorted position. Because it never compares elements, it beats the O(n log n) comparison-sort lower bound — at the cost of needing k to be small relative to n.
Open visualizer
O(d · (n + b)) · O(n + b) spaceRadix Sort
A non-comparison integer sort that processes digits rather than whole values. It repeatedly buckets elements by one digit at a time — least significant first — using a stable counting/bucket pass, and re-collects them after each pass. After d passes (d = number of digits in the largest value), the array is fully sorted.
Open visualizer
O(n + k) avg, O(n²) worst · O(n + k) spaceBucket Sort
A distribution sort that scatters elements into a fixed number of buckets based on their value range, sorts each (usually small) bucket individually, and concatenates the buckets back together in order. It works best when input values are spread roughly uniformly across the range, so buckets fill evenly.
Open visualizer
O(n²) · O(1) spaceCocktail Shaker Sort
A bidirectional variant of bubble sort. It alternates passes: left-to-right, bubbling the largest remaining value to the end, then right-to-left, sinking the smallest remaining value to the start. Shrinking the unsorted window from both ends each round lets small values near the end escape faster than plain bubble sort would move them.
Open visualizer
O(n²) · O(1) spaceGnome Sort
A comparison sort resembling insertion sort but implemented with a single moving position instead of nested loops. It steps forward whenever the current pair is in order, and whenever it isn't, it swaps the pair and steps back one place — repeating the check — until the prefix behind it is locally sorted again.
Open visualizer