Fundamentals

What Is a Sorting Algorithm?

A sorting algorithm rearranges data into order. Learn what sorting algorithms are, why they matter, and how to see them work with an interactive visualizer.

Aman Jaiman
6 min read
On this page +
  1. In plain English
  2. A plain-English definition
  3. Why sorting matters so much
  4. Where you already use sorting
  5. See it for yourself
  6. Frequently asked questions

A sorting algorithm is a step-by-step procedure that rearranges the elements of a list into a defined order — usually ascending or descending. It sounds simple, but sorting underpins an enormous amount of the software you use every day, and it is one of the first topics every computer science student studies for good reason.

This guide explains what sorting algorithms are, why they matter, and how watching one run in a sorting algorithm visualizer turns an abstract idea into something you can actually see.

A plain-English definition #

Given a collection of items that can be compared — numbers, words, dates, or records with a key — a sorting algorithm produces a new arrangement where every element sits in its correct position relative to the others. For ascending numeric order, that means each value is greater than or equal to the one before it.

The comparison rule is flexible. You can sort integers smallest-to-largest, strings alphabetically, or a list of users by signup date. The algorithm does not care what the data means; it only needs a way to decide whether one element should come before another.

Why sorting matters so much #

Sorted data unlocks dramatically faster operations:

  • Searching — binary search finds an item in a sorted array in O(log n) time versus O(n) for a linear scan. Sorting 1,000,000 items lets you search in about 20 steps instead of a million.
  • Deduplication — once equal items sit next to each other, removing duplicates is a single linear pass.
  • Merging and joining — combining two sorted datasets is far cheaper than combining unsorted ones, which is why databases sort before merge-joins.
  • Human readability — leaderboards, file listings, and search results all rely on order to be useful.

Where you already use sorting #

You rely on sorting constantly without noticing: your email inbox ordered by date, a spreadsheet sorted by column, an e-commerce page sorted by price, autocomplete suggestions ranked by relevance, and database queries with an ORDER BY clause. Every one of those is a sorting algorithm doing its job in the background.

See it for yourself #

The fastest way to build intuition is to watch the bars move. Open the interactive visualizer, pick Bubble Sort to start, and press play. You will see comparisons highlighted, swaps happen in real time, and the array gradually click into order. Then read our algorithm documentation for the complexity details behind each one.

Found this useful?

Share it with someone who is learning this too.

Questions

Frequently asked questions

What is the simplest sorting algorithm to understand? +
Bubble Sort is the simplest to understand. It repeatedly compares adjacent pairs and swaps them if they are out of order, which makes the mechanics of comparison and swapping very easy to follow in a visualizer.
How many sorting algorithms are there? +
There are dozens of documented sorting algorithms, but around 10 to 15 are commonly studied, including Bubble, Selection, Insertion, Merge, Quick, Heap, Shell, Counting, Radix, and Tim Sort.
Do I need to memorize sorting algorithms? +
For interviews you should understand how the main algorithms work and their time complexity, but in day-to-day work you almost always use your language's built-in sort rather than writing one from scratch.

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