Skip to content
All library documents

Insertion Sort: A Stable, In-Place Method for Ordering Trading Data

Article MQL5 code base

Summary

This document explains insertion sort, a method that builds an ordered list one item at a time by inserting each new element into its proper position among those already processed. It highlights the algorithm’s stability, constant extra-memory use, and ability to handle incoming items incrementally. The discussion gives linear best-case behavior, quadratic average and worst-case behavior, and notes that performance improves when the input is already nearly sorted. It also describes the method as practical for small datasets, while less suitable for large unsorted collections than more advanced sorting methods.

A MetaTrader example applies the algorithm to trading records: it loads deals, creates keys from deal types, sorts the records using those keys, and displays the result. This demonstrates an implementation context rather than a trading strategy or market finding. The document offers no benchmark timings or comparison on actual workloads. Its complexity depends on input order, and the example’s choice of key means the output groups deals by type; it does not establish broader performance or trading benefits.

Key ideas

  • Insertion sort creates a sorted sequence by inserting each item into its appropriate place.
  • The method is stable, uses constant extra memory, and can process items incrementally.
  • Its best case is linear, while average and worst cases are quadratic.
  • The example sorts trading records by deal type, without presenting a performance benchmark.

Tags

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