forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heaps.go
43 lines (39 loc) · 1.08 KB
/
heaps.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package permutation
import (
"strings"
)
// Heap's Algorithm for generating all permutations of n objects
func Heaps(out chan []string, n int) {
elementSetCh := make(chan []string)
go GenerateElementSet(elementSetCh, n)
elementSet := <-elementSetCh
var recursiveGenerate func([]string, int, []string)
var permutations []string
recursiveGenerate = func(previousIteration []string, n int, elements []string) {
if n == 1 {
permutations = append(permutations, strings.Join(elements, ""))
} else {
for i := 0; i < n; i++ {
recursiveGenerate(previousIteration, n-1, elements)
if n%2 == 1 {
tmp := elements[i]
elements[i] = elements[n-1]
elements[n-1] = tmp
} else {
tmp := elements[0]
elements[0] = elements[n-1]
elements[n-1] = tmp
}
}
}
}
recursiveGenerate(permutations, n, elementSet)
out <- permutations
}
func GenerateElementSet(out chan []string, n int) {
elementSet := make([]string, n)
for i := range elementSet {
elementSet[i] = string(rune(i + 49)) // Adjust this if you want to change your charset
}
out <- elementSet
}