Problem

Write a program that gives all of the permutations of characters stored in an array. For e...

Write a program that gives all of the permutations of characters stored in an array. For example:

char[] charArr = {'A', ’B1, 'C'};

findPermuations(charArr);

produces the following result:

[A, B, C]

[A, C, B]

[B, A, C]

[B, C, A]

[C, A, B]

[C, B, A]

Note that the strategy here is to start with an array of n items, and solve tltis problem in terms of permutingn-1 items—so notice that the program starts by finding permutations that start with A, leaving the problem of finding permutations of B and C. Once that is completed, you must go back and start again with B, and find permutations of A and C, and so on until each element in the array has been used as the first element. So the problem starts to look like this:

P({A, B, C}) = A + P({B, C}) producing [A, B, C]

[A, C, B]

B + p((A, C>) producing [B, A, C]

[B, C, A]

c + p({A, B})producing [c, a, b]

[C, B, A]

Rather than trying to work with a portion of the array, it will be helpful to include an index that keeps track of which element you are working on. For example, the initial call should include a parameter 0 (representing the fact that you are starting with the first element), and subsequent calls increment this index, so now we have

p({A, B, C}, 0) = P({A, B, C}, 1) producing [A, B, C]

[A, C, B]

p({b, A, C}, 1) producing [B, a, C]

[B, c, A]

p({C, A, B}, 1) producing [C, A, B]

[C, B, A]

Since each subsequent call needs to change the order of the elements, you should make a copy of the array and use diat in the recursive call. Also, think about what would be the base case—and when it is reached, print the array using Arraytostring.

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
Solutions For Problems in Chapter 6
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