TheHowPage
Interactive Playground

Sorting Algorithm Visualizer

Choose an algorithm, adjust the array size and speed, then watch it sort — bar by bar, swap by swap. See why Quick Sort is quick and Bubble Sort… isn't.

Algorithm: Bubble SortSteps: 0

How sorting algorithms work

A sorting algorithm takes a collection of items and rearranges them into order. Sounds simple, but the how matters enormously. The difference between a good and bad sorting algorithm can mean the difference between sorting a million items in seconds vs. hours.

The simple ones: O(n²)

Bubble Sort compares adjacent elements and swaps them if they're out of order, bubbling the largest values to the end. Insertion Sort builds the sorted list one element at a time, inserting each new element into its correct position. Selection Sort finds the smallest remaining element and puts it next. All three are O(n²) — fine for small lists, terrible for large ones.

The fast ones: O(n log n)

Merge Sort divides the array in half, recursively sorts each half, then merges them back together. It always takes O(n log n) time but needs extra space. Quick Sort picks a pivot element, partitions the array so everything smaller is on one side and larger on the other, then recursively sorts each side. It's usually the fastest in practice.

Try each one above and watch the difference in speed. Pay attention to the step count — it tells the real story of algorithmic efficiency.

Frequently Asked Questions

What is the fastest sorting algorithm?

It depends on the data. Quick Sort and Merge Sort are both O(n log n) on average, making them the fastest general-purpose comparison sorts. Quick Sort is usually faster in practice due to better cache performance, but Merge Sort guarantees O(n log n) even in the worst case.

What is Bubble Sort and why is it slow?

Bubble Sort repeatedly steps through the list, compares adjacent elements, and swaps them if they're in the wrong order. It's O(n²) — meaning for a list of 1,000 items, it may need up to 1,000,000 comparisons. It's rarely used in practice but great for learning.

When should I use Insertion Sort?

Insertion Sort is excellent for small arrays (under ~20 elements) and nearly-sorted data, where it can approach O(n). Many real-world sorting implementations (like TimSort in Python) use Insertion Sort for small partitions.

What is the difference between Merge Sort and Quick Sort?

Merge Sort divides the array in half, sorts each half, then merges them — it always takes O(n log n) time but needs extra memory. Quick Sort picks a pivot, partitions around it, and sorts each side — it's O(n log n) on average but O(n²) in the worst case. Quick Sort is typically faster in practice.

Keep exploring