Ok, I see now. Thanks for posting your loop code, couldn't figure out what you meant from your first post. Not sure how I'd do it with nested loops, but with recursive functions it should be doable, if a bit fiddly.

You have:

Code:

while ( next_permutation (myints+6,myints+9) );
}
while ( next_permutation (myints+3,myints+6) );
}
while ( next_permutation (myints,myints+3) );
return 0;

So obviously next_permutation(something) will be at the centre of all this. I still don't know how you decide how many groups you'll permute the numbers in. If you had 18 numbers, you could have 2 sets of 9, 9 sets of 2, 3 sets of 6.... you get the idea. If we have G groups and N numbers, we can generalise the code above:

g is current group

groupsize is N/G

Code:

next_permutation (myints+(g*groupsize),myints+groupsize + (g * groupsize))

So if I label the groups 0..G-1

Then we can write a function that'll recursively call itself.

Code:

void processGroup(int* myints, int group, int groups, int n, int groupsize) //plus anything else you need
{
bool perm = false;
do
{
if (g != groups-1) // if not the furthest-right group, call self with next group to the right
processGroup(..., group+1,...)
perm = next_permutation (...);
} while (perm);
}

You'd call it initially for group 0, then it'd straight away move over to the rightmost group, which is where you wanted to start.

That's the bones of how I'd do it. It wouldn't want to see how it performs on larger values of N. Probably not well -- the recursion isn't deep, but lots of function call overhead could be expensive.

If you do have some way of dynamically determining the size of the groups to permute, then it'll still work, just do that calculation before first call. Sounds like you're just picking at random though.