Clearing an array of defined element(s) - page 10

 

Compare the 2 code variants on the same ToR:

//+------------------------------------------------------------------+
//|                                             Erase and Resize.mq5 |
//|                                                      Peter Konow |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Peter Konow"
#property link      "https://www.mql5.com"
#property version   "1.00"
//--------------------------------------------------------------------
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   int Arr[20] = {1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2};
   ulong q1 = GetMicrosecondCount(); 
   //--------------------------------
   int deleted = 0,q = 0;
   //-------------- 
   for(int a1 = 0; a1 < ArraySize(Arr); a1++)
     {
      if(deleted)Arr[q] = Arr[q + deleted];
      if(Arr[q] == 3){deleted++; q--;}
      q++;
     }
   //--------------
   ulong q2 = GetMicrosecondCount(); 
   //--------------------------------
   //ArrayResize(Arr, ArraySize(Arr) - deleted);    
   //--------------------------------
   Print(Arr[0],",",Arr[1],",",Arr[2],",",Arr[3],",",Arr[4],",",Arr[5],",",Arr[6],",",Arr[7],",",Arr[8],",",Arr[9],
        ",",Arr[10],",",Arr[11],",",Arr[12],",",Arr[13],",",Arr[14],",",Arr[15],",",Arr[16],",",Arr[17],",",Arr[18],",",Arr[19]);
   Print("Array new size  ",ArraySize(Arr),"  Тime of operation  ",q2-q1,"  deleted  ",deleted);
   //--------------------------------  
  }
//+------------------------------------------------------------------+

и:

int Arr[20] = {1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2};
int deleted;
//-------------- 
for(int a1 = 0; a1 < ArraySize(Arr); a1++)
  {
   if(Arr[a1] == 3)
     {
      if(deleted)a1-= deleted;//Cмещение назад.
      deleted++; 
     }
   if(deleted)Arr[a1] = Arr[a1 + deleted];
  }
//-------------- 
ArrayResize(Arr, ArraySize(Arr) - deleted);
Could this have been written by the same person with a little over an hour apart? The handwriting is questionable )
 
Maxim Kuznetsov:

Alexei, you're killing young talent...

This marketer hasn't been able to start selling for 5 years...and you're saying he's even worse as a programmer

Sorry, I didn't mean to kill you.

 
Maxim Kuznetsov:

:-) if you don't try to keep the order, the time isO(1), the total number of steps of all the loops= array size

too lazy to code :-)

1. Search for first 3 from left to right.

2. if found, then look for a non-triple from right to left, copy it to the place of the 3.

continue until 1,2 have intersected, trim the array by the number of copies.

Ideally, it is exactly 1/2 of "bubble sorting" :-) If instead of copying, you make a swap, the output will be a partially ordered array (all 3-thirds moved to the right)

Total number of steps of all loops=array size is O(n) complexity.
If the input array is sorted, the given problem is solved through binary search.
The complexity is O(log(n)) in the average case and O(n) in the worst case.

 
It's bad enough when you're too lazy to code.
 
Nikolai Semko:

Still got the hang of Peter's version.

It's quite compact and even works properly. Kudos to Peter.
But in terms of speed it comes second from the end. Or first place from the end, if you don't count the original, completely unusable by speed variant of the owner of this thread.

How did you test it?

 
Not an option what you threw, the last one. You have to overdo it anyway.
 
Реter Konow:

How have you been testing?

Study the code.

2018.11.14 03:26:49.182 ArrayDeleteValue (BTCUSD,M15)   вариант Pastushak: Контрольная сумма = 496575839; элементов - 998974; время выполнения = 131158 микросекунд
2018.11.14 03:26:49.185 ArrayDeleteValue (BTCUSD,M15)   вариант Korotky:   Контрольная сумма = 496575839; элементов - 998974; время выполнения = 2431 микросекунд
2018.11.14 03:26:49.188 ArrayDeleteValue (BTCUSD,M15)   вариант Fedoseev:  Контрольная сумма = 496575839; элементов - 998974; время выполнения = 1809 микросекунд
2018.11.14 03:26:49.190 ArrayDeleteValue (BTCUSD,M15)   вариант Semko:     Контрольная сумма = 496575839; элементов - 998974; время выполнения = 785 микросекунд
2018.11.14 03:26:49.194 ArrayDeleteValue (BTCUSD,M15)   вариант Pavlov:    Контрольная сумма = 496575839; элементов - 998974; время выполнения = 2839 микросекунд
2018.11.14 03:26:49.199 ArrayDeleteValue (BTCUSD,M15)   вариант Nikitin:   Контрольная сумма = 496575839; элементов - 997971; время выполнения = 4049 микросекунд
2018.11.14 03:26:49.204 ArrayDeleteValue (BTCUSD,M15)   вариант Vladimir:  Контрольная сумма = 496575839; элементов - 998974; время выполнения = 3888 микросекунд
2018.11.14 03:26:49.212 ArrayDeleteValue (BTCUSD,M15)   вариант Peter:     Контрольная сумма = 496575839; элементов - 998974; время выполнения = 7597 микросекунд
Files:
 
Алексей Тарабанов:
It's bad when you're too lazy to code.

It's not that I'm lazy at all, but MT is only on VDSs and you don't experiment on them.

about that :

template <typename T>
int arrayFilter(const  T &arr[], const T x)
{
        int i=0;
        int j=ArraySize(arr)-1;
        for(;;) {
                while(arr[i]!=x && i<j) i++;
                while(arr[j]==x && i<j) j--;
                if (i<j) {
                        arr[i++]=arr[j--];
                } else break;
        }
        ArrayResize(j+1);
        return j+1;
}

+- 1 :-) I'll be back from summer camp in a couple of days, I'll check it out...

ps. in addition, in the code, there is an extra loop entry on termination... a trifle, but it can be removed

 
Maxim Kuznetsov:

It's not that I'm lazy at all, but MT is only on VDSs and you don't experiment on them.

about that :

+- 1 :-) I'll be back from the countryside in a couple of days, I'll check it out...

ps. in addition, in the code extra input to the loop at the end...a trifle, but can be removed

Bravo! After correcting a couple of errors you may have knocked me off the pedestal even without using ArrayCopy. Checkmate. :))

template <typename T>
int arrayFilter3(T &arr[], const T x)  // вариан Kuznetsov
{
        int i=0;
        int j=ArraySize(arr)-1;
        for(;;) {
                while(arr[i]!=x && i<j) i++;
                while(arr[j]==x && i<j) j--;
                if (i<j) {
                        arr[i++]=arr[j--];
                } else break;
        }
        ArrayResize(arr,j+1);
        return j+1;
}
2018.11.14 03:48:29.572 ArrayDeleteValue (USDRUB,M1)    вариант Pastushak: Контрольная сумма = 496206996; элементов - 999002; время выполнения = 131929 микросекунд
2018.11.14 03:48:29.576 ArrayDeleteValue (USDRUB,M1)    вариант Korotky:   Контрольная сумма = 496206996; элементов - 999002; время выполнения = 2411 микросекунд
2018.11.14 03:48:29.579 ArrayDeleteValue (USDRUB,M1)    вариант Fedoseev:  Контрольная сумма = 496206996; элементов - 999002; время выполнения = 1839 микросекунд
2018.11.14 03:48:29.581 ArrayDeleteValue (USDRUB,M1)    вариант Semko:     Контрольная сумма = 496206996; элементов - 999002; время выполнения = 782 микросекунд
2018.11.14 03:48:29.585 ArrayDeleteValue (USDRUB,M1)    вариант Pavlov:    Контрольная сумма = 496206996; элементов - 999002; время выполнения = 2813 микросекунд
2018.11.14 03:48:29.590 ArrayDeleteValue (USDRUB,M1)    вариант Nikitin:   Контрольная сумма = 496206996; элементов - 997969; время выполнения = 4200 микросекунд
2018.11.14 03:48:29.596 ArrayDeleteValue (USDRUB,M1)    вариант Vladimir:  Контрольная сумма = 496206996; элементов - 999002; время выполнения = 3597 микросекунд
2018.11.14 03:48:29.604 ArrayDeleteValue (USDRUB,M1)    вариант Peter:     Контрольная сумма = 496206996; элементов - 999002; время выполнения = 7684 микросекунд
2018.11.14 03:48:29.606 ArrayDeleteValue (USDRUB,M1)    вариант Kuznetsov: Контрольная сумма = 496206996; элементов - 999002; время выполнения = 681 микросекунд
Files:
 
Nikolai Semko:

Bravo! After correcting a couple of errors, you've knocked me off the pedestal even without using ArrayCopy. Checkmate. :))

Although not exactly so, because the output array is quite different - already mixed. But it's cool anyway!

Of course, if we are dealing with quotes, this will not work. I'm on the podium again. ))

I've changed the way of calculating the checksum. No longer the simple sum of all elements, but the sum is (element value)/(element number).
And this is what I got:

2018.11.14 04:20:26.063 ArrayDeleteValue (USDRUB,M1)    вариант Pastushak: Контрольная сумма = 7090.600736767353; элементов - 999003; время выполнения = 132291 микросекунд
2018.11.14 04:20:26.067 ArrayDeleteValue (USDRUB,M1)    вариант Korotky:   Контрольная сумма = 7090.600736767353; элементов - 999003; время выполнения = 2322 микросекунд
2018.11.14 04:20:26.071 ArrayDeleteValue (USDRUB,M1)    вариант Fedoseev:  Контрольная сумма = 7090.600736767353; элементов - 999003; время выполнения = 1831 микросекунд
2018.11.14 04:20:26.074 ArrayDeleteValue (USDRUB,M1)    вариант Semko:     Контрольная сумма = 7090.600736767353; элементов - 999003; время выполнения = 773 микросекунд
2018.11.14 04:20:26.079 ArrayDeleteValue (USDRUB,M1)    вариант Pavlov:    Контрольная сумма = 7090.600736767353; элементов - 999003; время выполнения = 2879 микросекунд
2018.11.14 04:20:26.085 ArrayDeleteValue (USDRUB,M1)    вариант Nikitin:   Контрольная сумма = 7093.903216084301; элементов - 998017; время выполнения = 3605 микросекунд
2018.11.14 04:20:26.090 ArrayDeleteValue (USDRUB,M1)    вариант Vladimir:  Контрольная сумма = 7090.600736767353; элементов - 999003; время выполнения = 3622 микросекунд
2018.11.14 04:20:26.100 ArrayDeleteValue (USDRUB,M1)    вариант Peter:     Контрольная сумма = 7090.600736767353; элементов - 999003; время выполнения = 7252 микросекунд
2018.11.14 04:20:26.102 ArrayDeleteValue (USDRUB,M1)    вариант Kuznetsov: Контрольная сумма = 7085.49357433088;  элементов - 999003; время выполнения = 691 микросекунд
Files:
Reason: