//+------------------------------------------------------------------+
//| 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
// ...
//
//+------------------------------------------------------------------+
#32608LibrariesMetaTrader 5👁 5821 views
quick sort - sorting algorithm
D
Author
DMITRII PECHERITSAPublished date
Dec 23, 2020
📚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 →