First see 2 algorithms
//+------------------------------------------------------------------+
//| BehchQuickSort.mq4 |
//| Copyright © 2005, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
//+------------------------------------------------------------------+
//| быстрая сортировка double |
//+------------------------------------------------------------------+
void QuickSort(double& item[],int left,int right)
{
int i, j;
int center;
double x;
//----
i=left;
j=right;
center=item[(left+right)/2];
while(i<=j)
{
while(item[i]<center && i<right) i++;
while(item[j]>center && j>left) j--;
if(i<=j)
{
x=item[i];
item[i]=item[j];
item[j]=x;
i++; j--;
}
}
if(left<j) QuickSort(item, left, j);
if(right>i) QuickSort(item, i, right);
}
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
int start()
{
int file;
double data[];
int length=0;
int start,total;
int min,sec,hsec,t=0;
//----
Print("QuickSort test.\nLoading data...!");
file=FileOpen("array_int.dat",FILE_BIN|FILE_READ);
if(file<0)
{
Print("file array_int.dat not found.\n");
return(-1);
}
//----
length=FileSize(file)/8;
ArrayResize(data,length);
FileReadArray(file,data,0,length);
FileClose(file);
//---- start sorting
start=GetTickCount();
// qsort(data, length,sizeof(int),cmp);
// QuickSort<int>(data,0,length-1);
// ArraySort(data);
QuickSort(data,0,length-1);
total=GetTickCount()-start;
//---- human presentation
min=total/60000;
t=total%60000;
sec=t/1000;
hsec=t%1000;
//---- outpit results
Print("Sorting int array[",length,"] time is ",min," min ",sec," sec ",hsec," msec");
//----
return(0);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| BenchBubbleSort.mq4 |
//| Copyright © 2005, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
int start()
{
StartRandom();
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int StartRandom()
{
double data[];
int length=20000;
int start,total;
int min,sec,hsec,t=0;
//----
Print("Loading data...!");
//----
ArrayResize(data,length);
for(int i=length-1;i>=0;i--) data[i]=i;
//---- start sorting
Print("Start sorting...!");
start=GetTickCount();
// ArraySort(data);
BubbleSort(data,length);
total=GetTickCount()-start;
//---- human presentation
min=total/60000;
t=total%60000;
sec=t/1000;
hsec=t%1000;
//---- outpit results
Print("Sorting double array[",length,"] time is ",min," min ",sec," sec ",hsec," msec");
//----
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int StartSorting()
{
int file;
double data[];
int length=0;
int start,total;
int min,sec,hsec,t=0;
//----
Print("QuickSort test.\nLoading data...!");
file=FileOpen("array_int.dat",FILE_BIN|FILE_READ);
if(file<0)
{
Print("file array_int.dat not found.\n");
return(-1);
}
//----
length=FileSize(file)/4;
ArrayResize(data,length);
FileReadArray(file,data,0,length);
FileClose(file);
//---- start sorting
start=GetTickCount();
BubbleSort(data,length);
total=GetTickCount()-start;
//---- human presentation
min=total/60000;
t=total%60000;
sec=t/1000;
hsec=t%1000;
//---- outpit results
Print("Sorting int array[",length,"] time is ",min," min ",sec," sec ",hsec," msec");
//----
return(0);
}
//+------------------------------------------------------------------+
//| Bubble sort |
//+------------------------------------------------------------------+
void BubbleSort(double& data[],int count)
{
int i, j;
double x;
//----
for(i=1;i<count; i++)
for(j=count-1;j>=i;j--)
if(data[j-1]>data[j])
{
x=data[j];
data[j]=data[j - 1];
data[j-1]=x;
}
//----
}
//+------------------------------------------------------------------+
Change sources as follow
void BubbleSort(double& data[][2],int count)
{
int i, j;
double x;
//----
for(i=1;i<count; i++)
for(j=count-1;j>=i;j--)
if(data[j-1][1]>data[j][1])
{
x=data[j][0];
data[j][0]=data[j - 1][0];
data[j-1][0]=x;
x=data[j][1];
data[j][1]=data[j - 1][1];
data[j-1][1]=x;
}
//----
}
See also: http://www.cubesteak.net/2006/08/more-array-functions-for-mt4/
I think it is similar, but may be useful.
Cheers,
CS
I think it is similar, but may be useful.
Cheers,
CS
Hello all,
Can a 2 dimention array be sorted by the value in second dimention? If so, can someone provide an example?
Thank you,
-charliev
Can a 2 dimention array be sorted by the value in second dimention? If so, can someone provide an example?
Thank you,
-charliev
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Can a 2 dimention array be sorted by the value in second dimention? If so, can someone provide an example?
Thank you,
-charliev