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 6145279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:46:15+00:00 2026-05-23T18:46:15+00:00

I’m having some difficulty converting this recursive algorithm for displaying all the permutations of

  • 0

I’m having some difficulty converting this recursive algorithm for displaying all the permutations of a given set of integers into an iterative one.

void getPermutationsR(int v[], int n, int i) 
{
    if (i == n)
    {
        //Display contents of v
    } 
    else
    {
        for (int j=i; j<n; j++) 
        {
            swap (v, i, j);
            getPermutationsR(v, n, i+1);
            swap (v, i, j);
        }
    }
}

This is my current attempt, it’s completely wrong but I can’t see any way to correct it without using a natively iterative algorithm for the problem . Half my attempts have had me ‘popping’ more than ‘pushing’ (Leads to an error as I attempt to access elements in an empty stack) and the other half I’m ‘pushing’ more than ‘popping’ (infinite loop).

void getPermutationsI(int v[], int n, int i) 
    {
    stack<int> iStack;
    stack<int> jStack;

    iStack.push(i);
    jStack.push(i);

    while(iStack.size() > 0)
    {
        if (iStack.top() == n)
        {
            jStack.pop();
            iStack.pop();
            //Display contents of v
        }
        else
        {
            for (int j = iStack.top(); j < n; j++)
            {
               //swap 
                               //something to do with jStack
            }
            //swap 
            iStack.push(i+1);
        }
    }
}
  • 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-23T18:46:15+00:00Added an answer on May 23, 2026 at 6:46 pm

    The challenge you are encountering is that you’ve got function calls and loop constructs intermingled. It is hard to disentangle those.

    First let’s start by replacing all control of flow operations with recursion.

    // You'll want to start with getPermutionsR(v, n, 0, 0)
    void getPermutationsR(int v[], int n, int i, int j) 
    {
        if (i == n)
        {
            //Display contents of v
        }
        else if (j == n) {
            // By doing nothing, we break out of the loop
        }
        else
        {
            // This was your recursive call inside of the loop.
            // Note that I'm sending you to to the first loop iteration here.
            swap (v, i, j);
            getPermutationsR(v, n, i+1, i+1);
            swap (v, i, j);
    
            // And the next loop iteration
            getPermutationsR(v, n, i, j+1);
        }
    }
    

    Next let’s add more state so that we only have one recursive call inside of an if condition.

    // You'll want to start with getPermutionsR(v, n, 0, 0, 1)
    void getPermutationsR(int v[], int n, int i, int j, bool firstCall)
    {
        if (i == n)
        {
            //Display contents of v
        }
    
        int x = i;
        int y = j+1;
        if (firstCall) {
            swap (v, i, j);
            x = i+1;
            y = i+1;
        }
    
        // My one recursive call.  Note that i=n implies j=n.
        if (j < n) {
            getPermutationsR(v, n, x, y, !firstCall);
        }
    
        if (firstCall) {
            swap (v, i, j);
        }
    }
    

    Now that we’ve accomplished this, we have it in a form where we can transform it into an iterative version in a straightforward way. Here is the transformation

    void recursiveForm (params, recursiveState) {
        topHalf...
        if (cond) {
            recursiveForm(...)
        }
        bottomHalf...
    }
    

    becomes

    void iterativeForm(params) {
        initializeStacks...
        pushStacks...
        topHalf...
        while (stacks not empty) {
            if (cond) {
                pushStacks...
                topHalf...
            }
            else {
                bottomHalf...
                popStacks...
            }
        }
    }
    

    So applying that pattern we get something like:

    // You'll want to start with getPermutionsR(v, n, 0, 0, 1)
    void getPermutationsI(int v[], int n)
    {
        stack<int> iStack;
        stack<int> jStack;
        stack<bool> firstCallStack;
    
        // pushStacks with initial value
        iStack.push(0);
        jStack.push(0);
        firstCallStack.push(1);
    
        // topHalf...
        if (iStack.top() == n)
        {
            //Display contents of v
        }
    
        int x = iStack.top();
        int y = jStack.top()+1;
        if (firstCallStack.top()) {
            swap (v, iStack.top(), jStack.top());
            x = iStack.top()+1;
            y = iStack.top()+1;
        }
    
        while iStack.size() > 0) {
            // if (cond) {
            if (jStack.top() < n) {
                //pushStacks...
                iStack.push(x);
                jStack.push(y);
                firstCallStack.push(!firstCallStack.top());
    
                // topHalf...
                if (iStack.top() == n)
                {
                    //Display contents of v
                }
    
                x = iStack.top();
                y = jStack.top()+1;
                if (firstCallStack.top()) {
                    swap (v, iStack.top(), jStack.top());
                    x = iStack.top()+1;
                    y = iStack.top()+1;
                }
            }
            else {
                // bottomHalf...
                if (firstCallStack.top()) {
                    swap (v, iStack.top(), jStack.top());
                }
            }
        }
    }
    

    (Warning, all code untested, and may well not even compile. But the idea is correct. And it is definitely the same algorithm.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.