I want to create a program for generating steps of swap elements when changing an array to another array (for example: from {0,1,2} to {0,2,1}, step 1 ↔ 2, which means replacing the position of element 1 and element 2) taking as an example A = {0,1,3,2} and B = {2,0,3,1}, my initial concept is this:
- get steps to replace items while sorting A in the order they are added
- get the steps to replace items while sorting B in order of addition
- replace the elements in A starting from the following steps to sort A, then follow the steps to sort B, but in reverse order
this is the code i tried:
#include <stdlib.h>
#include <functional>
#include <vector>
int main(){
std::function<bool(int,int)> f=[](int a,int b){
if(a>=b)
printf("%d<->%d\n",a,b);
return a<b;
};
std::vector<int> a={0,1,3,2};
std::sort(a.begin(),a.end(),f);
printf("---\n");
std::vector<int> b={2,0,3,1};
std::sort(b.begin(),b.end(),f);
return 0;
}
output:
1<->0 //step to sort A
3<->1
2<->1
---
3<->0 //step to sort B
3<->2
1<->0
therefore, the step for changing from 0,1,3,2 to 2,0,3,1 should be:
1<->0
3<->1
2<->1
1<->0
3<->2
3<->0
but when I follow the step:
0,1,3,2
1,0,3,2
3,0,1,2
3,0,2,1
3,1,2,0
2,1,3,0
2,1,0,3
2,1,0,3 2,0,3,1, ? ? , ?