Skip to content
All library documents

Merge Sort: Stable Sorting with O(n log n) Time

Article MQL5 code base

Summary

The document explains merge sort, a comparison-based sorting method that repeatedly combines smaller sorted groups into larger ones. It describes the algorithm as stable, meaning equal items retain their relative order, and gives best, average, and worst case running times of n log n. The method uses sequential access, so it can work with lists as well as arrays.

A short MetaTrader example sorts open position tickets in ascending order before displaying them. The explanation also notes the algorithm’s linear extra memory requirement and the copying involved in simple implementations. The example illustrates sorting records by ticket number; it does not present a trading signal, performance test, or evidence about market outcomes. The material is useful as a data handling concept for trading software, but it does not discuss how sorted positions affect execution or investment decisions.

Key ideas

  • Merge sort builds a sorted sequence by repeatedly merging smaller sorted groups.
  • The method has n log n running time in its best, average, and worst cases.
  • A stable implementation preserves the order of equal elements.
  • The algorithm needs linear additional memory and may make many copies.
  • The example sorts position tickets before printing their associated market data.

Tags

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