Created
March 26, 2017 22:44
-
-
Save unmeshvrije/e0b817a792b36fca4894714c53cc2e62 to your computer and use it in GitHub Desktop.
Recursive function to generate permutations of all elements of an array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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