I am currently writing code in C++ to find all possible permutations of 6 integers and store the best permutation (i.e. the one whose total is closest to a given value).
I am trying to write this code as efficiently as possible and would apreciate any advice or examples.
I was considering storing the integers in an array and performing the permutations using pointers within a loop. Is this a good approach?
They already thought of this one for you:
Note that the elements have to start in increasing order (by comparison via operator<), or else the loop will terminate before you’ve seen every permutation. See http://en.cppreference.com/w/cpp/algorithm/next_permutation for details.
It probably doesn’t give the fastest runtime possible, because at each step it has to examine the array to work out what to change. But it’s certainly the most efficient in programmer effort, and without knowing the compiler and platform it’s not really possible to micro-optimise the nested loop approaches anyway.