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 | selection sort - array sorting algorithm

MetaTrader Experts, Indicators, Scripts and Libraries
//+------------------------------------------------------------------+  //|                                                SelectionSort.mq5 |  //|                                    2019-2021, dimitri pecheritsa |  //|                                         mql5.com/en/users/dmipec |  //+------------------------------------------------------------------+  //| selection sort - array sorting algorithm                         |  //+------------------------------------------------------------------+  //|  best: n^2, average: n^2, worst: n^2                             |  //|  memory: 1, stable: no, method: selection                        |  //|  note: stable with o(n) extra space or when using linked lists.  |  //+------------------------------------------------------------------+  //|  in computer science, selection sort is an in-place comparison   |  //|sorting algorithm. it has an o(n2) time complexity, which makes   |  //|it inefficient on large lists, and generally performs worse than  |  //|the similar insertion sort. selection sort is noted for its       |  //|simplicity and has performance advantages over more complicated   |  //|algorithms in certain situations, particularly where auxiliary    |  //|memory is limited.                                                |  //|  the time efficiency of selection sort is quadratic, so there    |  //|are a number of sorting techniques which have better time         |  //|complexity than selection sort. one thing which distinguishes     |  //|selection sort from other sorting algorithms is that it makes the |  //|minimum possible number of swaps, n − 1 in the worst case.        |  //+------------------------------------------------------------------+  //+------------------------------------------------------------------+  //| script program start function                                    |  //|  [use] a selection sort example. sort deals by symbol            |  //+------------------------------------------------------------------+  #include <Mqh\Algorithms\SelectionSort\SelectionSort.mqh>  #include <Mqh\Algorithms\SelectionSort\Functions.mqh>  void OnStart(void)    {  //--- load deals from the terminal starting from the beginning of   //2021 till current day. they will be treated as the items array by   //the sorter     ulong deals[];     DealsLoad(deals,D'2021.01.01',0);  //--- create the keys array which contains the symbols of each deal   //from the deals array, which is the basis for sorting     string symbols[];     DealsKeySymbol(deals,symbols);  //--- sort deals by symbols in descending order with the selection   //sort algorithm     ArraySort(symbols,deals,new CSelectionSort<string,ulong>,false);  //--- check the result of sorting by printing a table of symbols.   //your table will look different     DealsPrint(deals);    }  //--------------------------------------------------------------------  //                          deal | symbol  //--------------------------------------------------------------------  //                     170477949 | XAUUSD  //                     170764903 | XAUUSD  //                     170764902 | XAUUSD  //                     170252156 | XAUUSD  //                     170541532 | XAUUSD  //                     172313700 | BTCUSD  //                     172313699 | BTCUSD  //                     172313666 | BTCUSD  //                     172313530 | BTCUSD  //                     172313512 | BTCUSD  //                     172313511 | BTCUSD  //                     172313502 | BTCUSD  //                     172313501 | BTCUSD  //                     172313500 | BTCUSD  //                     172313493 | BTCUSD  //                     172313490 | BTCUSD  //                     172313488 | BTCUSD  //                     172313474 | BTCUSD  //                     172313453 | BTCUSD  //                     172313412 | BTCUSD  //                     171147845 | BTCUSD  //                     171145409 | BTCUSD  //                     171145256 | BTCUSD  //                     171145029 | BTCUSD  //                     171011667 | BTCUSD  //                     170983807 | BTCUSD  //                     170720576 | BTCUSD  //                     170429897 | BTCUSD  //                     169998112 | BTCUSD  //                     169998099 | BTCUSD  //                     169990154 | BTCUSD  //--------------------------------------------------------------------  
33104