Skip to content
All library documents

Gnome Sort for Ordering Trading Symbols by Spread

Article MQL5 code base

Summary

The document explains gnome sort as a simple, stable array-sorting method that orders items through adjacent comparisons and swaps. It resembles insertion sort in processing one element at a time, while using swaps to move out-of-order items into place. The stated complexity is linear in the best case and quadratic on average and in the worst case; performance tends toward linear when the input is nearly sorted. It uses constant extra memory.

The MQL5 example loads symbols from the market watch, obtains their spreads as sort keys, and prints the symbols in ascending spread order. The sample output illustrates ordering instruments from relatively narrow to wide spreads, including currencies, an index, a commodity, a stock, and a cryptocurrency. This is a programming example for organizing market data, not a trading strategy or evidence that spread ranking alone improves execution. Its usefulness depends on having accurate, comparable spread values and choosing a sorting method appropriate to the list size.

Key ideas

  • Gnome sort orders items by repeatedly checking and swapping adjacent elements that are out of order.\nThe algorithm is stable and uses constant extra memory.\nIts best-case runtime is linear, while average and worst-case runtimes are quadratic.\nThe MQL5 example sorts market-watch symbols by spread in ascending order.\nSpread ranking is a data-handling example and does not establish a trading edge.

Tags

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