-
Notifications
You must be signed in to change notification settings - Fork 0
/
pb24v1.go
108 lines (98 loc) · 2.69 KB
/
pb24v1.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"fmt"
"math/rand"
)
func removeFromArray(array []int, index int) []int{
// Removes element at given index from array
removed := make([]int, len(array[:index]) + len(array[index+1:]))
copy(removed, array[:index])
copy(removed[len(array[:index]):], array[index+1:])
return removed
}
func removeFromIntArray(array [][]int, index int) [][]int{
// Removes element at given index from array
removed := make([][]int, len(array[:index]) + len(array[index+1:]))
copy(removed, array[:index])
copy(removed[len(array[:index]):], array[index+1:])
return removed
}
func permutations(input []int) [][]int{
// Returns a list of all permutations
perms := [][]int{}
if len(input) == 1{
perms = append(perms, input)
} else {
for i:=0;i<len(input);i++{
arr_ex := removeFromArray(input, i)
permutations_of_subset := permutations(arr_ex)
for j:=0;j<len(permutations_of_subset);j++{
p := append(permutations_of_subset[j], input[i])
perms = append(perms, p)
}
}
}
return perms
}
func swapIndexes(array [][]int,i1,i2 int) [][]int{
// Swap indexes in a double array
tmp := array[i1]
array[i1] = array[i2]
array[i2] = tmp
return array
}
func lexigraphicSort(input [][]int) [][]int{
// Bubble sort of a double integer array
// Very slow
for k:=0;k<len(input[0]);k++{
fmt.Println("Increasing k")
for i:=0;i<len(input);i++{
for j:=i+1;j<len(input);j++{
//fmt.Printf("Checking input[%d][%d] and input[%d][%d]\n", i,k,j,k)
l := fmt.Sprintf("%v", input[i][:k])
t := fmt.Sprintf("%v", input[j][:k])
if l != t{ break }
if input[i][k] > input[j][k] {
input = swapIndexes(input, i, j)
}
}
}
}
return input
}
func concatenate(l1,l2 [][]int) [][]int{
concatenated := make([][]int, len(l1)+len(l2))
copy(concatenated, l1)
copy(concatenated[len(l1):], l2)
return concatenated
}
func lexigraphicQuickSort(input [][]int) [][]int{
// Quick sort of a double integer array
// Too slow for this exercice as well.
if len(input) <= 1{
return input
}
pivot_index := rand.Intn(len(input))
pivot_value := input[pivot_index][0]
array := removeFromIntArray(input, pivot_index)
less := [][]int{}
greater := [][]int{}
for _,v := range array{
if v[0] <= pivot_value {
less = append(less, v)
} else {
greater = append(greater, v)
}
}
piv := [][]int{input[pivot_index]}
res := concatenate(lexigraphicQuickSort(less),piv)
res = concatenate(res, lexigraphicQuickSort(greater))
return res
}
func main(){
in := []int{0,1,2,3,4,5,6,7,8,9}
l := permutations(in)
t := lexigraphicSort(l)
nth := 1000000
fmt.Println(t[nth-1])
}