Java and Algorithm (4)-Digital full array

Source: Internet
Author: User

The full arrangement refers to the number of n (or other characters) of all possible permutations, such as 1 2 33 digits of the full arrangement is

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

So the question comes, arbitrarily enter a number greater than 1 N, listing the full array of 1-n n numbers.

If you try to enumerate the full permutations of 1 2 3 manually, you will find that we usually make the rules in our minds and enumerate them according to the established rules to get all the permutations.

The rules we set here are:

(Imagine we've got 3 numbers in our hands, with a, B, C, three seats on the ground)

1) before each vacancy, try to put down a number in the order of 1->2->3, and if the number is already down, try the next

2) Move one cell backwards after each drop and repeat the 1->2->3 attempt

3) If there is no new possibility in the current position, retrieve the current position number and move left one cell to try again

According to the above rules it is easy to deduce that the first permutation is 1 2 3

Retrieve 3, return B position, fetch 2, and then press 1->2->3 try, find can put down 3, move right to C, try to drop 2, get 1 3 2

The next step must return to the position of a new possibility, at this time has retrieved all the numbers, according to the rule down 2, move to B, put down 1, move to C, put down 3, get 2 1 3

。。。

Here's what the implementation code looks for:

[Java]View PlainCopyprint?
  1. Public class Permutation {
  2. private int max;
  3. private int[] array;
  4. private int[] hold;
  5. Public permutation (int max) {
  6. This.max = max;
  7. Array = new Int[max + 1];
  8. Hold = new Int[max + 1];
  9. }
  10. public void permute (int step) {
  11. if (step = = max + 1) {
  12. For (int i = 1; I <= max; i++) {
  13. System.out.print (Array[i] + "");
  14. }
  15. System.out.println ();
  16. return; //returns to the last step, which is the last row of the most recent call to the Permute method
  17. }
  18. //try in order of 1->2->3->...->n
  19. For (int num = 1; num <= max; num++) {
  20. //Determine if the number is still held
  21. if (hold[num] = = 0) {
  22. Array[step] = num;
  23. Hold[num] = 1;
  24. //recursion: Move right one cell repeat the attempt to traverse the number
  25. Permute (step + 1);
  26. Retrieve current position number when returning to current position
  27. Hold[num] = 0;
  28. }
  29. }
  30. }
  31. public static void Main (string[] args) {
  32. Permutation FA = new permutation (3);
  33. Fa.permute (1);
  34. }
  35. }

Run output

[Java]View PlainCopy  print?
    1. 1 2 3
    2. 1 3 2
    3. 2 1 3
    4. 2 3 1
    5. 3 1 2
    6. 3 2 1

We use a pseudo-sequence diagram to help understand the execution of recursive calls

By the way, there are many algorithms for the full permutation problem, and the model of the depth-first algorithm is used in this paper.

Java and Algorithm (4)-Digital full array

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.