Sorting Algorithms

Explore different sorting algorithms through interactive visualizations. Compare their performance, understand their mechanics, and see how they work step-by-step.

Bubble Sort

Beginner

A simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order.

Time Complexity
O(n²)
Space Complexity
O(1)
Properties
StableIn-placeAdaptive
Visualize Algorithm

Insertion Sort

Beginner

Builds the sorted array one element at a time by repeatedly taking an element and inserting it into its correct position.

Time Complexity
O(n²)
Space Complexity
O(1)
Properties
StableIn-placeAdaptive
Visualize Algorithm

Selection Sort

Beginner

Finds the minimum element from the unsorted portion and places it at the beginning of the sorted portion.

Time Complexity
O(n²)
Space Complexity
O(1)
Properties
UnstableIn-placeNon-adaptive
Visualize Algorithm

Quick Sort

Intermediate

A divide-and-conquer algorithm that picks a pivot element and partitions the array around it, then recursively sorts the sub-arrays.

Time Complexity
O(n log n)
Space Complexity
O(log n)
Properties
UnstableIn-placeDivide & Conquer
Visualize Algorithm

Merge Sort

Intermediate

A stable divide-and-conquer algorithm that divides the array into halves, recursively sorts them, and then merges the sorted halves.

Time Complexity
O(n log n)
Space Complexity
O(n)
Properties
StableExternalDivide & Conquer
Visualize Algorithm

Heap Sort

Intermediate

Uses a binary heap data structure to sort elements. Builds a max-heap and repeatedly extracts the maximum element.

Time Complexity
O(n log n)
Space Complexity
O(1)
Properties
UnstableIn-placeHeap-based
Visualize Algorithm

Radix Sort

Advanced

A non-comparison based algorithm that sorts numbers digit by digit, from least to most significant digit.

Time Complexity
O(d × n)
Space Complexity
O(n + k)
Properties
StableNon-comparisonLinear time
Visualize Algorithm

Counting Sort

Intermediate

Counts occurrences of each value, then uses cumulative counts to place elements directly into their sorted positions.

Time Complexity
O(n + k)
Space Complexity
O(n + k)
Properties
StableNon-comparisonLinear time
Visualize Algorithm

Shell Sort

Intermediate

Generalized insertion sort that compares elements far apart, progressively reducing the gap for faster convergence.

Time Complexity
O(n log² n)
Space Complexity
O(1)
Properties
UnstableIn-placeGap-based
Visualize Algorithm

Tim Sort

Advanced

A hybrid algorithm combining insertion sort and merge sort. The default sort in Python and Java — optimized for real-world data.

Time Complexity
O(n log n)
Space Complexity
O(n)
Properties
StableAdaptiveHybrid
Visualize Algorithm

Bucket Sort

Intermediate

Distributes elements into buckets by value range, sorts each bucket individually, then concatenates the results.

Time Complexity
O(n + k)
Space Complexity
O(n + k)
Properties
StableDistribution-basedLinear avg
Visualize Algorithm

Cocktail Shaker Sort

Beginner

A bidirectional variant of bubble sort that alternates sweeping left-to-right and right-to-left each pass.

Time Complexity
O(n²)
Space Complexity
O(1)
Properties
StableIn-placeBidirectional
Visualize Algorithm

Comb Sort

Beginner

Improves on bubble sort by comparing elements with a shrinking gap, eliminating small values stuck at the end.

Time Complexity
O(n²)
Space Complexity
O(1)
Properties
UnstableIn-placeGap-based
Visualize Algorithm

Algorithm Comparison

Quick comparison of time and space complexities

AlgorithmBest CaseAverage CaseWorst CaseSpaceStable
Bubble SortO(n)O(n²)O(n²)O(1)✅ Yes
Insertion SortO(n)O(n²)O(n²)O(1)✅ Yes
Selection SortO(n²)O(n²)O(n²)O(1)❌ No
Quick SortO(n log n)O(n log n)O(n²)O(log n)❌ No
Merge SortO(n log n)O(n log n)O(n log n)O(n)✅ Yes
Heap SortO(n log n)O(n log n)O(n log n)O(1)❌ No
Radix SortO(d×n)O(d×n)O(d×n)O(n+k)✅ Yes
Counting SortO(n+k)O(n+k)O(n+k)O(n+k)✅ Yes
Shell SortO(n log n)O(n^1.5)O(n²)O(1)❌ No
Tim SortO(n)O(n log n)O(n log n)O(n)✅ Yes
Bucket SortO(n+k)O(n+k)O(n²)O(n+k)✅ Yes
Cocktail ShakerO(n)O(n²)O(n²)O(1)✅ Yes
Comb SortO(n log n)O(n²/2^p)O(n²)O(1)❌ No