Expert Advisors • Indicators • Scripts • Libraries

MQL.RobotFX.org is the biggest collection of MetaTrader expert advisors (MT5 & MT4), indicators, scripts and libraries that can be used to improve trading results, minimize risks or simply automate trading tasks

MetaTrader 5 Libraries | quick sort - sorting algorithm

MetaTrader Experts, Indicators, Scripts and Libraries
//+------------------------------------------------------------------+  //|                                                    QuickSort.mq5 |  //|                                    2019-2020, dimitri pecheritsa |  //|                                                 792112@gmail.com |  //+------------------------------------------------------------------+  //  //   quick sort - sorting algorithm  //  //   quick sort is a highly efficient sorting algorithm and is based  //on partitioning of array of data into smaller arrays  //   a large array is partitioned into two arrays one of which holds  //values smaller than the specified value say pivot, based on which  //the partition is made and another array holds values greater than  //the pivot value  //   quick sort partitions an array and then calls itself recursively  //twice to sort the two resulting subarrays  //   this algorithm is quite efficient for large-sized data sets as  //its average and worst-case complexity are O(nlogn) and theta(n^2),  //respectively  //  //   quick sort pivot algorithm  //  //   step 1: choose the highest index value has pivot  //   step 2: take two variables to point left and right of the values  //excluding pivot  //   step 3: left points to the low index  //   step 4: right points to the high  //   step 5: while value at left is less than pivot move right  //   step 6: while value at right is greater than pivot move left  //   step 7: if both step 5 and step 6 does not match swap left and  //right  //   step 8: if left ≥ right, the point where they met is new pivot  //  //   quick sort algorithm  //  //   using pivot algorithm recursively, we end up with smaller  //possible partitions  //   each partition is then processed for quick sort  //   step 1: make the right-most index value pivot  //   step 2: partition the array using pivot value  //   step 3: quicksort left partition recursively  //   step 4: quicksort right partition recursively  //  //+------------------------------------------------------------------+  //| quick sort example - sort positions by open price in accending   |  //| or descending order                                              |  //+------------------------------------------------------------------+  #include <Mqh\Algorithms\QuickSort\QuickSort.mqh>  #include <Mqh\Algorithms\QuickSort\Functions.mqh>  void OnStart()    {  //---load tickets from terminal - the items to be sorted      string symbol=_Symbol;     ulong tickets[];     PositionsLoad(tickets,symbol);  //---create keys for sorting - open prices of the positions     double keys[];     PositionsKeysPriceOpen(tickets,keys);  //---sort positions by open price     CQuickSort<double,ulong> sorter;     bool accending_order=true; //false for descending     sorter.Sort(keys,tickets,accending_order);  //---print positions     int digits=(int)SymbolInfoInteger(symbol,SYMBOL_DIGITS);     PositionsPrint(tickets,digits);    }  //  //   example output  //  //    EURUSD | 195762889 |  1.16667  //    EURUSD | 195806889 |  1.16939  //    EURUSD | 195998318 |  1.17384  //    EURUSD | 197300364 |  1.17602  //    EURUSD | 197454338 |  1.17680  //    EURUSD | 197488771 |  1.17859  //    EURUSD | 196985503 |  1.17924  //    EURUSD | 196058650 |  1.17935  //    EURUSD | 197513038 |  1.18068  //    EURUSD | 196921898 |  1.18179  //    EURUSD | 196317657 |  1.18295  //    EURUSD | 198719928 |  1.18325  //    EURUSD | 198678762 |  1.18342  //    EURUSD | 198673400 |  1.18387  //    EURUSD | 197932787 |  1.18482  //   ...  //  //+------------------------------------------------------------------+  
32608