Question

(Java - Stack) Describe a nonrecursive algorithm for enumerating all permutations of the numbers {1,2, ....

(Java - Stack) Describe a nonrecursive algorithm for enumerating all permutations of the numbers {1,2, . . . ,n} using an explicit stack.

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

Implementation of procedure combination which accepts the two parameters and calculates the permutations of the numbers {1,2 … n}

Algorithm:

Step 1: Start

Step 2: Create a procedure combination which accepts two parameters n and k as a type of integers.

Step 2.1: Declare an output as an array as a type of integer with size n.

Step 2.2: Declare stkPtr as stack<int> type and initialize the stkPtr.

Step 2.3: Push the initially value 0 to stack through stack object stkPtr.

Step 2.4: Iterate loop through while, check the Count of the stack is greater than zero.

Step 2.4.1: Declare indexflag as a type of integer variable and assign the resultant value of stkPtr.Count -1 to indexflag.

Step 2.4.2: Declare popValue as a type of integer variable and assign the resultant value of stkPtr.Pop() to popValue.

Step 2.4.3: Iterate the loop popValue is less than k then

Step 2.4.3.1: Assign the pre-increment value of popValue to output[indexflag++].

Step 2.4.3.2: push the popValue into stack object stkPtr.

Step 2.4.3.3: check indexflag is equal to n then

                        Step 2.4.3.3.1: return the output.

                        Step 2.4.3.3.2 : break the loop through the break statement.

Step 3: End the procedure combination.

Step 4: Call the procedure combination from the main method with appropriate parameters n and k.

Step 5: Stop.

Add a comment
Know the answer?
Add Answer to:
(Java - Stack) Describe a nonrecursive algorithm for enumerating all permutations of the numbers {1,2, ....
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