//+------------------------------------------------------------------+
//| 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
//--------------------------------------------------------------------
#33356LibrariesMetaTrader 5👁 5067 views
gnome sort - array sorting algorithm
D
Author
DMITRII PECHERITSAPublished date
Feb 3, 2021
📚Related Articles
← View other articles →#77531Libraries
CSetupLedger: a per-setup win rate ledger for Expert Advisors
Why I wrote it My scalper trades two setups. A bullish order block and the bearish mirror of it. The Strategy Tester gave me one profit factor for both of them. I spent two weeks t...
Ali AkbarView detail →
#77488Libraries
Position sizing from risk, using the broker's own tick value and volume step
Every lot size calculator that assumes one pip is worth ten dollars is wrong on gold, wrong on indices, wrong on JPY crosses and wrong on any account not denominated in USD which i...
Ralph Ivan SimeonView detail →
#77156Libraries
PropFirm Defense
A prop firm account runs under a second rulebook MetaTrader 5 knows nothing about: a maximum loss measured from a fixed starting balance, a daily limit that resets at an hour the b...
Bruno Nunes Myrrha RibeiroView detail →