MQL5 Language: #define SWAP - page 2 12 New comment Arthur Albano 2018.08.26 19:38 #11 Full code example, if another user need for reference. #define SWAP(a,b) {a=a+b;b=a-b;a=a-b;} #define SORT(a,b,c) {if(a > b) SWAP(a,b); if(a > c) SWAP(a,c); if (b>c) SWAP(b,c)} void OnStart() { int Num1 = rand(); int Num2 = rand(); int Num3 = rand(); SORT(Num1, Num2, Num3); } Arthur Albano 2018.08.26 19:40 #12 fxsaber: Beatiful. Arthur Albano 2018.08.26 19:45 #13 Full example (again): #define SWAP(a, b) a ^= (b ^= (a ^= b)); #define SORT(a, b, c) {if(a > b) SWAP(a,b); if(a > c) SWAP(a,c); if (b>c) SWAP(b,c)} void OnStart() { int Num1 = rand(); int Num2 = rand(); int Num3 = rand(); SORT(Num1, Num2, Num3); } Arthur Albano 2018.08.27 06:33 #14 Try to swap numbers in an array with the same position to see what happens... Arthur Albano 2018.08.27 06:44 #15 Try to take out if's before the SWAP macro... Self-regressive Median Coefficient 2018.08.27 www.mql5.com Hi all, I am trying to build an indicator that uses the following formula and returns the expected value, so I can use the same chart as indicator_... fxsaber 2018.08.27 09:03 #16 Arthur Albano: Try to take out if's before the SWAP macro...#define SWAP(a, b) { if (a != b) VALUE(a) ^= (VALUE(b) ^= (VALUE(a) ^= VALUE(b))); } #define SORT(a, b, c) {if(a > b) SWAP(a,b); if(a > c) SWAP(a,c); if (b>c) SWAP(b,c)} // Partition using Lomuto partition scheme #define VALUE(A) list[A] int partition(int &list[],int left,int right,int pivotIndex) { int pivotValue = list[pivotIndex]; SWAP(pivotIndex,right); int storeIndex = left; for(int i=left; i<right; i++) { if(list[i] < pivotValue) { SWAP(storeIndex,i); storeIndex++; } } SWAP(right,storeIndex); return storeIndex; } #undef VALUE #define VALUE(A) A 12 New comment
Full code example, if another user need for reference.
#define SWAP(a,b) {a=a+b;b=a-b;a=a-b;} #define SORT(a,b,c) {if(a > b) SWAP(a,b); if(a > c) SWAP(a,c); if (b>c) SWAP(b,c)} void OnStart() { int Num1 = rand(); int Num2 = rand(); int Num3 = rand(); SORT(Num1, Num2, Num3); }Beatiful.
Full example (again):
Try to take out if's before the SWAP macro...