forked from go-nerds/cli-algorithms-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
167 lines (139 loc) · 3.44 KB
/
main.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"bufio"
"fmt"
"os"
"strings"
"time"
"github.com/manifoldco/promptui"
)
const (
BubbleSort int = 1
SelectionSort int = 2
InsertionSort int = 3
GnomeSort int = 4
CocktailShakerSort int = 5
CombSort int = 6
OddEvenSort int = 7
)
type options struct {
Name string
Uuid int
}
func main() {
clearConsole()
handleInterrupt()
r := bufio.NewReader(os.Stdin)
n := getSliceSize(r)
arr := generateSlice(r, n)
fmt.Println("Initial array:", arr)
algorithms := []options{
{Name: "Bubble Sort", Uuid: 1},
{Name: "Selection Sort", Uuid: 2},
{Name: "Insertion Sort", Uuid: 3},
{Name: "Gnome Sort", Uuid: 4},
{Name: "Cocktail Shaker Sort", Uuid: 5},
{Name: "Comb Sort", Uuid: 6},
{Name: "Odd-Even Sort", Uuid: 7},
}
templates := &promptui.SelectTemplates{
Label: "{{ . }}?",
Active: "\U00002728 {{ .Name | cyan }}",
Inactive: " {{ .Name | cyan }}",
Selected: "\U00002728 {{ .Name | red | cyan }}",
Details: `
--------- Algorithm ----------
{{ "Name:" | faint }} {{ .Name }}`,
}
searcher := func(input string, index int) bool {
algorithm := algorithms[index]
name := strings.Replace(strings.ToLower(algorithm.Name), " ", "", -1)
input = strings.Replace(strings.ToLower(input), " ", "", -1)
return strings.Contains(name, input)
}
algorithmPrompt := promptui.Select{
Label: "Select a sorting algorithm!",
Items: algorithms,
Templates: templates,
Size: 4,
Searcher: searcher,
}
i, _, err := algorithmPrompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}
clearConsole()
actionPrompt := promptui.Select{
Label: "Select Action",
Items: []string{"Description", "Run"},
}
_, action, err := actionPrompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}
clearConsole()
displayPrompt := promptui.Select{
Label: "Select Type of displaying",
Items: []string{"Array", "Graph"},
}
_, displayType, err := displayPrompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}
switch action {
case "Description":
printAlgorithmDescription(algorithms[i].Uuid)
case "Run":
speedPrompt := promptui.Select{
Label: "Select Visualization Speed",
Items: []string{"Slow", "Medium", "Fast", "Very Fast"},
}
_, speed, err := speedPrompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}
var delay time.Duration
switch speed {
case "Slow":
delay = time.Second * 2
case "Medium":
delay = time.Second
case "Fast":
delay = time.Second / 3
case "Very Fast":
delay = time.Second / 6
default:
delay = time.Second
}
clearConsole()
runAlgorithm(algorithms[i].Uuid, arr, displayType, delay)
default:
fmt.Println("Invalid choice")
return
}
}
func runAlgorithm(algorithm int, arr []int, displayType string, delay time.Duration) {
switch algorithm {
case BubbleSort:
bubbleSortVisualizer(arr, displayType, delay)
case SelectionSort:
selectionSortVisualizer(arr, displayType, delay)
case InsertionSort:
insertionSortVisualizer(arr, displayType, delay)
case GnomeSort:
gnomeSortVisualizer(arr, displayType, delay)
case CocktailShakerSort:
cocktailShakerSortVisualizer(arr, displayType, delay)
case CombSort:
combSortVisualizer(arr, displayType, delay)
case OddEvenSort:
oddEvenSortVisualizer(arr, displayType, delay)
default:
fmt.Println("Invalid selection")
}
fmt.Println("Sorted array:", arr)
}