📊

Sorting Fundamentals

Learn key sorting algorithms, when to use each, and how sorting unlocks other patterns.

0 Problems1-2 hours

Overview

Sorting arranges elements in a defined order. In interviews you rarely implement sorting from scratch — you use the language built-in sort (O(n log n)). But understanding how sorting works helps you choose the right approach, reason about stability, and know when sorting is the first step to solving a harder problem.

Complexity Summary

Time Complexity

Comparison sorts O(n log n) lower bound · Non-comparison O(n)

Space Complexity

Merge Sort O(n) · Quick/Heap O(log n) stack · Counting O(k)

Key Patterns & Techniques

Learn the core patterns in this topic. Each block explains when to use the pattern, the intuition behind it, and a compact code example.

1

Bubble Sort

Concept

O(n²) — repeatedly swap adjacent elements; simple but slow

Practice questions for this pattern
2

Selection Sort

Concept

O(n²) — find min and place it; not stable

Practice questions for this pattern
3

Insertion Sort

Concept

O(n²) worst, O(n) best — good for nearly sorted data

Practice questions for this pattern
4

Merge Sort

Concept

O(n log n) — divide and conquer; stable; uses O(n) extra space

Practice questions for this pattern
5

Quick Sort

Concept

O(n log n) avg, O(n²) worst — in-place; fastest in practice

Practice questions for this pattern
6

Heap Sort

Concept

O(n log n) — uses a heap; in-place but not stable

Practice questions for this pattern
7

Counting / Radix Sort

Concept

O(n) — works on integers in a range

Practice questions for this pattern
8

Stable sort

Concept

equal elements preserve their original order (Merge Sort is stable)

Practice questions for this pattern
9

Built-in sort

Concept

always prefer language built-ins in interviews unless asked otherwise

Practice questions for this pattern
DSA Practice Online - 150+ Coding Interview Questions | LeetCode Alternative | InstaMock - AI Mock Interview