Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Java8 programming,convert pseudo-code to actual java code(method) may use lots of helper functions, and using main method( public static void main (String[] args)) to test
Java8 programming,convert pseudo-code to actual java code(method) may use lots of helper functions, and using main method(public static void main(String[] args)) to test the method
Problem 5. Consider the problem of given an integer n, generating all possible permutations of the numbers {1,2,...,n}. b) Provide a non-recursive algorithm for this problem using a stack. b) For the non-recurvise version we simulate the calls to the helper function with a stack. We use the tuple (c,i,j) to denote stages of a call. The tuple ('start',i,j) corresponds to the start of the for loop for some choice of (i,j) and the tuple ('finish, i,j) to the part of the boday of the for loop after the recursive call to HELPER. 1: def PERMUTATIONS(n) 2: A array [1,2,...n] 3: Sr a stack with the tuple ('start',0,0) while S is not empty do c,i,jt pop S if c = 'start' then if i = n then output A else A[i], A[j] + A[j], A[i] push ('finish', i, j) onto S 12: push ('start', i +1,i+1) onto S 13: else if c = 'finish' then A[i], A[j] + A[j], A[i] if j
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started