Skip to content

Instantly share code, notes, and snippets.

@unmeshvrije
Created March 26, 2017 22:44
Show Gist options
  • Save unmeshvrije/e0b817a792b36fca4894714c53cc2e62 to your computer and use it in GitHub Desktop.
Save unmeshvrije/e0b817a792b36fca4894714c53cc2e62 to your computer and use it in GitHub Desktop.
Recursive function to generate permutations of all elements of an array
void generate_all_permutations(vector<int>& toBePermuted, int nextIndex)
{
if (nextIndex == toBePermuted.size()){
print_vector(toBePermuted);
return;
}
for (int i = nextIndex; i < toBePermuted.size(); ++i) {
swap(toBePermuted[i], toBePermuted[nextIndex]);
generate_all_permutations(toBePermuted, nextIndex+1);
swap(toBePermuted[i], toBePermuted[nextIndex]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment