MQLArticles/Utils/FA/Sort.mqh

131 lines
3.9 KiB
MQL5
Raw Permalink Normal View History

2025-09-22 09:08:13 -05:00
//+------------------------------------------------------------------+
//| Sort.mqh |
//| Copyright 2025, Niquel Mendoza. |
//| https://www.mql5.com/es/users/nique_372/news |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, Niquel Mendoza."
#property link "https://www.mql5.com/es/users/nique_372/news"
#property strict
2025-09-27 10:30:20 -05:00
//+------------------------------------------------------------------+
//| |
2025-09-22 09:08:13 -05:00
//+------------------------------------------------------------------+
template <typename S, typename CompareFuncionMayor>
void SortArrayDescendente(S* &array[], int left, int right, CompareFuncionMayor mayor, MqlParam &params[])
{
if(left >= right)
return;
const int pivotIndex = (left + right) >> 1;
2025-09-27 10:30:20 -05:00
const S* pivotValue = array[pivotIndex];
2025-09-22 09:08:13 -05:00
int i = left, j = right;
while(i <= j)
{
while(mayor(params, array[i], pivotValue)) // array[i] > es mayor pivot
i++;
while(mayor(params, pivotValue, array[j]))
j--;
if(i <= j)
{
2025-09-27 10:30:20 -05:00
S* temp = array[i];
2025-09-22 09:08:13 -05:00
array[i] = array[j];
array[j] = temp;
i++;
j--;
}
}
SortArrayDescendente(array, left, j, mayor, params);
SortArrayDescendente(array, i, right, mayor, params);
}
//+------------------------------------------------------------------+
template <typename S, typename CompareFuncionMayor>
void SortArrayAscendente(S* &array[], int left, int right, CompareFuncionMayor mayor, MqlParam &params[])
{
if(left >= right)
return;
const int pivotIndex = (left + right) >> 1;
2025-09-27 10:30:20 -05:00
const S* pivotValue = array[pivotIndex];
2025-09-22 09:08:13 -05:00
int i = left, j = right;
while(i <= j)
{
while(mayor(params, pivotValue, array[i]))
i++;
while(mayor(params, array[j], pivotValue))
j--;
if(i <= j)
{
2025-09-27 10:30:20 -05:00
S* temp = array[i];
2025-09-22 09:08:13 -05:00
array[i] = array[j];
array[j] = temp;
i++;
j--;
}
}
SortArrayAscendente(array, left, j, mayor, params);
SortArrayAscendente(array, i, right, mayor, params);
}
2025-09-27 10:30:20 -05:00
//+------------------------------------------------------------------+
//| |
2025-09-22 09:08:13 -05:00
//+------------------------------------------------------------------+
template <typename S, typename CompareFuncionMayor>
void SortArrayDescendente(S &array[], int left, int right, CompareFuncionMayor mayor, MqlParam &params[])
{
if(left >= right)
return;
const int pivotIndex = (left + right) >> 1;
const S pivotValue = array[pivotIndex];
int i = left, j = right;
while(i <= j)
{
while(mayor(params, array[i], pivotValue)) // array[i] > es mayor pivot
i++;
while(mayor(params, pivotValue, array[j]))
j--;
if(i <= j)
{
S temp = array[i];
array[i] = array[j];
array[j] = temp;
i++;
j--;
}
}
SortArrayDescendente(array, left, j, mayor, params);
SortArrayDescendente(array, i, right, mayor, params);
}
//+------------------------------------------------------------------+
template <typename S, typename CompareFuncionMayor>
void SortArrayAscendente(S &array[], int left, int right, CompareFuncionMayor mayor, MqlParam &params[])
{
if(left >= right)
return;
const int pivotIndex = (left + right) >> 1;
const S pivotValue = array[pivotIndex];
int i = left, j = right;
while(i <= j)
{
while(mayor(params, pivotValue, array[i]))
i++;
while(mayor(params, array[j], pivotValue))
j--;
if(i <= j)
{
S temp = array[i];
array[i] = array[j];
array[j] = temp;
i++;
j--;
}
}
SortArrayAscendente(array, left, j, mayor, params);
SortArrayAscendente(array, i, right, mayor, params);
}
//+------------------------------------------------------------------+