Skip to content

Instantly share code, notes, and snippets.

@danielrangelmoreira
Last active April 30, 2017 20:35
Show Gist options
  • Save danielrangelmoreira/49aec9d478e216fcd1034957dcc5b126 to your computer and use it in GitHub Desktop.
Save danielrangelmoreira/49aec9d478e216fcd1034957dcc5b126 to your computer and use it in GitHub Desktop.
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