Home >Backend Development >C++ >How can I generate all possible permutations of an array of unique integers using a recursive approach?

How can I generate all possible permutations of an array of unique integers using a recursive approach?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-23 14:14:15868browse

How can I generate all possible permutations of an array of unique integers using a recursive approach?

Generating Permutations of an Array

Understanding the Problem

We're given an array of unique integers and asked to generate all possible permutations. Two permutations are considered different if they differ in the order of the elements. For an array of length n, there are n! possible permutations.

Approach: Recursive Permutation

The solution involves two main steps:

  1. Recursion: Take each element of the array in turn and permute the remaining elements.
  2. Exchange: Exchange the current element with elements from the remaining part to create new permutations.

Using this approach, we can generate all permutations.

Code Implementation

import java.util.ArrayList;
import java.util.List;

public class Permutation {

    public static List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        permute(nums, 0, result);
        return result;
    }

    private static void permute(int[] nums, int startIndex, List<List<Integer>> result) {
        if (startIndex == nums.length - 1) {
            // Base case: If we reach the end of the array, add the current permutation to the result.
            List<Integer> permutation = new ArrayList<>();
            for (int num : nums) {
                permutation.add(num);
            }
            result.add(permutation);
        } else {
            // Recursive case: Permute the remaining elements for each element at the current index.
            for (int i = startIndex; i < nums.length; i++) {
                swap(nums, startIndex, i);
                permute(nums, startIndex + 1, result);
                swap(nums, startIndex, i);
            }
        }
    }

    private static void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}

Example Usage

int[] nums = {3, 4, 6, 2, 1};
List<List<Integer>> permutations = Permutation.permute(nums);
for (List<Integer> permutation : permutations) {
    System.out.println(permutation);
}

Output:

[3, 4, 6, 2, 1]
[3, 4, 6, 1, 2]
[3, 4, 2, 6, 1]
[3, 4, 2, 1, 6]
[3, 4, 1, 6, 2]
[3, 4, 1, 2, 6]
[3, 2, 6, 4, 1]
[3, 2, 6, 1, 4]
[3, 2, 4, 6, 1]
[3, 2, 4, 1, 6]
[3, 2, 1, 6, 4]
[3, 2, 1, 4, 6]
[3, 6, 4, 2, 1]
[3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, 1, 3, 1, 6]
[6, 2, 4, 3, 1]
[6, 2, 4, 1, 3]
[6, 2, 1, 4, 3]
[6, 2, 1, 3, 4]
[6, 4, 2, 3, 1]
[6, 4, 2, 1, 3]
[6, 1, 2, 4, 3]
[6, 1, 2, 1, 4]
[6, 1, 4, 2, 3]
[6, 1, 4, 1, 3]
[6, 1, 3, 1, 4]
[6, 1, 3, 4, 1]
[4, 2, 6, 3, 1]
[4, 2, 6, 1, 3]
[4, 2, 1, 6, 3]
[4, 2, 1, 3, 6]
[4, 6, 2, 3, 1]

The above is the detailed content of How can I generate all possible permutations of an array of unique integers using a recursive approach?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn