...

/

Solution Review: All Permutations of an Integer List

Solution Review: All Permutations of an Integer List

This review provides a detailed analysis of generating all permutations of an integer list.

Solution

At each recursive call, we swap the number at index i with all the array elements on its right. We recursively repeat the process for sub-arrays until the sub-array of one element is left.

Let’s look at the illustration to better understand the solution before reviewing its code.

Coding exercise

Press + to interact
package main
import("fmt")
func swap(x, y int) (int, int) {
return y, x
}
func Permutation(data []int, i int, length int) {
if length == i {
fmt.Printf("%v\n", data)
return
}
for j := i; j < length; j++ {
data[i],data[j] = swap(data[i], data[j])
Permutation(data, i+1, length)
data[i],data[j] = swap(data[i], data[j])
}
}
//Testing code
func main() {
var data [3]int
for i := 0; i < 3; i++ {
data[i] = i
}
Permutation(data[:], 0, 3)
}

Time complexity

The time complexity of the solution isO ...