Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

Heap Sort - Array Sorting Algorithm - MetaTrader 5 Library | MT5 EA Download - MetaTrader 5 Resources

author blogger | 551 reads | 0 comments |
//+------------------------------------------------------------------+
//| Heap sort.mq5 |
//| 2019 - 2021, Dimitri Pecherica|
//| https://www.mql5.com/en/users/dmipec |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|Heap sort - array sorting algorithm |
//| Best - n log n, average - n log n, worst - n log n |
//| Memory - 1, Stable - No, Method - Select |
//| Heap sort is a more efficient version of selection sort. it |
//| also works by determining the largest (or smallest) element of |
//|List, put it at the end (or beginning) of the list, |
//|Then continue with the rest of the list, but complete this operation|
//|To perform tasks efficiently by using a data structure called a heap, a |
//|Special type of binary tree. After the data list is created |
//|Put it into the heap, the root node is guaranteed to be the largest (or |
//|Minimum) element. When it is removed and placed at the end of |
//|List, heap is rearranged so the largest element remaining|
//|Move to the root. Using the heap, find the next largest |
//|element takes o(log n) time instead of linear scan o(n) |
//|Simple selection sorting. This allows heap sorting in O(n log |
//|n) time, which is also the worst-case complexity.              |
//| although in practice slower than | on most machines
//|Achieve good quick sort, its advantage is more |
//|Favorable worst-case o(n log n) running time. Heap sort is an in-place sort
//|Algorithm, but it is not a stable sort.                          |
//| Heap sort was invented by j. w. j. Williams 1964. This is |
//| is also the birth of the heap, which Williams has described as |
//|It is a useful data structure in itself. In the same year, r.w.  |
//|floyd released an improved version that can sort arrays |
//|In situ, continuing his early research on tree sorting |
//|Algorithm.                                                        |
//| Heap sort mainly competes with quick sort, the other one is very |
//|Efficient and versatile comparison-based near-in-place sorting|
//|Algorithm.                                                        |
//+------------------------------------------------------------------+
//|Compare with other algorithms |
//| Quick sort is usually faster due to some factors, but |
//|The worst-case running time of quick sort is o(n2), that is, |
//|Unacceptable for large data sets, can be intentional |
//|Triggered with sufficient implementation knowledge, create |
//|Security risk.                                                  |
//| Therefore, due to the o(n log n) upper bound of heap sort | |
//|The running time and constant upper limit of its auxiliary storage, |
//|Embedded or related systems with real-time constraints|
//|Heap sorting is often used for security purposes, such as the Linux kernel.      |
//| Heap sort also competes with merge sort, which has the same time |
//|Boundary. Merge sort requires Ω(n) auxiliary space, but heap sort |
//|Only constant quantities are required. Heap sort usually runs faster
//|In practice on machines with small or slow data caches, and indeed
//|No need for so much external memory. On the other hand, Merge |
//|Sort has several advantages over heap sort: |
//| Merge sort on arrays with better data caching |
//|Performance, often better than heap sort on modern desktops |
//|Computer, because merge sort frequently accesses consecutive |
//|Memory location (good reference location); heap sort |
//|References are distributed throughout the heap.                        |
//| Heap sort is not a stable sort; merge sort is stable.          |
//| The parallelism of merge sort is very good and can achieve close to linear effect |
//|Speed ​​up by simple implementation; heap sort is not an obvious |
//|Candidates for parallel algorithms.                              |
//| Merge sort can be applied to singly linked lists
//|There are o(1) extra space. Heap sort can be applied to the operation |
//|Doubly linked list with only O(1) extra space overhead.          |
//| Merge sort is used for external sorting; heap sort is not.      |
//|Locality of references is the problem.                              |an> //| introsort is an alternative to heap sort that combines quicksort | //| and heap sort while retaining the best of both worlds: worst-case speed | //| average speed of heap sort and quick sort.                          | //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //|Script program startup function | //|Heap sort example | //| Sort symbols (items) by color (key). Colors in MQL are | //|integers. Similarly, items can be sorted by any other key, such as by | //|Number | //+------------------------------------------------------------------+ #include #include blank on start ( blank ) { //--- Load symbols from the terminal into array (item) string symbols[]; SymbolLoad(symbol); //--- Load the colors of the symbols into the keys array colorkeys []; Print the symbol table to check the results SymbolsPrint(symbol); } //-------------------------------------------------------------------------------- // Symbol | Color name ( id ) | Number //-------------------------------------------------------------------------------- // OGZD.L | clrGold ( 55295 ) | 3 // SGGD.L | clrGold (55295) | 3 // MGNT.L | clrGold (55295) | 3 // ATAD.L | clrGold (55295) | 3 // PLZL.L | clrGold (55295) | 3 // Five.L | clrGold (55295) | 3 // ROSN.L | clrGold (55295) | 3 // NVTK.L | clrGold (55295) | 3 // SBER.L | clrGold (55295) | 3 // MNOD.L | clrGold (55295) | 3 // SVST.L | clrGold (55295) | 3 // NLMK.L | clrGold (55295) | 3 // LKOD.L | clrGold (55295) | 3 // XAGUSD | clrkhaki(9234160) | 5 // XAUUSD | clrkhaki(9234160) | 3 // LTCUSD | clrMediumSpringGreen (10156544) | 2 // (10156544 ) | 2 // Crypto.ALT | clrMediumSpringGreen (10156544) | 2 // ETHEUR | clrMediumSpringGreen (10156544) | 2 // Crypto.Top | clrMediumSpringGreen (10156544) | 2 // EOSUSD | clrMediumSpringGreen (10156544) | 3 // LTCBTC | clrMediumSpringGreen (10156544) | 5 // BTCUSD | clrMediumSpringGreen (10156544) | 2 // 2n class="comment">// DSHUSD | clrMediumSpringGreen (10156544) | 2 // ADBE | clrPaleGoldenrod (11200750) | 2 // PM | clrPaleGoldenrod (11200750) | 2 // MMM | clrPaleGoldenrod (11200750) | 2 // EA | clrPaleGoldenrod (11200750) | 2 // HPE | clrPaleGoldenrod (11200750) | 2 // VZ | clrPaleGoldenrod (11200750) | 2 // MSFT | clrPaleGoldenrod (11200750) | 2 // PFE | clrPaleGoldenrod (11200750) | 2 // GOOGL | clrPaleGoldenrod (11200750) | 2 // JNJ | clrPaleGoldenrod (11200750) | 2 // Bachelor | clrPaleGoldenrod (11200750) | 2 // Cat | clrPaleGoldenrod (11200750) | 2 // NKE | clrPaleGoldenrod (11200750) | 2 // PG | clrPaleGoldenrod (11200750) | 2 // PEP | clrPaleGoldenrod (11200750) | 2 // KO | clrPaleGoldenrod (11200750) | 2 // General Manager | clrPaleGoldenrod (11200750) | 2 // GE | clrPaleGoldenrod (11200750) | 2 // Apple | clrPaleGoldenrod (11200750) | 2 // PYPL | clrPaleGoldenrod (11200750) | 2 // LLY | clrPaleGoldenrod (11200750) | 2 // FB | clrPaleGoldenrod (11200750) | 2 // BRK.B | clrPaleGoldenrod (11200750) | 2 // IBM | clrPaleGoldenrod (11200750) | 2 // V | clrPaleGoldenrod (11200750) | 2 // INTC | clrPaleGoldenrod (11200750) | 2 // WFC | clrPaleGoldenrod (11200750) | 2 // C | clrPaleGoldenrod (11200750) | 2 // PRU | clrPaleGoldenrod (11200750) | 2 // ATVI | clrPaleGoldenrod (11200750) | 2 // GS | clrPaleGoldenrod (11200750) | 2 // JP Morgan | clrPaleGoldenrod (11200750) | 2 // NEM | clrPaleGoldenrod (11200750) | 2 // Tesla | clrPaleGoldenrod (11200750) | 2 // CVX | clrPaleGoldenrod (11200750) | 2 // DAL | clrPaleGoldenrod (11200750) | 2 // WMT | clrPaleGoldenrod (11200750) | 2 // eBay | clrPaleGoldenrod (11200750) | 2 // SBUX | clrPaleGoldenrod (11200750) | 2 // MCD | clrPaleGoldenrod (11200750) | 2 // NFLX | clrPaleGoldenrod (11200750) | 2 // UPS | clrPaleGoldenrod (11200750) | 2 // Amazon | clrPaleGoldenrod (11200750) | 2 // FOXA | clrPaleGoldenrod (11200750) | 2 // NVDA | clrPaleGoldenrod (11200750) | 2 // XOM | clrPaleGoldenrod (11200750) | 2 // DIS | clrPaleGoldenrod (11200750) | 2> // CMCSA | clrPaleGoldenrod (11200750) | 2 // CSCO | clrPaleGoldenrod (11200750) | 2 // BAC | clrPaleGoldenrod (11200750) | 2 // ORCL | clrPaleGoldenrod (11200750) | 2 // .JP225Cash | clrPink (13353215) | 1 // WTI | clrPink (13353215) | 2 // Brent | clrPink (13353215) | 2 // .USTECHCash | clrPink (13353215 ) | 1 // .US30Cash | clrPink (13353215) | 1 // .US500Cash | clrPink (13353215) | 1 // .DE30Cash | clrPink (13353215) | 1 // GBXUSD | clr Mei(14524637) | 5 // USD/MXN | clr Honeydew (15794160) | 5 // USD Try | clr Honeydew(15794160 ) | 5 // USD/PLN | clr Honeydew(15794160) | 5 // EURUSD | clr Honeydew (15794160) | 5 // USDJPY | clr Honeydew(15794160) | 3 // USD/CHF | clr Honeydew(15794160) | 5 // EUR/NZD | clr Honeydew(15794160) | 5 // CADJPY | clr Honeydew(15794160) | 3 // USD Canadian Dollar | clr Honeydew (15794160) | 5 // NZD USD | clr Honeydew(15794160) | 5 // Euro Japanese Yen | clr Honeydew( 15794160) | 3 // NZDJPY | clr Honeydew(15794160) | 3 // NZDCHF | clr Honeydew(15794160) | 5 // EURUSD | clr Honeydew(15794160) | 5 // CADCHF | clr Honeydew(15794160) | 5 // AUDJPY| clr Honeydew(15794160) | 3 // NZDCAD | clr Honeydew(15794160) | 5 // GBPCHF | clr Honeydew(15794160) | 5 // Euro | clr Honeydew(15794160) | 5 // GBP/USD | clr Honeydew(15794160) | 5 // GBPJPY| clr Honeydew(15794160) | 3 // Euro Canadian Dollar | clr Honeydew(15794160) | 5 // AUD/USD | clr Honeydew(15794160) | 5 // British Pound New Zealand Dollar | clr Honeydew(15794160) | 5 // British Pound Canadian Dollar | clr Honeydew(15794160) | 5 // Euro| clr Honeydew (15794160) | 5 // USD offshore RMB | clr Honeydew (15794160) | 5 // British pound to Australian dollar | clr Honeydew (15794160) | 5 // USD to Russian ruble | clr Honeydew(15794160) | 4 // USD ZAR | clr Honeydew(15794160) | 5 // EURPLN | clr Honeydew(15794160) | 5 // Swiss Franc Japanese Yen | clr Honeydew(15794160) | 3 // AUD/NZD | clr Honeydew(15794160) | 5 // AUDCHF | clr Honeydew (15794160) | 5 // AUDCAD | clr Honeydew(15794160) | 5 //+------------------------------------------------------------------+



Attachment download

📎 heapsort.mq5 (9.41 KB)

📎 heapsort.mqh (19.87 KB)

📎 functions.mqh (4.25 KB)

📎 asorter.mqh (2.79 KB)

📎 comparefunction.mqh (6.39 KB)

Source: MQL5 #32775

Refresh