Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Non recursive Heap (1963) algorithm
package main
import "fmt"
func generate(n int, a []int64) {
var c = make([]int, n)
fmt.Println(a)
i := 0
for i < n {
if c[i] < i {
if i%2 == 0 {
a[0], a[i] = a[i], a[0]
} else {
a[c[i]], a[i] = a[i], a[c[i]]
}
fmt.Println(a)
c[i]++
i = 0
} else {
c[i] = 0
i++
}
}
}
func main() {
var digits = []int64{1, 2, 3, 4, 5, 6, 7, 8, 9}
generate(len(digits), digits)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment