Question

. Consider the problem of generating all the possible permutations of length n. For example, the...

. Consider the problem of generating all the possible permutations of length n. For example, the permutations of length 3 are: {1,2,3}, {2,1,3},{2,3,1}, {1,3,2}, {3,1,2}, {3,2,1}.

Write a Well documented pseudocode of a non-recursive algorithm that computes all the permutations of size n. The only data structure allowed is a queue. Any other memory usage should be O(1). Calculate the time complexity of your algorithm using the big-Oh notation. Show all calculations.

(The code should be written in Java!!)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Pseudocode of a non-recursive algorithm:

Screenshots of the java Program: Generat_All_Permutations.java

Sample output:

Code to copy:  Generat_All_Permutations.java

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Generat_All_Permutations
{
   /*function name: permutations
   * parameters: n- size
   * non-recursive algorithm that computes all
   * the permutations of size n
   */
   public static void findPermutations(int n)
   {
       String startStr = "";
       //create inital string of length n
       for (int i = 1; i <= n; i++)
       {
           startStr += "" + i;
       }
       // Create a queue using to store permutations
       Queue<String> queue = new LinkedList<>();
       // Add first characeter of the startStr into queue
       queue.add(String.valueOf(startStr.charAt(0)));
      
       // Add all the characters into queue for permutations
       for (int x = 1; x < startStr.length(); x++)
       {
           /*consider previously constructed partial permutation one by one
           *iterate backwards to avoid ConcurrentModificationException)
           */
           for (int y = queue.size() - 1; y >= 0; y--)
           {
               // remove current partial permutation from the queue
               String str = queue.remove();
               /* Insert next character of the specified string in all
               * possible positions of current partial permutation. Then
               * insert each of these newly constructed string in the list
               */
               for (int k = 0; k <= str.length(); k++)
               {
                   // Advice: use StringBuilder for concatenation
                   String newS = str.substring(0, k);
                               newS += startStr.charAt(x);
                               newS += str.substring(k);
                   //add new string into queue
                   queue.add(newS);                  
               }
           }
       }
       //Print All permutations of the string length n
       while(!queue.isEmpty())
       {
       // remove current partial permutation from the queue
           String str = queue.remove();
           System.out.print("{");
           for (int k = 0; k < str.length(); k++)
           {
               if(k==str.length()-1)
                   System.out.print(str.charAt(k)+"} ");
               else
                   System.out.print(str.charAt(k)+",");
           }
       }
      
   }

   /* program to generate all permutations of a String
   * using non-recursive
   */

   public static void main(String[] args)
   {
       //Scanner object to read N value
       Scanner sc=new Scanner(System.in);
       //Prompt n value
       System.out.print("Enter Length of the starting String: ");
       int n = sc.nextInt();//read n value
       System.out.println("\nAll permutations: ");
   //call method findPermutations to print all permutations
       findPermutations(n);
   }
}

Add a comment
Know the answer?
Add Answer to:
. Consider the problem of generating all the possible permutations of length n. For example, the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT