Fundamentals

How Do Sorting Algorithms Work?

Sorting algorithms work by comparing and rearranging elements. Learn the core operations — compare, swap, insert, merge — behind every sorting algorithm.

Aman Jaiman
7 min read
On this page +
  1. In plain English
  2. The core operations
  3. Two big families
  4. Watching the operations happen
  5. From operations to complexity
  6. Frequently asked questions

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 visualiser, 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.

Found this useful?

Share it with someone who is learning this too.

Questions

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 nine others run step by step, with live pseudocode and comparison counters.

Launch the visualiser
Aman Jaiman
Written by
Aman Jaiman

Software engineer at a stealth-stage startup, and previously a front-end engineer for around a year and a half.

Keep reading

Related guides

Link copied