Sorting Algorithms

Watch how arrays get sorted, step by step — comparisons, swaps, and final order.

Bubble Sort

Repeatedly swaps adjacent elements if they are in the wrong order.

Avg: O(n²) · Space: O(1)

Selection Sort

Finds the minimum from the unsorted part and places it at the front.

Avg: O(n²) · Space: O(1)

Insertion Sort

Builds sorted array one element at a time by inserting into correct position.

Avg: O(n²) · Space: O(1)

Merge Sort

Divides array into halves, sorts each, then merges them back together.

Avg: O(n log n) · Space: O(n)

Quick Sort

Picks a pivot, partitions array around it, recursively sorts each side.

Avg: O(n log n) · Space: O(log n)

Heap Sort

Builds a max heap, then repeatedly extracts the largest element.

Avg: O(n log n) · Space: O(1)

Counting Sort

Counts occurrences of each value, then places them in order. Works best for small integer ranges.

Avg: O(n + k) · Space: O(n + k)

Radix Sort

Sorts numbers digit by digit, from least to most significant, using counting sort as a subroutine.

Avg: O(nk) · Space: O(n + k)

Shell Sort

Generalizes insertion sort by comparing elements far apart first, shrinking the gap each pass.

Avg: O(n^1.3) · Space: O(1)

Bucket Sort

Distributes elements into buckets, sorts each bucket, then concatenates them.

Avg: O(n + k) · Space: O(n + k)

Tim Sort

Splits array into small runs sorted with insertion sort, then merges runs using merge sort.

Avg: O(n log n) · Space: O(n)

Intro Sort

Hybrid of quicksort and insertion sort — uses quicksort for large ranges, insertion sort for small ones.

Avg: O(n log n) · Space: O(log n)

Bitonic Sort

Builds bitonic sequences and merges them recursively. Parallelizable, requires power-of-2 length (auto-padded here).

Avg: O(log² n) · Space: O(n log n)