Back to All Algorithms
Intro Sort
Category: Sorting | Time: O(n log n) | Space: O(log n)
Visualization
Visual representation of the data structure
About Intro Sort

A hybrid sorting algorithm that provides both fast average performance and optimal worst-case performance. It starts with quicksort, switches to heapsort when the recursion depth exceeds a level based on the number of elements being sorted, and switches to insertion sort for small partitions.

Code Editor
The code is for reference. Editing it won't affect the visualization.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
Variable State
{
  "arr": "[2,1,0,5,8,3,7,4,9,6]"
}
Intro Sort: How It Works
Last updated October 28, 2026

Overview

Intro Sort (introspective sort) starts as Quick Sort but watches its own recursion depth. If the depth passes 2 * floor(log2(n)), the input is behaving like a Quick Sort worst case, so it switches that range to Heap Sort, which is guaranteed O(n log n). Small ranges, under 16 elements, are finished with Insertion Sort.

This combination gives Quick Sort's speed on typical data, Heap Sort's worst-case guarantee, and Insertion Sort's low overhead on tiny pieces. It is the algorithm behind std::sort in most C++ standard libraries and behind .NET's Array.Sort.

The default input has 10 elements, below the 16-element threshold, so the dry run goes straight to Insertion Sort — a nice illustration that hybrid sorts often never use their 'main' algorithm on small inputs. Try 20 or more numbers to see partitioning, and a sorted 40-element list to push it toward the heap-sort fallback.

Step by step

  1. Compute maxdepth = 2 * floor(log2(n)).
  2. If the current range has fewer than 16 elements, sort it with Insertion Sort and return.
  3. If maxdepth has reached 0, sort the range with Heap Sort and return.
  4. Otherwise partition around a pivot as in Quick Sort.
  5. Recurse on both sides with maxdepth - 1.

Complexity

CaseCostWhy
Best caseO(n log n)Balanced partitions, as in Quick Sort.
Average caseO(n log n)Quick Sort behaviour dominates.
Worst caseO(n log n)The depth limit hands bad ranges to Heap Sort.
Extra spaceO(log n)Recursion depth is capped at 2·log2(n).

When to use it

  • General-purpose unstable sorting where worst-case guarantees matter, e.g. sorting untrusted input that could be crafted to attack Quick Sort.
  • Systems code in C and C++ — it is the default choice there.
  • Any time you would use Quick Sort but cannot rule out adversarial or pre-sorted data.

Common mistakes

  • It is not stable; use Merge Sort or Tim Sort when equal keys must keep their order.
  • Getting the depth limit wrong either never triggers the fallback or triggers it too often.
  • The 16-element cutoff is a tuning constant; benchmarking on your platform may suggest a different value.
Worked Example: Intro Sort Dry Run
Generated from the same trace that drives the visualizer above, using the default input.

A dry run is the best way to check your understanding of Intro Sort: follow each row, predict the next one, and compare. The run below takes 80 steps in total. Steps that only re-print loop counters are left out so the decisions stand out. The Line column refers to the line numbers in the code listing above.

Input
2, 1, 0, 5, 8, 3, 7, 4, 9, 6
Result
return value = [0,1,2,3,4,5,6,7,8,9], arr = [0,1,2,3,4,5,6,7,8,9]
StepLineArrayWhat happens
11[2, 1, 0, 5, 8, 3, 7, 4, 9, 6]
49[2, 1, 0, 5, 8, 3, 7, 4, 9, 6]status = Switching to Insertion Sort
568[2, 1, 0, 5, 8, 3, 7, 4, 9, 6]begin = 0, end = 9, status = Running Insertion Sort
972[2, 1, 0, 5, 8, 3, 7, 4, 9, 6]j = 0, key = 1, condition = 2 > 1
1073[2, 2, 0, 5, 8, 3, 7, 4, 9, 6]
1276[1, 2, 0, 5, 8, 3, 7, 4, 9, 6]
1672[1, 2, 0, 5, 8, 3, 7, 4, 9, 6]j = 1, key = 0, condition = 2 > 0
1773[1, 2, 2, 5, 8, 3, 7, 4, 9, 6]
1972[1, 2, 2, 5, 8, 3, 7, 4, 9, 6]j = 0, key = 0, condition = 1 > 0
2073[1, 1, 2, 5, 8, 3, 7, 4, 9, 6]
2276[0, 1, 2, 5, 8, 3, 7, 4, 9, 6]
3472[0, 1, 2, 5, 8, 3, 7, 4, 9, 6]j = 4, key = 3, condition = 8 > 3
3573[0, 1, 2, 5, 8, 8, 7, 4, 9, 6]
3772[0, 1, 2, 5, 8, 8, 7, 4, 9, 6]j = 3, key = 3, condition = 5 > 3
3873[0, 1, 2, 5, 5, 8, 7, 4, 9, 6]
4076[0, 1, 2, 3, 5, 8, 7, 4, 9, 6]
4472[0, 1, 2, 3, 5, 8, 7, 4, 9, 6]j = 5, key = 7, condition = 8 > 7
4573[0, 1, 2, 3, 5, 8, 8, 4, 9, 6]
4776[0, 1, 2, 3, 5, 7, 8, 4, 9, 6]
5172[0, 1, 2, 3, 5, 7, 8, 4, 9, 6]j = 6, key = 4, condition = 8 > 4
5273[0, 1, 2, 3, 5, 7, 8, 8, 9, 6]
5472[0, 1, 2, 3, 5, 7, 8, 8, 9, 6]j = 5, key = 4, condition = 7 > 4
5573[0, 1, 2, 3, 5, 7, 7, 8, 9, 6]
5772[0, 1, 2, 3, 5, 7, 7, 8, 9, 6]j = 4, key = 4, condition = 5 > 4
5873[0, 1, 2, 3, 5, 5, 7, 8, 9, 6]
6076[0, 1, 2, 3, 4, 5, 7, 8, 9, 6]
6872[0, 1, 2, 3, 4, 5, 7, 8, 9, 6]j = 8, key = 6, condition = 9 > 6
6973[0, 1, 2, 3, 4, 5, 7, 8, 9, 9]
7172[0, 1, 2, 3, 4, 5, 7, 8, 9, 9]j = 7, key = 6, condition = 8 > 6
7273[0, 1, 2, 3, 4, 5, 7, 8, 8, 9]
7472[0, 1, 2, 3, 4, 5, 7, 8, 8, 9]j = 6, key = 6, condition = 7 > 6
7573[0, 1, 2, 3, 4, 5, 7, 7, 8, 9]
7776[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
804[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]return value = [0,1,2,3,4,5,6,7,8,9]
Frequently Asked Questions
About IntroSort (Introspective Sort)

What is IntroSort?

IntroSort (Introspective Sort) is a hybrid sorting algorithm that provides both fast average performance and an optimal O(n log n) worst-case performance. It starts with Quick Sort but switches to Heap Sort when the recursion depth exceeds a certain level, and finally switches to Insertion Sort for very small partitions.

Why does IntroSort switch algorithms?

It switches algorithms to combine their strengths and mitigate their weaknesses. It uses Quick Sort for its excellent average-case speed. It switches to Heap Sort to avoid Quick Sort's O(n^2) worst-case scenario. It uses Insertion Sort for small partitions because its low overhead makes it faster for tiny arrays.

What is the time complexity of IntroSort?

The time complexity is O(n log n) in all cases (best, average, and worst). This makes it a very reliable and high-performance general-purpose sorting algorithm, and it is the default sorting algorithm in many C++ standard libraries.

Is IntroSort stable?

No, because its primary component, Quick Sort, and its fallback, Heap Sort, are not stable algorithms, IntroSort is also not stable. It does not preserve the relative order of equal elements.

What triggers the switch to Heap Sort?

The switch to Heap Sort is triggered when the recursion depth of Quick Sort reaches a certain limit. This limit is typically set to 2 * log2(n). This 'introspection' prevents the Quick Sort part of the algorithm from degrading to its O(n^2) worst-case performance on pathological inputs.

What triggers the switch to Insertion Sort?

IntroSort switches to Insertion Sort when the size of the partition to be sorted becomes very small (e.g., 16 elements or fewer). For such small arrays, the overhead of recursion in Quick Sort or Heap Sort is higher than the straightforward loops of Insertion Sort.

What is the space complexity of IntroSort?

The space complexity is O(log n) due to the recursion stack depth required by Quick Sort and Heap Sort.

How does IntroSort compare to Tim Sort?

Both are advanced hybrid sorts. IntroSort is generally faster on average for random data but is not stable. Tim Sort is stable and is exceptionally fast on data with pre-existing sorted runs, which is common in real-world scenarios. The choice depends on whether stability is required and the expected nature of the data.

Is IntroSort an in-place sort?

Yes, it is considered an in-place sorting algorithm as it does not require auxiliary storage proportional to the input size, only the O(log n) space for the recursion stack.

Why is it called 'Introspective' Sort?

It is named 'Introspective' because the algorithm 'watches' its own performance during the Quick Sort phase. If the recursion becomes too deep (an indicator of a potential worst-case scenario), it reflects on this and switches its strategy to Heap Sort to guarantee O(n log n) performance.

Where can I learn more?

You can find a more detailed explanation on GeeksforGeeks.