Solution Review: All Permutations of an Integer List
This review provides a detailed analysis of generating all permutations of an integer list.
We'll cover the following...
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 mainimport("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 codefunc main() {var data [3]intfor i := 0; i < 3; i++ {data[i] = i}Permutation(data[:], 0, 3)}
Time complexity
The time complexity of the solution is ...