-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
143 lines (121 loc) · 2.84 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
package main
import (
"bufio"
"fmt"
"os"
"strings"
"time"
"github.com/manifoldco/promptui"
)
type options struct {
Name SortingAlgorithm
}
func main() {
clearConsole()
r := bufio.NewReader(os.Stdin)
n := getSliceSize(r)
arr := make([]int, 0, n)
fillSliceWithRandElems(&arr)
fmt.Println("Initial array:", arr)
algorithms := []options{
{Name: BubbleSort},
{Name: SelectionSort},
{Name: InsertionSort},
{Name: GnomeSort},
{Name: CocktailShakerSort},
{Name: CombSort},
{Name: OddEvenSort},
}
templates := &promptui.SelectTemplates{
Label: "{{ . }}?",
Active: "--> {{ .Name | cyan }}",
Inactive: " {{ .Name | cyan }}",
Selected: "--> {{ .Name | cyan }}",
Details: `
--------- Algorithm ----------
{{ "Name:" | faint }} {{ .Name }}
`,
}
searcher := func(input string, index int) bool {
algorithm := algorithms[index]
name := strings.Replace(strings.ToLower(algorithm.Name.String()), " ", "", -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: %s\n", err.Error())
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()
switch action {
case "Description":
printAlgorithmDescription(algorithms[i].Name)
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.Millisecond * 1500
case "Medium":
delay = time.Second
case "Fast":
delay = time.Second / 2
case "Very Fast":
delay = time.Second / 4
default:
delay = time.Second
}
clearConsole()
runAlgorithm(algorithms[i].Name, arr, delay)
default:
fmt.Println("Invalid choice")
return
}
}
func runAlgorithm(algorithm SortingAlgorithm, arr []int, delay time.Duration) {
switch algorithm {
case BubbleSort:
bubbleSortVisualizer(arr, delay)
case SelectionSort:
selectionSortVisualizer(arr, delay)
case InsertionSort:
insertionSortVisualizer(arr, delay)
case GnomeSort:
gnomeSortVisualizer(arr, delay)
case CocktailShakerSort:
cocktailShakerSortVisualizer(arr, delay)
case CombSort:
combSortVisualizer(arr, delay)
case OddEvenSort:
oddEvenSortVisualizer(arr, delay)
default:
fmt.Println("Invalid selection")
}
fmt.Println("Sorted array:", arr)
}