Skip to content
All library documents

Introsort Combines Quicksort, Heapsort, and Insertion Sort

Article MQL5 code base

Summary

Introsort is a comparison-based array sorting method designed to pair quicksort’s typical speed with a bound on worst-case runtime. It begins with quicksort and tracks recursion depth, switching to heapsort if that depth passes a limit based on the logarithm of the input size. For small partitions, it switches to insertion sort, which is effective on short sequences.

The document explains that this hybrid has average behavior comparable to quicksort on typical data while retaining O(n log n) worst-case performance through heapsort. It also notes that the algorithm sorts in place and is not stable, and identifies its use in the C++ standard library’s sort routine. The included example simply sorts an integer array and prints the ordered values; it is an illustration rather than a benchmark. The note offers no performance measurements or trading-specific application, so its relevance to quant work is limited to general algorithmic knowledge.

Key ideas

  • Introsort starts with quicksort and switches algorithms when recursion depth or partition size warrants it.
  • Heapsort provides a worst-case runtime bound of O(n log n).
  • Insertion sort handles small partitions efficiently.
  • The method is an in-place comparison sort and is not stable.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.