Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6596775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:07:12+00:00 2026-05-25T18:07:12+00:00

I find recursion, apart from very straight forward ones like factorial, very difficult to

  • 0

I find recursion, apart from very straight forward ones like factorial, very difficult to understand. The following snippet prints all permutations of a string. Can anyone help me understand it. What is the way to go about to understand recursion properly.

void permute(char a[], int i, int n)
{
   int j;
   if (i == n)
     cout << a << endl;
   else
   {
       for (j = i; j <= n; j++)
       {
          swap(a[i], a[j]);          
          permute(a, i+1, n);
          swap(a[i], a[j]);
       }
   }
} 

int main()
{
   char a[] = "ABCD";
   permute(a, 0, 3);
   getchar();
   return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:07:13+00:00Added an answer on May 25, 2026 at 6:07 pm

    PaulR has the right suggestion. You have to run through the code by “hand” (using whatever tools you want – debuggers, paper, logging function calls and variables at certain points) until you understand it. For an explanation of the code I’ll refer you to quasiverse’s excellent answer.

    Perhaps this visualization of the call graph with a slightly smaller string makes it more obvious how it works:
    Call graph

    The graph was made with graphviz.

    // x.dot
    // dot x.dot -Tpng -o x.png
    digraph x {
    rankdir=LR
    size="16,10"
    
    node [label="permute(\"ABC\", 0, 2)"] n0;
     node [label="permute(\"ABC\", 1, 2)"] n1;
      node [label="permute(\"ABC\", 2, 2)"] n2;
      node [label="permute(\"ACB\", 2, 2)"] n3;
     node [label="permute(\"BAC\", 1, 2)"] n4;
      node [label="permute(\"BAC\", 2, 2)"] n5;
      node [label="permute(\"BCA\", 2, 2)"] n6;
     node [label="permute(\"CBA\", 1, 2)"] n7;
      node [label="permute(\"CBA\", 2, 2)"] n8;
      node [label="permute(\"CAB\", 2, 2)"] n9;
    
    n0 -> n1 [label="swap(0, 0)"];
    n0 -> n4 [label="swap(0, 1)"];
    n0 -> n7 [label="swap(0, 2)"];
    
    n1 -> n2 [label="swap(1, 1)"];
    n1 -> n3 [label="swap(1, 2)"];
    
    n4 -> n5 [label="swap(1, 1)"];
    n4 -> n6 [label="swap(1, 2)"];
    
    n7 -> n8 [label="swap(1, 1)"];
    n7 -> n9 [label="swap(1, 2)"];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying to write recursion function,to find factorial of a number. int factorial(int
Using recursion, find an index that cuts an array in two parts so that
What is the easiest way, preferably using recursion, to find the shortest root-to-leaf path
find . -type f -print prints out ./file1 ./file2 ./file3 Any way to make
I find the concept of the programmable, tiling, keyboard-focuessed window manager ion3 very appealing,
I find from reading perldoc perlvar, about a thousand lines in is help for
I've got some Javascript code which uses fairly deep recursion and I'd like to
I have this function to find all the paths from root to leaf in
I have the following function in my c# silverlight application to find the total
I am trying to find the Big O for stooge sort. From Wikipedia algorithm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.