Fundamentals

How Do Sorting Algorithms Work?

Updated June 8, 2026 7 min read

At first glance the dozen common sorting algorithms look very different, but they are all built from the same handful of primitive operations. Once you understand compare, swap, insert, and merge, every algorithm becomes a recipe that combines them in a particular way.

This article breaks down those building blocks and shows how they fit together — and the best companion is the visualizer, where each operation is colour-coded as it happens.

The core operations

  • Compare — ask whether element A should come before element B. This is the fundamental decision every comparison sort makes.
  • Swap — exchange the positions of two elements. Bubble, Selection, Quick, and Heap Sort all rely on swapping.
  • Insert — lift an element out and place it into its correct spot among already-sorted elements. This is the heart of Insertion Sort.
  • Merge — combine two already-sorted sequences into one sorted sequence. This powers Merge Sort and Tim Sort.

Two big families

Most algorithms fall into one of two strategies. Iterative sorts such as Bubble, Selection, and Insertion walk through the array with nested loops, gradually growing a sorted region. Divide-and-conquer sorts such as Merge and Quick split the problem into smaller sub-problems, solve each, and combine the results — which is how they reach O(n log n) performance.

A third family, non-comparison sorts like Counting and Radix Sort, avoids comparing elements at all and instead uses the values themselves as array indices.

Watching the operations happen

In the Sort & Visualize tool, colour tells you exactly which operation is running: amber bars are being compared, red bars are being swapped, cyan marks the current element, purple is the pivot in Quick Sort, and green confirms an element is in its final sorted position. Slowing the speed down to x1 lets you trace a single comparison at a time.

From operations to complexity

The number of compares and swaps an algorithm performs is exactly what its time complexity measures. Nested loops over n elements give O(n²); halving the problem repeatedly gives the log factor in O(n log n). Counting operations as you watch is the most intuitive way to feel the difference between a slow and a fast algorithm.

Frequently asked questions

What are the basic operations in sorting? +
The basic operations are comparing two elements, swapping their positions, inserting an element into a sorted region, and merging two sorted sequences. Different algorithms combine these in different ways.
What is the difference between iterative and recursive sorting? +
Iterative sorts use loops and constant stack space (Bubble, Insertion, Selection). Recursive sorts like Merge and Quick Sort divide the array into sub-problems using function calls, using O(log n) stack space.

See it in motion

Watch this algorithm and 9 others run step by step in our free interactive visualizer.

▶ Launch Visualiser

Related articles

← Back to all articles