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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:20:14+00:00 2026-06-11T12:20:14+00:00

I am using a recursive algorithm to list all possible permutations of elements of

  • 0

I am using a recursive algorithm to list all possible permutations of elements of an array p = {1,2,3}. Following is the simple recursive implementation I am using:

void swap(int x, int y){
    int temp = array[x];
    array[x]=array[y];
    array[y]=temp;    
    return;
}

void printArray(int size){
    int i;

    for (i=0;i<size;i++)
        printf("%d ", array[i]);

    printf("\n");    
    return;
}

void permute(int k,int size){
    int i;

    if (k==0)
        printArray(size);
    else{
        for (i=k-1;i>=0;i--){
            swap(i,k-1);
            permute(k-1,size);
            swap(i,k-1);
        }
    }    
    return;
}

The problem is instead of printing them I want to add every permutation to a 2D array. Currently, I am printing the permutations to a file and then reading it to a 2D array, but I think there should be a better way to do this.

  • 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-06-11T12:20:16+00:00Added an answer on June 11, 2026 at 12:20 pm

    declaring these as global:

    int **resultArray;
    resultArray = malloc( (n!) * sizeof(int *));    // n! : factorial of n
    for(i=0; i<(n!); i++)
        resultArray[i] = malloc(size * sizeof(int));
    int index = 0;
    

    this to fill in the 2 dimensional array:

    void addToArray(int size){
        int i;
        for(i=0 ; i<size ; i++)
            resultArray[index][i] = array[i];
        index++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've heard that any recursive algorithm can always be expressed by using a stack.
I have written a recursive DFS algorithm to traverse a graph: void Graph<E, N>::DFS(Node
I'm using a recursive tree of hashmaps, specifically Hashmap map where Object is a
This question is related to this SO post Rather than using a recursive CTE
Problem: I am trying to build a recursive tree using a function and data
How can I speed up a recursive code using OpenMP? Basically, I have to
I am trying to fix a problem with my recursive function JS using a
Using recursion, find an index that cuts an array in two parts so that
I was trying to make a tail-recursive version of this very simple SML function:
Java 6's mergesort implementation in Arrays.java uses an insertion-sort if the array length is

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.