Question

Create the following function in java:

ALGORITHM PERMUTATION GENERATOR PermGenerator(integer n 22) li generates in lexicographical order all permutations llof the i

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

SampleOutput:

RawCode:

import java.util.*;
class permutation
{
   public static int fact(int n)        //factorial function in recursion
   {
  
   return(n==1||n==0)?1:n*fact(n - 1);
   }
   public static int PermGenerator(int n)  
   {
     
       int i,j;   //declaring variables
       int k;
       int[] d;   //initialinzing array
       d=new int [n+1];   //allocating memory for array
       for(k=1;k<=n;k++)   //creatinh smallest permutation
           d[k-1]=k;
       for(i=0;i<n;i++)
           System.out.print(d[i]);
       System.out.println("\n");
       for(k=2;k<=fact(n);k++)
       {
           i=n-1;
           j=n;
           while(d[i-1]>d[j-1])
           {
               i=i-1;
               j=j-1;
           }
           j=n;
           while(d[i-1]>d[j-1])
               j=j-1;
           int temp=d[i-1];   //swaping
           d[i-1]=d[j-1];
           d[j-1]=temp;
           i=i+1;       //incrementing i value
           j=n;  
           while(i<j)
           {
               int t=d[i-1];       //swaping
               d[i-1]=d[j-1];
               d[j-1]=t;
               i=i+1;
               j=j-1;
           }
           for(int a=0;a<n;a++)
               System.out.print(d[a]);   //printing permutation
           System.out.println("\n");
       }
       return -1;
}
public static void main(String args[])       //main method
{
    Scanner sc=new Scanner(System.in);      
    int n;
    System.out.println("Enter the n value: ");   //getting input from user
    n=sc.nextInt();
    System.out.println("\n");
    PermGenerator(n);       //calling the PermGenerator() function
}
}

--------------------------------------------------------------------------

Hope you understand.If you have any doubts comment me.Upvote me.

Add a comment
Know the answer?
Add Answer to:
Create the following function in java: ALGORITHM PERMUTATION GENERATOR PermGenerator(integer n 22) li generates in lexicographical...
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
  • JAVA For the code in which I implemented most of the quick select algorithm. Quick select...

    JAVA For the code in which I implemented most of the quick select algorithm. Quick select is a O(n) time algorithm to find the nth smallest value in an (unordered list). The following recursive algorithm finds thenth smallest element with an index bewtween left and right in list: Code: Integer QuickSelect(list, left, right, n) { if left = right // If we only have one index available return list[left] // return the element at that index endif pivotIndex ⇐ partition(list,...

  • Perform the following to the algorithm below: - - Express T(n) as a function of n...

    Perform the following to the algorithm below: - - Express T(n) as a function of n Find a best approximation for the Big O function for T(n) Perform a time complexity analysis Define the basic operation of the algorithm Correctness Efficiency - - Procedure maxMin (n, A, I, h) integer h, I, A (1:n), n integer j j-2 IA (1) hS (1) while (i <=n) do if (Ali) < 1) then TEA (0) if(Ali) >h) then h A() j+į+1 repeat...

  • Let (,)j-0,n-1 be an arbitrary set of n integer-valued coordinates Hence the values of rj and...

    Let (,)j-0,n-1 be an arbitrary set of n integer-valued coordinates Hence the values of rj and y are integers In this question, we deine the bounding rectangle as follows 1. The rectangle has horizontal and vertical edges. 2. It is the smallest rectangle which encloses all the points (Fj, yi), j = 0, ,n-1. 3. Let the coordinates of the bounding rectangle be (uo, vo), (ui,vi), (u2, 2) and (us, vs) (u0,t0) = botton left corner (u1, v)bom right corner...

  • JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers:...

    JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers: 47 71 15 35 66 61 44 26 68 56 18 19 36 84 69 55 1. Find the value of pivot 2. Show the result after partitionIt() is called first time 3. Show the value of partition 4. Show the content of the array ///////////////////////////// Lab6.java class ArrayIns { private long[] theArray; // ref to array theArray private int nElems; // number of...

  • 6. Consider the following algorithm, where P is an array containing random numbers. The function swap(v1,v2)...

    6. Consider the following algorithm, where P is an array containing random numbers. The function swap(v1,v2) will swap the values stored in the variables v1 and v2. Note that % is the modulus operation, and will return the integer remainder r of a/b, i.e., r-a%b Require: Array P with n > 0 values 1: i-1, j-n-l 2: while i<=j do for a=i to j by i do 4: 5: 6: 7: if Pla>Pat 11 and Pla]%2--0 then swap(Plal, Pla+1l) end...

  • (V). Given the following algorithm, answer relevant questions. Algorithm 1 An algorithm 1: procedure WHATISTHIS(21,22,...,n: a...

    (V). Given the following algorithm, answer relevant questions. Algorithm 1 An algorithm 1: procedure WHATISTHIS(21,22,...,n: a list of n integers) for i = 2 to n do c= j=i-1 while (j > 0) do if ra; then break end if 4j+1 = a; j= j-1 end while j+1 = 1 end for 14: return 0.02. 1, 15: end procedure Answer the following questions: (1) Run the algorithm with input (41, 02, 03, 04) = (3, 0, 1,6). Record the values...

  • (15 points) Consider the algorithm for insertion sort shown below. The input to this algorithm is...

    (15 points) Consider the algorithm for insertion sort shown below. The input to this algorithm is an earray A. You must assume that indexing begins at 1. 1: for j = 2: A.length do key = A i=j-1 while i > 0 and A[i] > key do Ali + 1] = Ai i=i-1 7: A[i+1] = key (a) Follow this algorithm for A[1..4) =< 7,9,6,8 >. Specifically, please indicate the contents of the array after each iteration of the outer...

  • 9. [10 points) Consider the following algorithm: procedure Algorithm(n: positive integer; ddd: distinet integers) for k:=1 to n-1 for 1-1 to n-k print(k, I, di,da...-1,dn) if ds dti then interch...

    9. [10 points) Consider the following algorithm: procedure Algorithm(n: positive integer; ddd: distinet integers) for k:=1 to n-1 for 1-1 to n-k print(k, I, di,da...-1,dn) if ds dti then interchange dy and d (a) Assume that this algorithm receives as input the integer n 6 and the input sequence 하하하하하하, Miss ^-ruteae rehen i12|3141516 Fill out the table below: ds ds (b) Assume that the algorithm receives the same input values as in part a). Once the algorithm finishes, what...

  • Search for 4 in the sequence {3,5,7,8,9,12,21,25}, by working through each step of the algorithm given...

    Search for 4 in the sequence {3,5,7,8,9,12,21,25}, by working through each step of the algorithm given below. Specify the values of i, j, m and am in each step. procedure binary search (x: integer, a1,a2,...,an: increasing integers) i := 1 {i is left endpoint of search interval } j := n {j is right endpoint of search interval } while i < j m := b(i + j)/2c if x > am then i := m + 1 else j...

  • Create an ArrayListReview class with one generic type to do the following • Creates an array...

    Create an ArrayListReview class with one generic type to do the following • Creates an array list filled with the generic type of the ArrayListReview class, and inserts new elements into the specified location index-i in the list. (5 points) • Create a method inside the class to implement the calculation of Fibonacci numbers. Use System.nanoTime to find out how much time it takes to get fab(50). Create a method inside the class to check if an array list is...

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