Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

introsort - Array sorting algorithm - MetaTrader 5 Library - MT4/MT5 Resources

author EAcpu | 3 reads | 0 comments |
//+------------------------------------------------------------------+
//| Introsort.mq5 |
//| 2019-2021, Dimitri Pecherica|
//| mql5.com/en/users/dmipec | mql5.com/en/users/dmipec |
//|------------------------------------------------------------------|
//| c|Array sorting algorithm |
//| introsort or introspective sort is a hybrid sorting algorithm |
//|Provides both fast average performance and (asymptotically) |
//|Best performance in worst case scenario. It starts with quicksort, which |
//|Switch to heap sort when the recursion depth exceeds a certain level |
//|Based on (the logarithm of) the number of elements being sorted |
//|When the number of elements is |, it switches to insertion sort
//|Below a certain threshold. This combines the best of all three |
//| algorithm, practical performance comparable to quick sort on |
//|Typical data set and worst case o(n log n) running time due to |
//|Heap sort. Because the three algorithms it uses are comparison |
//|Sort is also a comparison sort.                              |
//| introsort was invented in 1997 by david musser, who also worked on it |
//|Introduces introselect, a hybrid selection algorithm based on |
//|quickselect (variant of quicksort), fall back to median|
//|The median, thus providing the worst-case linear complexity, which |
//| is the best. The purpose of introducing these two algorithms is |
//|Provides general algorithms for the C++ standard library
//|Have both fast average performance and best worst case performance |
//|Performance, so that the performance requirement is |
//|Tighten. introsort is in place, but unstable |
//| The most popular c++ stl algorithm sort() uses introsort.      |
//|------------------------------------------------------------------|
//| SCR|Internal sorting example |
//+------------------------------------------------------------------+
#include #include blank start ()
  {
Integer a[] = { 3 , 1 , 23 , - 9 , 233 , 23 , - 313 , 32 , - 9 };
ArraySort (a, new CIntroSort< integer , integer >,SORT_ORDER_ACCENDING);
arrayprint (a);
  } //+------------------------------------------------------------------+
//| >>>| -313 -9 -9 1 3 23 23 32 233 |
//+------------------------------------------------------------------+



Attachment download

📎 introsort.mq5 (0.96 KB)

📎 introsort.mqh (26.5 KB)

📎 functions.mqh (1.94 KB)

📎 asorter.mqh (2.18 KB)

📎 comparefunction.mqh (10.95 KB)

Source: MQL5 #33475

Verification code Refresh