I have some difficulty in tracking recursive functions. For example, take the following function for string permutation as an example:
void string_permutation(string str, int mid, int end)
{
if (mid == end)
cout << str << endl;
else
{
for (int i = mid; i <= end; i++)
{
swap(str[mid], str[i]);
string_permutation(str, mid + 1, end);
swap(str[mid], str[i]);
}
}
}
Given an input, say "abcd" and call it by:
string str = "abcd";
string_permutation(str, 0, str.size()-1);
How can I quickly (by manually tracking, i.e. without debugger, consider you're in a interview) find out, say, the 10th output of such function? More generally, how to trace a recursive function?