Home > Backend Development > Golang > How Can I Efficiently Generate All Permutations of a List in Go?

How Can I Efficiently Generate All Permutations of a List in Go?

Susan Sarandon
Release: 2024-12-07 16:44:14
Original
801 people have browsed it

How Can I Efficiently Generate All Permutations of a List in Go?

Generating All Permutations in Go: An Efficient Approach

When dealing with datasets, it is often necessary to generate all possible permutations of a list of elements. Go provides a robust programming environment that enables the efficient creation of permutations.

Heap's algorithm is a well-known method for generating permutations. It constructs each permutation from the previous one by swapping pairs of elements. The following implementation of Heap's algorithm returns all permutations of an array:

func permutations(arr []int) [][]int {
    var helper func([]int, int)
    res := [][]int{}

    helper = func(arr []int, n int) {
        if n == 1 {
            tmp := make([]int, len(arr))
            copy(tmp, arr)
            res = append(res, tmp)
        } else {
            for i := 0; i < n; i++ {
                helper(arr, n-1)
                if n%2 == 1 {
                    tmp := arr[i]
                    arr[i] = arr[n-1]
                    arr[n-1] = tmp
                } else {
                    tmp := arr[0]
                    arr[0] = arr[n-1]
                    arr[n-1] = tmp
                }
            }
        }
    }
    helper(arr, len(arr))
    return res
}
Copy after login

Here's an example demonstrating the usage of this function:

arr := []int{1, 2, 3}
fmt.Println(permutations(arr))
Copy after login

Output:

[[1 2 3] [2 1 3] [3 2 1] [2 3 1] [3 1 2] [1 3 2]]
Copy after login

Please note that the permutations are not lexicographically sorted. To achieve sorted permutations, consider generating them using a factorial number system, as described in the linked documentation on permutations.

Other resources for generating permutations in Go include:

  • [Comprehensive guide on permutation generation](https://www.section.io/engineering-education/go-generate-all-permutations-of-a-list/)
  • [Go implementation on GitHub](https://github.com/senghoo/golang-permutations)
  • [Python's itertools.permutations() equivalent in Go](https://stackoverflow.com/questions/9179817/permutations-in-go)

The above is the detailed content of How Can I Efficiently Generate All Permutations of a List in Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template