-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmenu.js
229 lines (196 loc) · 5.74 KB
/
menu.js
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
var items
var selectedIndex
var originalArray = []
var isSimulating
// Bubble Sort Variables
var end
var previousMarkedJ = -1
var levels = [
{
"name": "Bubble Sort",
"description": "Bubble sort is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.",
"function": bubble_sort,
"loop_function": bSortNext,
"array": [2,5,1,6,7,3],
"author": "Rudra Nil Basu",
"email": "[email protected]"
},
{
"name": "Selection Sort",
"description": "Arranging a given list of numbers in ascending order by Selection Sort",
"function": selection_sort,
"loop_function": sSortNext,
"array": [10,9,1,5,4,2],
"author": "Rudra Nil Basu",
"email": "[email protected]"
}
]
var i, j, length
function start(_items) {
items = _items
selectedIndex = -1
isSimulating = false
initialiseMenuItems()
}
function initialiseMenuItems() {
items.elements.model = levels.length
for(var i=0; i<items.elements.model;i++) {
items.elements.itemAt(i).topic = levels[i].name
items.elements.itemAt(i).index = i
}
}
function load(index) {
if(selectedIndex != -1) {
items.elements.itemAt(selectedIndex).selected = false
// reload
// set isSimulating = false
reload()
isSimulating = false
}
selectedIndex = index
items.elements.itemAt(selectedIndex).selected = true
items.header.text = levels[index].name
items.description.text = levels[index].description
items.credits.text = "<b>Credits</b>: " + levels[index].author + " ( " + levels[index].email + ")"
items.array.model = levels[index].array.length
initialiseArray()
}
function initialiseArray() {
for(var i = 0;i < items.array.model;i++) {
items.array.itemAt(i).value = levels[selectedIndex].array[i]
}
for(var i=0;i < levels[selectedIndex].array.length; i++) {
originalArray[i] = levels[selectedIndex].array[i]
}
}
function simulate() {
if(selectedIndex == -1 || isSimulating) {
return
}
// Deselect the selected item
items.elements.itemAt(selectedIndex).selected = false
levels[selectedIndex].function();
}
function loop() {
levels[selectedIndex].loop_function()
}
function isSorted() {
for(var i=0; i < originalArray.length-1;i++) {
if(originalArray[i] > originalArray[i+1]) {
return false
}
}
return true
}
function interchangeBlocksWithLabels(label1, label2) {
var block1, block2
for(var i = 0;i < items.array.model;i++) {
if(items.array.itemAt(i).value == label1) {
block1 = items.array.itemAt(i)
}
if(items.array.itemAt(i).value == label2) {
block2 = items.array.itemAt(i)
}
}
var tempx, tempy
tempx = block2.x
tempy = block2.y
block2.x = block1.x
block2.y = block1.y
block1.x = tempx
block1.y = tempy
}
function markRed(label) {
for(var i = 0;i < items.array.model;i++) {
if( items.array.itemAt(i).value == label) {
items.array.itemAt(i).selectBig = true
items.array.itemAt(i).color = "red"
return
}
}
}
function markBlue(label) {
for(var i = 0;i < items.array.model;i++) {
if( items.array.itemAt(i).value == label) {
items.array.itemAt(i).color = "blue"
return
}
}
}
function unmark(label) {
for(var i = 0;i < items.array.model;i++) {
if( items.array.itemAt(i).value == label) {
items.array.itemAt(i).notSelected = true
items.array.itemAt(i).color = "black"
return
}
}
}
function findBlockWithLabel(label) {
for(var i = 0;i < items.array.model;i++) {
if(items.array.itemAt(i).value == label) {
return items.array.itemAt(i)
}
}
}
function reload() {
for(var i=0;i<originalArray.length;i++) {
var currentBlock = findBlockWithLabel(levels[selectedIndex].array[i])
var finalPosition = findBlockWithLabel(originalArray[i])
currentBlock.x = finalPosition.x
currentBlock.y = finalPosition.y
}
}
/*
* Algorithms
* Functions for all the algorithms goes down here.
* Divided into two parts - the "function" (start function) and the "loop" (defines
* the iterations).
*/
// Bubble Sort starting function
function bubble_sort() {
// if originalArray is already sorted, return
if(isSorted()) {
return
}
isSimulating = true
length = originalArray.length
i=0
j=0
previousMarkedJ = -1
end = length -1
// start timer
items.timer.running = true
items.timer.repeat = true
}
// Bubble Sort iterative part
function bSortNext() {
if(previousMarkedJ != -1) {
unmark(originalArray[previousMarkedJ])
unmark(originalArray[previousMarkedJ+1])
}
markRed(originalArray[j])
markBlue(originalArray[j+1])
if(originalArray[j] > originalArray[j+1]) {
interchangeBlocksWithLabels(originalArray[j], originalArray[j+1])
var temp = originalArray[j]
originalArray[j] = originalArray[j+1]
originalArray[j+1] = temp
}
previousMarkedJ = j
j++
if(j >= end -i) {
i++
j = 0
if(i >= end) {
unmark(originalArray[previousMarkedJ])
unmark(originalArray[previousMarkedJ+1])
items.timer.running = false
items.timer.repeat = false
}
}
}
function selection_sort() {
console.log("S Sort it")
}
function sSortNext() {}