Loading…

Array Sorting Algorithms

The built-in ArraySort() sorts a double array in place, ascending only, on its own values. That covers "sort this price list" and nothing else. The moment you need "sort these tickets by their profit" or "sort these symbol names by their spread", the built-in has no way to carry a second array along - it only knows how to reorder the array you hand it, and a ticket number sorted by ticket number is not what anyone wants. ArgSortD is the answer: instead of sorting values, it returns the permutation that would sort them - an int array where indices[k] is the original position of whatever belongs in sorted position k. Nothing about the input is touched. ApplyPermutationD, ApplyPermutationI and ApplyPermutationS then replay that exact permutation onto any array of matching length - profits, tickets, symbol names, anything. Sort once, apply the same order everywhere. QuickSortD and InsertionSortD are included as ordinary in-place sorts for when there is no companion array at all. QuickSortD uses the middle element as its pivot rather than the first element, which matters here specifically because price and history arrays arrive already sorted by time far more often than random data does - a first-element pivot degrades to O(n^2) on exactly that input, and a middle pivot does not. InsertionSortD is O(n^2) by design and is the better choice below a few dozen elements, or on a rolling window that is already nearly sorted every time a new value arrives. ArgSortD's tie-breaking is stable - equal values keep their original relative order - which matters when the tie-break should mean something, such as "whichever position was opened first". BinarySearchD and IsSortedD round out the set for code that needs to query a sorted array rather than build one. Notes and limitations - QuickSortD is not stable - equal elements can be reordered relative to each other. Use InsertionSortD when stability matters and the array is small, or use ArgSortD (which is stable) and apply the permutation. - QuickSortRangeD is the recursive worker behind QuickSortD and is exposed as a public function; call QuickSortD for the normal case, it sets up the full-array bounds correctly. - BinarySearchD's precondition - ascending sorted input - is not checked. A linear scan to verify sortedness first would remove the entire performance reason to use binary search in the first place. - ArgSortD and InsertionSortD are both O(n^2) internally on the index bookkeeping; for very large arrays prefer QuickSortD directly when no companion array is needed.

Script · MT5 · v1.0 · Free

About Array Sorting Algorithms

Array Sorting Algorithms is a script for MetaTrader 5. MQL source code included.

Files in the download: ArraySort.mq5.mqh, ArraySort.txt, ArraySortDemo.ex5, ArraySortDemo.mq5.

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria