Interview's MateDSA Visualizer
Interview's Mate

DSA Visualizer

Learn by watching algorithms run

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
All topics
FOUNDATIONS

Sorting

Put data in order - and know what each algorithm costs and keeps stable.

Intermediate12 problems5 interactive subtopicsO(n²) here · O(n log n) best
Practice in AI DSA
WalkthroughOverviewRecognize itMistakes & edge casesKnowledge check (3)Practice
Algorithms & custom examples (5)Open in full playground →
STEP 1/5Pick 2
key = 2
5
2
4
1
0123
i
  • visited / done
  • active this step
  • result
Space Play←→ StepR Replay
1 / 5
PYTHONnums = [5, 2, 4, 1]
1def insertion_sort(nums):
2 for i in range(1, len(nums)):
▶3 key = nums[i]
4 j = i - 1
5 while j >= 0 and nums[j] > key:
6 nums[j + 1] = nums[j]
7 j -= 1
8 nums[j + 1] = key
9 return nums
STEP 1 OF 5Setup

Pick 2

Everything left of index 1 - just [5] - is already sorted.

VARIABLES

i
1
key
2
nums
[5, 2, 4, 1]
02

Intuition & complexity

The core mental model, trade-offs, and Big-O cost.

What it is

Sorting arranges elements by a key. Merge sort and heap sort take O(n log n) in the worst case; simple sorts like insertion sort take O(n²).

Why it matters

Sorted data unlocks binary search, two pointers, interval merging and easy duplicate detection.

Think of it like this

Insertion sort is how most people sort a hand of cards: take the next card and slide it left into place.

Use it when

  • Order makes the rest of the problem easy (pairs, intervals, duplicates)
  • The output must be ordered
  • n is small enough that O(n log n) fits

Avoid it when

  • You only need the top k of a huge list - a heap is O(n log k)
  • Values are small integers in a known range - counting sort is O(n + k)

Complexity cheatsheet

OperationTimeSpace
Bubble, selection, insertionO(n²)O(1)
Merge sortO(n log n)O(n)
Quick sort (average)O(n log n)O(log n)

Often confused with: heaps - they keep the minimum ready without fully sorting.

03

How to recognize it

Clues in interview prompts that point straight to this pattern.

sorted orderkth largestmergeintervalsarrange
  • If sorting first is allowed, O(n log n) is often the target
  • Stability matters when you sort by one key after another
04

Mistakes & edge cases

Catch common off-by-one bugs and corner cases before submitting.

Common mistakes

  • Assuming quick sort is always O(n log n) - bad pivots make it O(n²)
  • Forgetting sorted() returns a new list while list.sort() sorts in place
  • Sorting when one pass or a heap would do

Edge cases to test

  • Already sorted input - insertion sort’s best case, O(n)
  • Reverse-sorted input - its worst case
  • Many duplicates
Interview tip: Python’s built-in sort is Timsort: stable and O(n log n) in the worst case. Say so if you rely on it.
05

Knowledge check

Quick check to lock in the intuition.

Question 1 of 3

Which sort is stable and O(n log n) in the worst case?

06

Practice

Apply what you just watched on real interview problems.

Generate Array problem in AI DSA Practice →
  • BeginnerMerge Sorted ArrayLeetCode 88 ↗ (opens in a new tab)
  • IntermediateSort an ArrayLeetCode 912 ↗ (opens in a new tab)
  • AdvancedCount of Smaller Numbers After SelfLeetCode 315 ↗ (opens in a new tab)
RecursionSearching