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

MetaTrader Experts, Indicators, Scripts and Libraries
//+------------------------------------------------------------------+  //|                                                    GnomeSort.mq5 |  //|                                    2019-2021, dimitri pecheritsa |  //|                                         mql5.com/en/users/dmipec |  //|------------------------------------------------------------------|  //|  c  | gnome sort                                                 |  //|------------------------------------------------------------------|  //| use | array sorting algorithm                                    |  //|  best: n; average: n^2; worst: n^2                               |  //|  memory: 1; stable: yes; method: exchanging                      |  //|  note: tiny code size                                            |  //|  originally proposed by iranian computer scientist hamid         |  //|sarbazi-azad (professor of computer science and engineering at    |  //|sharif university of technology) in 2000. the sort was first      |  //|called stupid sort (not to be confused with bogosort), and then   |  //|later described by dick grune and named gnome sort.               |  //|  the gnome sort is a sorting algorithm which is similar to       |  //|insertion sort in that it works with one item at a time but gets  |  //|the item to the proper place by a series of swaps, similar to a   |  //|bubble sort. it is conceptually simple, requiring no nested loops.|  //|the average running time is o(n^2) but tends towards o(n) if the  |  //|list is initially almost sorted.                                  |  //|  the algorithm finds the first place where two adjacent elements |  //|are in the wrong order and swaps them. it takes advantage of the  |  //|fact that performing a swap can introduce a new out-of-order      |  //|adjacent pair next to the previously swapped elements. it does not|  //|assume that elements forward of the current position are sorted,  |  //|so it only needs to check the position directly previous to the   |  //|swapped elements.                                                 |  //|------------------------------------------------------------------|  //|  f  | script program start function                              |  //|------------------------------------------------------------------|  //| use | gnome sort example. sort market watch symbols by spread    |  //+------------------------------------------------------------------+  #include <Mqh\Algorithms\SortGnome\GnomeSort.mqh>  #include <Mqh\Algorithms\SortGnome\Functions.mqh>  void OnStart(void)    {  //--- load symbols from the warket watch window     string symbols[];     SymbolsLoad(symbols);  //--- load spreads of the symbols      int keys[];     SymbolKeysSpread(symbols,keys);  //--- sort symbols by spread in accending order      ArraySort(keys,symbols,new CGnomeSort<int,string>);  //--- print a table of symbols to check result     SymbolsPrint(symbols);    }  //--------------------------------------------------------------------  //               symbol |   spread |    point  //--------------------------------------------------------------------  //                BRENT |        6 |     0.01  //               EURUSD |       16 |    1E-05  //            .US30Cash |       16 |      0.1  //                 TSLA |       47 |     0.01  //               XAUUSD |      300 |    0.001  //               USDRUB |      672 |   0.0001  //               BTCUSD |    13833 |     0.01  //--------------------------------------------------------------------  
33356